Update app.py
Browse files
app.py
CHANGED
|
@@ -58,33 +58,57 @@ UI_TEXT = {
|
|
| 58 |
}
|
| 59 |
|
| 60 |
# --- 2. Model Loading (EXPANDED TO 7 MODELS) ---
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 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 |
|