Echo9k commited on
Commit
0dd8dfd
·
1 Parent(s): 3ba48f6

Download models checks in automatic the existance of the models from app.py execution.

Browse files
Files changed (3) hide show
  1. app.py +7 -0
  2. download_models_hf.py +44 -27
  3. tts.py +2 -2
app.py CHANGED
@@ -8,6 +8,9 @@ from pdf_processor import to_pdf, to_markdown
8
  from config import config
9
  from tts import text_to_speech # Import TTS module
10
 
 
 
 
11
  # Set up logging with ANSI escape codes for colored output
12
  logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
13
 
@@ -117,4 +120,8 @@ with gr.Blocks() as demo:
117
  clear_button.add([file_input, md_render, pdf_display, md_text, output_file, is_ocr])
118
 
119
  if __name__ == "__main__":
 
 
 
 
120
  demo.launch(ssr_mode=True)
 
8
  from config import config
9
  from tts import text_to_speech # Import TTS module
10
 
11
+
12
+
13
+
14
  # Set up logging with ANSI escape codes for colored output
15
  logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
16
 
 
120
  clear_button.add([file_input, md_render, pdf_display, md_text, output_file, is_ocr])
121
 
122
  if __name__ == "__main__":
123
+ import subprocess
124
+ print("Checking and downloading models if necessary...")
125
+ subprocess.run(["python", "download_models.py"])
126
+ print("Models are ready!")
127
  demo.launch(ssr_mode=True)
download_models_hf.py CHANGED
@@ -6,61 +6,78 @@ from huggingface_hub import snapshot_download
6
 
7
 
8
  def download_json(url):
9
- # 下载JSON文件
10
  response = requests.get(url)
11
- response.raise_for_status() # 检查请求是否成功
12
  return response.json()
13
 
14
 
15
  def download_and_modify_json(url, local_filename, modifications):
 
16
  if os.path.exists(local_filename):
17
- data = json.load(open(local_filename))
 
18
  config_version = data.get('config_version', '0.0.0')
 
 
19
  if config_version < '1.1.1':
20
  data = download_json(url)
21
  else:
22
  data = download_json(url)
23
 
24
- # 修改内容
25
  for key, value in modifications.items():
26
  data[key] = value
27
 
28
- # 保存修改后的内容
29
  with open(local_filename, 'w', encoding='utf-8') as f:
30
  json.dump(data, f, ensure_ascii=False, indent=4)
31
 
32
 
33
- if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- mineru_patterns = [
36
- "models/Layout/LayoutLMv3/*",
37
- "models/Layout/YOLO/*",
38
- "models/MFD/YOLO/*",
39
- "models/MFR/unimernet_small_2501/*",
40
- "models/TabRec/TableMaster/*",
41
- "models/TabRec/StructEqTable/*",
42
- ]
43
- model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', allow_patterns=mineru_patterns)
44
-
45
- layoutreader_pattern = [
46
- "*.json",
47
- "*.safetensors",
48
- ]
49
- layoutreader_model_dir = snapshot_download('hantian/layoutreader', allow_patterns=layoutreader_pattern)
50
-
51
- model_dir = model_dir + '/models'
52
- print(f'model_dir is: {model_dir}')
53
- print(f'layoutreader_model_dir is: {layoutreader_model_dir}')
54
 
 
55
  json_url = 'https://github.com/opendatalab/MinerU/raw/master/magic-pdf.template.json'
56
  config_file_name = 'magic-pdf.json'
57
  home_dir = os.path.expanduser('~')
58
  config_file = os.path.join(home_dir, config_file_name)
59
 
60
  json_mods = {
61
- 'models-dir': model_dir,
62
- 'layoutreader-model-dir': layoutreader_model_dir,
63
  }
64
 
65
  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}')
 
 
 
 
 
6
 
7
 
8
  def download_json(url):
9
+ """Download JSON file from a URL."""
10
  response = requests.get(url)
11
+ response.raise_for_status()
12
  return response.json()
13
 
14
 
15
  def download_and_modify_json(url, local_filename, modifications):
16
+ """Download and modify a JSON file if it doesn't exist or if it's outdated."""
17
  if os.path.exists(local_filename):
