Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,12 @@ from huggingface_hub import hf_hub_download
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
|
| 8 |
# ============================================================
|
| 9 |
-
# CONFIG
|
| 10 |
# ============================================================
|
| 11 |
|
| 12 |
HF_USER = "ClergeF"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
MODEL_REPOS = {
|
| 16 |
-
"value_impact": "value-impact-model",
|
| 17 |
-
"impact": "impact-model",
|
| 18 |
"family": "family-model",
|
| 19 |
"community": "community-model",
|
| 20 |
"education": "education-model",
|
|
@@ -27,10 +24,7 @@ MODEL_REPOS = {
|
|
| 27 |
"innovation": "innovation-model",
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
MODEL_FILES = {
|
| 32 |
-
"value_impact": "value_impact.json",
|
| 33 |
-
"impact": "impact.json",
|
| 34 |
"family": "family_level.json",
|
| 35 |
"community": "community_level.json",
|
| 36 |
"education": "education_level.json",
|
|
@@ -44,70 +38,54 @@ MODEL_FILES = {
|
|
| 44 |
}
|
| 45 |
|
| 46 |
# ============================================================
|
| 47 |
-
#
|
| 48 |
# ============================================================
|
| 49 |
|
| 50 |
-
# Keep it light + consistent for Spaces
|
| 51 |
print("Loading embedder: all-MiniLM-L6-v2 …")
|
| 52 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 53 |
|
| 54 |
-
|
| 55 |
# ============================================================
|
| 56 |
-
#
|
| 57 |
# ============================================================
|
| 58 |
|
| 59 |
-
def
|
| 60 |
-
|
| 61 |
-
print(f"↳ Loading {filename} from {repo_name} …")
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
path = hf_hub_download(
|
| 64 |
-
repo_id=f"{HF_USER}/{
|
| 65 |
filename=filename
|
| 66 |
)
|
| 67 |
-
|
| 68 |
with open(path, "r") as f:
|
| 69 |
return json.load(f)
|
| 70 |
|
| 71 |
|
| 72 |
-
def embed(text: str):
|
| 73 |
-
"""Returns a 384-dim sentence embedding."""
|
| 74 |
-
return embedder.encode([text])[0]
|
| 75 |
-
|
| 76 |
-
|
| 77 |
def linear_predict(model_json, vec):
|
| 78 |
-
"""Linear model forward pass using coef + intercept."""
|
| 79 |
coef = np.array(model_json["coef"])
|
| 80 |
intercept = np.array(model_json["intercept"])
|
| 81 |
-
|
| 82 |
-
if coef.ndim == 2: # Multi-output
|
| 83 |
-
return coef @ vec + intercept
|
| 84 |
-
else:
|
| 85 |
-
return float(np.dot(coef, vec) + intercept)
|
| 86 |
-
|
| 87 |
|
| 88 |
# ============================================================
|
| 89 |
-
# LOAD
|
| 90 |
# ============================================================
|
| 91 |
|
| 92 |
-
print("Loading
|
| 93 |
-
|
| 94 |
-
loaded_models = {}
|
| 95 |
-
|
| 96 |
-
for key in MODEL_REPOS:
|
| 97 |
-
repo = MODEL_REPOS[key]
|
| 98 |
-
file = MODEL_FILES[key]
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
|
|
|
|
| 105 |
|
| 106 |
# ============================================================
|
| 107 |
-
#
|
| 108 |
# ============================================================
|
| 109 |
|
| 110 |
-
app = FastAPI(title="
|
| 111 |
|
| 112 |
|
| 113 |
class InputText(BaseModel):
|
|
@@ -115,33 +93,20 @@ class InputText(BaseModel):
|
|
| 115 |
|
| 116 |
|
| 117 |
@app.get("/")
|
| 118 |
-
def
|
| 119 |
-
return {"status": "ok", "message": "
|
| 120 |
|
| 121 |
|
| 122 |
-
# ============================================================
|
| 123 |
-
# PREDICT ROUTE
|
| 124 |
-
# ============================================================
|
| 125 |
-
|
| 126 |
@app.post("/predict")
|
| 127 |
def predict(payload: InputText):
|
| 128 |
text = payload.text
|
| 129 |
vec = embed(text)
|
| 130 |
|
| 131 |
out = {}
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
value_pred, impact_pred = linear_predict(loaded_models["value_impact"], vec)
|
| 135 |
-
out["estimated_value"] = float(value_pred)
|
| 136 |
-
out["impact_level"] = float(impact_pred)
|
| 137 |
-
|
| 138 |
-
# ALL OTHER SINGLE-OUTPUT MODELS
|
| 139 |
-
for key in MODEL_REPOS:
|
| 140 |
-
if key == "value_impact":
|
| 141 |
-
continue # skip, already handled
|
| 142 |
-
out[key] = float(linear_predict(loaded_models[key], vec))
|
| 143 |
|
| 144 |
return {
|
| 145 |
"input": text,
|
| 146 |
-
"
|
| 147 |
}
|
|
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
|
| 8 |
# ============================================================
|
| 9 |
+
# CONFIG — ONLY CATEGORY MODELS
|
| 10 |
# ============================================================
|
| 11 |
|
| 12 |
HF_USER = "ClergeF"
|
| 13 |
|
| 14 |
+
CATEGORY_REPOS = {
|
|
|
|
|
|
|
|
|
|
| 15 |
"family": "family-model",
|
| 16 |
"community": "community-model",
|
| 17 |
"education": "education-model",
|
|
|
|
| 24 |
"innovation": "innovation-model",
|
| 25 |
}
|
| 26 |
|
| 27 |
+
CATEGORY_FILES = {
|
|
|
|
|
|
|
|
|
|
| 28 |
"family": "family_level.json",
|
| 29 |
"community": "community_level.json",
|
| 30 |
"education": "education_level.json",
|
|
|
|
| 38 |
}
|
| 39 |
|
| 40 |
# ============================================================
|
| 41 |
+
# EMBEDDER
|
| 42 |
# ============================================================
|
| 43 |
|
|
|
|
| 44 |
print("Loading embedder: all-MiniLM-L6-v2 …")
|
| 45 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 46 |
|
|
|
|
| 47 |
# ============================================================
|
| 48 |
+
# HELPERS
|
| 49 |
# ============================================================
|
| 50 |
|
| 51 |
+
def embed(text: str):
|
| 52 |
+
return embedder.encode([text])[0]
|
|
|
|
| 53 |
|
| 54 |
+
|
| 55 |
+
def load_model(repo, filename):
|
| 56 |
+
print(f"Loading: {repo}/{filename}")
|
| 57 |
path = hf_hub_download(
|
| 58 |
+
repo_id=f"{HF_USER}/{repo}",
|
| 59 |
filename=filename
|
| 60 |
)
|
|
|
|
| 61 |
with open(path, "r") as f:
|
| 62 |
return json.load(f)
|
| 63 |
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def linear_predict(model_json, vec):
|
|
|
|
| 66 |
coef = np.array(model_json["coef"])
|
| 67 |
intercept = np.array(model_json["intercept"])
|
| 68 |
+
return float(np.dot(coef, vec) + intercept)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# ============================================================
|
| 71 |
+
# LOAD 10 CATEGORY MODELS
|
| 72 |
# ============================================================
|
| 73 |
|
| 74 |
+
print("Loading category models…")
|
| 75 |
+
models = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
for key in CATEGORY_REPOS:
|
| 78 |
+
repo = CATEGORY_REPOS[key]
|
| 79 |
+
file = CATEGORY_FILES[key]
|
| 80 |
+
models[key] = load_model(repo, file)
|
| 81 |
|
| 82 |
+
print("✔ Category models loaded!")
|
| 83 |
|
| 84 |
# ============================================================
|
| 85 |
+
# API
|
| 86 |
# ============================================================
|
| 87 |
|
| 88 |
+
app = FastAPI(title="Category Classification API")
|
| 89 |
|
| 90 |
|
| 91 |
class InputText(BaseModel):
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
@app.get("/")
|
| 96 |
+
def home():
|
| 97 |
+
return {"status": "ok", "message": "Category API running"}
|
| 98 |
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
@app.post("/predict")
|
| 101 |
def predict(payload: InputText):
|
| 102 |
text = payload.text
|
| 103 |
vec = embed(text)
|
| 104 |
|
| 105 |
out = {}
|
| 106 |
+
for cat in CATEGORY_REPOS:
|
| 107 |
+
out[f"{cat}_score"] = linear_predict(models[cat], vec)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
return {
|
| 110 |
"input": text,
|
| 111 |
+
"categories": out
|
| 112 |
}
|