download_models_hf.py 2 KB
Newer Older
1
import json
2
import os
3

4
import requests
drunkpig's avatar
drunkpig committed
5
from huggingface_hub import snapshot_download
6
7


8
9
10
11
12
13
14
def download_json(url):
    # 下载JSON文件
    response = requests.get(url)
    response.raise_for_status()  # 检查请求是否成功
    return response.json()


15
16
17
def download_and_modify_json(url, local_filename, modifications):
    if os.path.exists(local_filename):
        data = json.load(open(local_filename))
18
19
20
        config_version = data.get('config_version', '0.0.0')
        if config_version < '1.0.0':
            data = download_json(url)
21
    else:
22
        data = download_json(url)
23
24
25
26
27
28
29
30
31
32
33

    # 修改内容
    for key, value in modifications.items():
        data[key] = value

    # 保存修改后的内容
    with open(local_filename, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)


if __name__ == '__main__':
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    mineru_patterns = [
        "models/Layout/LayoutLMv3/*",
        "models/Layout/YOLO/*",
        "models/MFD/YOLO/*",
        "models/MFR/unimernet_small/*",
        "models/TabRec/TableMaster/*",
        "models/TabRec/StructEqTable/*",
    ]
    model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', allow_patterns=mineru_patterns)

    layoutreader_pattern = [
        "*.json",
        "*.safetensors",
    ]
    layoutreader_model_dir = snapshot_download('hantian/layoutreader', allow_patterns=layoutreader_pattern)

51
52
53
    model_dir = model_dir + '/models'
    print(f'model_dir is: {model_dir}')
    print(f'layoutreader_model_dir is: {layoutreader_model_dir}')
54

55
    json_url = 'https://github.com/opendatalab/MinerU/raw/dev/magic-pdf.template.json'
56
57
    config_file_name = 'magic-pdf.json'
    home_dir = os.path.expanduser('~')
58
59
60
61
62
63
64
65
    config_file = os.path.join(home_dir, config_file_name)

    json_mods = {
        'models-dir': model_dir,
        'layoutreader-model-dir': layoutreader_model_dir,
    }

    download_and_modify_json(json_url, config_file, json_mods)
66
    print(f'The configuration file has been configured successfully, the path is: {config_file}')