cstr commited on
Commit
49f0835
·
verified ·
1 Parent(s): 1f1e9a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -58,33 +58,57 @@ UI_TEXT = {
58
  }
59
 
60
  # --- 2. Model Loading (EXPANDED TO 7 MODELS) ---
61
- MODEL_NAMES = {
62
- "de": "de_core_news_md",
63
- "en": "en_core_web_md",
64
- "es": "es_core_news_md",
65
- "la": "la_core_web_sm", # Medium model not available for core Latin
66
- "grc": "grc_core_news_md", # Ancient Greek
67
- "he": "he_core_news_md", # Hebrew
68
- "ar": "ar_core_news_md" # Arabic
 
 
 
 
 
 
 
69
  }
70
 
71
- def load_model(model_name):
72
- """Checks if model is installed and downloads it if not."""
 
 
 
 
 
73
  try:
74
  nlp = spacy.load(model_name)
75
- print(f"{model_name} loaded successfully.")
76
  except OSError:
77
- print(f"{model_name} not found. Downloading...")
78
- # This command runs in the Hugging Face environment
79
- os.system(f"python -m spacy download {model_name}")
 
 
 
 
 
 
 
 
 
 
 
80
  nlp = spacy.load(model_name)
81
- print(f"{model_name} downloaded and loaded.")
82
  return nlp
83
 
 
84
  # Load all models at startup and store them in a dictionary
85
  print("Loading all models...")
86
  MODELS = {
87
- lang_code: load_model(model_name) for lang_code, model_name in MODEL_NAMES.items()
88
  }
89
  print("All models loaded successfully.")
90
 
 
58
  }
59
 
60
  # --- 2. Model Loading (EXPANDED TO 7 MODELS) ---
61
+
62
+ # We now store a tuple: (model_name_to_load, install_package_name_or_url)
63
+ # For 'la', we use the direct wheel URL as the package.
64
+ # This URL is for spaCy v3.8.x, which matches the HF Space log
65
+ LATIN_URL = "https://github.com/explosion/spacy-models/releases/download/la_core_web_sm-3.8.0/la_core_web_sm-3.8.0-py3-none-any.whl"
66
+
67
+ MODEL_INFO = {
68
+ # lang_code: (model_name, install_package)
69
+ "de": ("de_core_news_md", "de_core_news_md"),
70
+ "en": ("en_core_web_md", "en_core_web_md"),
71
+ "es": ("es_core_news_md", "es_core_news_md"),
72
+ "la": ("la_core_web_sm", f"pip install {LATIN_URL}"), # <-- The Fix from previous step
73
+ "grc": ("grc_core_news_md", "grc_core_news_md"),
74
+ "he": ("he_core_news_md", "he_core_news_md"),
75
+ "ar": ("ar_core_news_md", "ar_core_news_md")
76
  }
77
 
78
+
79
+ def load_model(model_info):
80
+ """
81
+ Checks if model is installed. If not, attempts to download it
82
+ using the specific install command provided in MODEL_INFO.
83
+ """
84
+ model_name, install_command = model_info
85
  try:
86
  nlp = spacy.load(model_name)
87
+ print(f"Model {model_name} loaded successfully.")
88
  except OSError:
89
+ print(f"Model {model_name} not found. Attempting download...")
90
+
91
+ # Use pip install for the Latin URL, spacy download for others
92
+ if not install_command.startswith("pip"):
93
+ install_command = f"python -m spacy download {install_command}"
94
+
95
+ print(f"Running: {install_command}")
96
+ exit_code = os.system(install_command) # Run the correct command
97
+
98
+ if exit_code != 0:
99
+ print(f"--- ERROR: Failed to download {model_name}. Exit code: {exit_code} ---")
100
+ # This will raise a final error, as the load will fail again
101
+
102
+ # Try loading again after install attempt
103
  nlp = spacy.load(model_name)
104
+ print(f"Model {model_name} downloaded and loaded.")
105
  return nlp
106
 
107
+
108
  # Load all models at startup and store them in a dictionary
109
  print("Loading all models...")
110
  MODELS = {
111
+ lang_code: load_model(model_info) for lang_code, model_info in MODEL_INFO.items()
112
  }
113
  print("All models loaded successfully.")
114