18
+ with open(local_filename, 'r', encoding='utf-8') as f:
19
+ data = json.load(f)
20
  config_version = data.get('config_version', '0.0.0')
21
+
22
+ # Only re-download if the version is outdated
23
  if config_version < '1.1.1':
24
  data = download_json(url)
25
  else:
26
  data = download_json(url)
27
 
28
+ # Apply modifications
29
  for key, value in modifications.items():
30
  data[key] = value
31
 
32
+ # Save modified JSON
33
  with open(local_filename, 'w', encoding='utf-8') as f:
34
  json.dump(data, f, ensure_ascii=False, indent=4)
35
 
36
 
37
+ def check_and_download_models():
38
+ """Download models only if they are not already present."""
39
+ model_base_path = os.path.expanduser("~/.cache/huggingface/hub")
40
+
41
+ mineru_models_path = os.path.join(model_base_path, "opendatalab__PDF-Extract-Kit-1.0")
42
+ layoutreader_models_path = os.path.join(model_base_path, "hantian__layoutreader")
43
+
44
+ # Check if models exist before downloading
45
+ if not os.path.exists(mineru_models_path):
46
+ mineru_patterns = [
47
+ "models/Layout/LayoutLMv3/*",
48
+ "models/Layout/YOLO/*",
49
+ "models/MFD/YOLO/*",
50
+ "models/MFR/unimernet_small_2501/*",
51
+ "models/TabRec/TableMaster/*",
52
+ "models/TabRec/StructEqTable/*",
53
+ ]
54
+ mineru_models_path = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', allow_patterns=mineru_patterns)
55
+
56
+ if not os.path.exists(layoutreader_models_path):
57
+ layoutreader_pattern = [
58
+ "*.json",
59
+ "*.safetensors",
60
+ ]
61
+ layoutreader_models_path = snapshot_download('hantian/layoutreader', allow_patterns=layoutreader_pattern)
62
 
63
+ # Print paths
64
+ print(f'model_dir is: {mineru_models_path}/models')
65
+ print(f'layoutreader_model_dir is: {layoutreader_models_path}')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # JSON configuration update
68
  json_url = 'https://github.com/opendatalab/MinerU/raw/master/magic-pdf.template.json'
69
  config_file_name = 'magic-pdf.json'
70
  home_dir = os.path.expanduser('~')
71
  config_file = os.path.join(home_dir, config_file_name)
72
 
73
  json_mods = {
74
+ 'models-dir': f"{mineru_models_path}/models",
75
+ 'layoutreader-model-dir': layoutreader_models_path,
76
  }
77
 
78
  download_and_modify_json(json_url, config_file, json_mods)
79
  print(f'The configuration file has been configured successfully, the path is: {config_file}')
80
+
81
+
82
+ if __name__ == '__main__':
83
+ check_and_download_models()
tts.py CHANGED
@@ -17,7 +17,7 @@ def text_to_speech(text: str, voice: str = "coral", model: str = "tts-1") -> str
17
  The file path to the generated audio file.
18
  """
19
  # Generate a unique filename using a hash of the text
20
- output_file = Path(__file__).parent / f"speech_{abs(hash(text))}.pus"
21
  try:
22
  response = openai.Audio.speech.create(
23
  model=model,
@@ -38,7 +38,7 @@ def text_to_speech_gtts(text: str) -> str:
38
  Returns:
39
  The file path to the generated audio file.
40
  """
41
- output_file = Path(__file__).parent / f"speech_{abs(hash(text))}.mp3"
42
  try:
43
  tts = gTTS(text=text, lang='en')
44
  tts.save(str(output_file))
 
17
  The file path to the generated audio file.
18
  """
19
  # Generate a unique filename using a hash of the text
20
+ output_file = Path(__file__).parent / 'output' / f"speech_{abs(hash(text))}.pus"
21
  try:
22
  response = openai.Audio.speech.create(
23
  model=model,
 
38
  Returns:
39
  The file path to the generated audio file.
40
  """
41
+ output_file = Path(__file__).parent / 'output' / f"speech_{abs(hash(text))}.mp3"
42
  try:
43
  tts = gTTS(text=text, lang='en')
44
  tts.save(str(output_file))