download_models.py 2.2 KB
Newer Older
xu rui's avatar
xu rui committed
1
import json
2
import shutil
xu rui's avatar
xu rui committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os

import requests
from modelscope import snapshot_download


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


def download_and_modify_json(url, local_filename, modifications):
    if os.path.exists(local_filename):
        data = json.load(open(local_filename))
        config_version = data.get('config_version', '0.0.0')
20
        if config_version < '1.2.0':
xu rui's avatar
xu rui committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
            data = download_json(url)
    else:
        data = download_json(url)

    # 修改内容
    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__':
    mineru_patterns = [
36
        # "models/Layout/LayoutLMv3/*",
xu rui's avatar
xu rui committed
37
38
        "models/Layout/YOLO/*",
        "models/MFD/YOLO/*",
39
        "models/MFR/unimernet_hf_small_2503/*",
40
        "models/OCR/paddleocr/*",
41
42
        # "models/TabRec/TableMaster/*",
        # "models/TabRec/StructEqTable/*",
xu rui's avatar
xu rui committed
43
44
45
46
47
48
49
    ]
    model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', allow_patterns=mineru_patterns)
    layoutreader_model_dir = snapshot_download('ppaanngggg/layoutreader')
    model_dir = model_dir + '/models'
    print(f'model_dir is: {model_dir}')
    print(f'layoutreader_model_dir is: {layoutreader_model_dir}')

50
51
52
53
54
55
    paddleocr_model_dir = model_dir + '/OCR/paddleocr'
    user_paddleocr_dir = os.path.expanduser('~/.paddleocr')
    if os.path.exists(user_paddleocr_dir):
        shutil.rmtree(user_paddleocr_dir)
    shutil.copytree(paddleocr_model_dir, user_paddleocr_dir)

56
    json_url = 'https://gcore.jsdelivr.net/gh/opendatalab/MinerU@master/magic-pdf.template.json'
xu rui's avatar
xu rui committed
57
58
59
60
61
62
63
64
65
66
67
    config_file_name = 'magic-pdf.json'
    home_dir = os.path.expanduser('~')
    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)
    print(f'The configuration file has been configured successfully, the path is: {config_file}')