Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,15 @@ import numpy as np
|
|
| 6 |
import pandas as pd
|
| 7 |
import joblib, os, sys, types, traceback
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
# ============================================================
|
| 12 |
USE_HF_HUB = True
|
| 13 |
HF_REPO_ID = "reub1/teacher_ai"
|
| 14 |
HF_FILENAME = "model (3).joblib"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
def to_float(x):
|
| 20 |
try:
|
| 21 |
return x.astype(float)
|
|
@@ -37,9 +36,7 @@ def _expose_helpers_in_main():
|
|
| 37 |
for name, fn in {"to_float": to_float, "to_1d": to_1d, "ravel_1d": ravel_1d}.items():
|
| 38 |
setattr(mod, name, fn)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
# 📦 Schémas Pydantic selon la spécification officielle
|
| 42 |
-
# ============================================================
|
| 43 |
class Diploma(BaseModel):
|
| 44 |
level: Optional[str] = ""
|
| 45 |
title: Optional[str] = ""
|
|
@@ -56,7 +53,7 @@ class PastCourse(BaseModel):
|
|
| 56 |
numberOfStars: Optional[float] = 0.0
|
| 57 |
|
| 58 |
class Professor(BaseModel):
|
| 59 |
-
firstname: Optional[str] = ""
|
| 60 |
lastname: Optional[str] = ""
|
| 61 |
city: Optional[str] = ""
|
| 62 |
description: Optional[str] = ""
|
|
@@ -72,9 +69,7 @@ class Payload(BaseModel):
|
|
| 72 |
course: Course
|
| 73 |
professor: Professor
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
# 🧠 Fonctions utilitaires
|
| 77 |
-
# ============================================================
|
| 78 |
def safe_str(x: Optional[str]) -> str:
|
| 79 |
return (x or "").strip()
|
| 80 |
|
|
@@ -114,9 +109,8 @@ def build_prof_global_text(p: Professor) -> str:
|
|
| 114 |
parts += [c.title or "", c.description or ""]
|
| 115 |
return " ".join([t for t in parts if t]).lower()
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
# ============================================================
|
| 120 |
app = FastAPI(
|
| 121 |
title="AI Teacher Rating Predictor",
|
| 122 |
description="API de prédiction de la satisfaction moyenne d'un professeur (gradeAverage).",
|
|
@@ -135,24 +129,23 @@ def load_model():
|
|
| 135 |
from huggingface_hub import hf_hub_download
|
| 136 |
fp = hf_hub_download(repo_id=HF_REPO_ID, filename=HF_FILENAME, repo_type="model")
|
| 137 |
model = joblib.load(fp)
|
| 138 |
-
print(f"
|
| 139 |
return
|
| 140 |
except Exception as e:
|
| 141 |
-
print(f"
|
| 142 |
# fallback local
|
| 143 |
local_path = os.path.join("artifacts", "model.joblib")
|
| 144 |
if not os.path.exists(local_path):
|
| 145 |
-
raise RuntimeError("
|
| 146 |
model = joblib.load(local_path)
|
| 147 |
-
print(f"
|
| 148 |
|
| 149 |
@app.get("/health")
|
| 150 |
def health_check():
|
| 151 |
return {"status": "ok"}
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
# ============================================================
|
| 156 |
@app.post("/api/predict")
|
| 157 |
def predict(payload: Payload):
|
| 158 |
try:
|
|
@@ -189,11 +182,11 @@ def predict(payload: Payload):
|
|
| 189 |
return {"gradeAverage": round(grade, 2)}
|
| 190 |
|
| 191 |
except Exception as e:
|
| 192 |
-
print("
|
| 193 |
raise HTTPException(status_code=500, detail=f"Erreur interne : {str(e)}")
|
| 194 |
|
| 195 |
# ============================================================
|
| 196 |
-
#
|
| 197 |
# ============================================================
|
| 198 |
if __name__ == "__main__":
|
| 199 |
import uvicorn, os
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
import joblib, os, sys, types, traceback
|
| 8 |
|
| 9 |
+
# CONFIGURATION
|
| 10 |
+
|
|
|
|
| 11 |
USE_HF_HUB = True
|
| 12 |
HF_REPO_ID = "reub1/teacher_ai"
|
| 13 |
HF_FILENAME = "model (3).joblib"
|
| 14 |
|
| 15 |
+
|
| 16 |
+
# Helpers nécessaires pour le modèle picklé
|
| 17 |
+
|
| 18 |
def to_float(x):
|
| 19 |
try:
|
| 20 |
return x.astype(float)
|
|
|
|
| 36 |
for name, fn in {"to_float": to_float, "to_1d": to_1d, "ravel_1d": ravel_1d}.items():
|
| 37 |
setattr(mod, name, fn)
|
| 38 |
|
| 39 |
+
# Schémas Pydantic selon la spécification officielle
|
|
|
|
|
|
|
| 40 |
class Diploma(BaseModel):
|
| 41 |
level: Optional[str] = ""
|
| 42 |
title: Optional[str] = ""
|
|
|
|
| 53 |
numberOfStars: Optional[float] = 0.0
|
| 54 |
|
| 55 |
class Professor(BaseModel):
|
| 56 |
+
firstname: Optional[str] = ""
|
| 57 |
lastname: Optional[str] = ""
|
| 58 |
city: Optional[str] = ""
|
| 59 |
description: Optional[str] = ""
|
|
|
|
| 69 |
course: Course
|
| 70 |
professor: Professor
|
| 71 |
|
| 72 |
+
# Fonctions utilitaires
|
|
|
|
|
|
|
| 73 |
def safe_str(x: Optional[str]) -> str:
|
| 74 |
return (x or "").strip()
|
| 75 |
|
|
|
|
| 109 |
parts += [c.title or "", c.description or ""]
|
| 110 |
return " ".join([t for t in parts if t]).lower()
|
| 111 |
|
| 112 |
+
# FastAPI App
|
| 113 |
+
|
|
|
|
| 114 |
app = FastAPI(
|
| 115 |
title="AI Teacher Rating Predictor",
|
| 116 |
description="API de prédiction de la satisfaction moyenne d'un professeur (gradeAverage).",
|
|
|
|
| 129 |
from huggingface_hub import hf_hub_download
|
| 130 |
fp = hf_hub_download(repo_id=HF_REPO_ID, filename=HF_FILENAME, repo_type="model")
|
| 131 |
model = joblib.load(fp)
|
| 132 |
+
print(f" Modèle chargé depuis Hugging Face Hub : {fp}")
|
| 133 |
return
|
| 134 |
except Exception as e:
|
| 135 |
+
print(f"Impossible de charger depuis HF Hub : {e}")
|
| 136 |
# fallback local
|
| 137 |
local_path = os.path.join("artifacts", "model.joblib")
|
| 138 |
if not os.path.exists(local_path):
|
| 139 |
+
raise RuntimeError(" Modèle introuvable (ni HF, ni local).")
|
| 140 |
model = joblib.load(local_path)
|
| 141 |
+
print(f" Modèle chargé localement : {local_path}")
|
| 142 |
|
| 143 |
@app.get("/health")
|
| 144 |
def health_check():
|
| 145 |
return {"status": "ok"}
|
| 146 |
|
| 147 |
+
# Endpoint principal /api/predict
|
| 148 |
+
|
|
|
|
| 149 |
@app.post("/api/predict")
|
| 150 |
def predict(payload: Payload):
|
| 151 |
try:
|
|
|
|
| 182 |
return {"gradeAverage": round(grade, 2)}
|
| 183 |
|
| 184 |
except Exception as e:
|
| 185 |
+
print(" ERREUR INTERNE :", traceback.format_exc())
|
| 186 |
raise HTTPException(status_code=500, detail=f"Erreur interne : {str(e)}")
|
| 187 |
|
| 188 |
# ============================================================
|
| 189 |
+
# Lancement local
|
| 190 |
# ============================================================
|
| 191 |
if __name__ == "__main__":
|
| 192 |
import uvicorn, os
|