| | import json |
| | import os |
| |
|
| | from loguru import logger |
| | import requests |
| | from huggingface_hub import snapshot_download |
| |
|
| | def download_json(url): |
| | 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') |
| | if config_version < '1.2.0': |
| | 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) |
| |
|
| |
|
| | def initialize_models(): |
| | """ |
| | Downloads and configures all necessary models and settings. |
| | This function is designed to be called once at application startup. |
| | """ |
| | mineru_patterns = [ |
| | |
| | "models/Layout/YOLO/*", |
| | "models/MFD/YOLO/*", |
| | "models/MFR/unimernet_hf_small_2503/*", |
| | "models/OCR/paddleocr_torch/*", |
| | |
| | |
| | ] |
| | 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) |
| |
|
| | model_dir = model_dir + '/models' |
| | logger.info(f'model_dir is: {model_dir}') |
| | logger.info(f'layoutreader_model_dir is: {layoutreader_model_dir}') |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | json_url = 'https://github.com/opendatalab/MinerU/raw/master/mineru.template.json' |
| | 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) |
| | logger.info(f'The configuration file has been configured successfully, the path is: {config_file}') |
| |
|
| |
|
| | if __name__ == '__main__': |
| | initialize_models() |
| |
|