Spaces:
Sleeping
Sleeping
Update backend/app/services/cefr_predictor.py
Browse files
backend/app/services/cefr_predictor.py
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
import joblib
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
class CEFRPredictor:
|
| 9 |
def __init__(self):
|
| 10 |
if not MODEL_PATH.exists():
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
self.model = joblib.load(MODEL_PATH)
|
| 13 |
|
| 14 |
-
def predict(self, text: str)
|
| 15 |
-
text = (text or "").strip()
|
| 16 |
-
if not text:
|
| 17 |
-
return "A2"
|
| 18 |
return self.model.predict([text])[0]
|
|
|
|
| 1 |
+
import os
|
| 2 |
from pathlib import Path
|
| 3 |
import joblib
|
| 4 |
|
| 5 |
+
MODEL_PATH = Path(os.getenv("CEFR_MODEL_PATH", "/app/ml/models/cefr_model.pkl"))
|
| 6 |
+
|
| 7 |
+
HF_REPO_ID = os.getenv("CEFR_MODEL_REPO", "Zinebhm/cefr-model-learnlanguage")
|
| 8 |
+
HF_FILENAME = os.getenv("CEFR_MODEL_FILENAME", "cefr_model.pkl")
|
| 9 |
|
| 10 |
class CEFRPredictor:
|
| 11 |
def __init__(self):
|
| 12 |
if not MODEL_PATH.exists():
|
| 13 |
+
MODEL_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 14 |
+
# download from Hugging Face
|
| 15 |
+
from huggingface_hub import hf_hub_download
|
| 16 |
+
downloaded = hf_hub_download(
|
| 17 |
+
repo_id=HF_REPO_ID,
|
| 18 |
+
filename=HF_FILENAME,
|
| 19 |
+
repo_type="model",
|
| 20 |
+
)
|
| 21 |
+
# copy to expected path
|
| 22 |
+
os.replace(downloaded, MODEL_PATH)
|
| 23 |
+
|
| 24 |
self.model = joblib.load(MODEL_PATH)
|
| 25 |
|
| 26 |
+
def predict(self, text: str):
|
|
|
|
|
|
|
|
|
|
| 27 |
return self.model.predict([text])[0]
|