Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,92 +2,128 @@ import json
|
|
| 2 |
import numpy as np
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# ===============================
|
| 16 |
-
# JSON FILES FOR YOUR 12 MODELS
|
| 17 |
-
# ===============================
|
| 18 |
MODEL_FILES = {
|
| 19 |
-
"
|
| 20 |
-
"impact":
|
| 21 |
-
"family":
|
| 22 |
-
"community":
|
| 23 |
-
"education":
|
| 24 |
-
"health":
|
| 25 |
"environment": "environment_level.json",
|
| 26 |
-
"business":
|
| 27 |
-
"finance":
|
| 28 |
-
"history":
|
| 29 |
-
"spirituality":
|
| 30 |
-
"innovation":
|
| 31 |
}
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
-
#
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# ===============================
|
| 39 |
-
# LOAD ALL MODELS
|
| 40 |
-
# ===============================
|
| 41 |
-
def load_json_model(filename):
|
| 42 |
path = hf_hub_download(
|
| 43 |
-
repo_id=
|
| 44 |
-
filename=
|
| 45 |
)
|
|
|
|
| 46 |
with open(path, "r") as f:
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# ===============================
|
| 52 |
-
# FASTAPI SETUP
|
| 53 |
-
# ===============================
|
| 54 |
-
app = FastAPI(title="MVT Category + Impact API")
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
text: str
|
| 58 |
|
| 59 |
-
|
| 60 |
-
#
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@app.post("/predict")
|
| 76 |
-
def
|
| 77 |
-
text =
|
| 78 |
-
vec = embed(text)
|
| 79 |
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
output["estimated_value"] = float(value)
|
| 85 |
-
output["impact_level"] = float(imp)
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
for
|
| 89 |
-
if key == "value_impact":
|
| 90 |
-
continue
|
| 91 |
-
output[key] = float(linear_predict(models[key], vec))
|
| 92 |
|
| 93 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# ============================================================
|
| 12 |
+
# Load Embedder (NO MORE CUSTOM REPO — USE BASE MODEL)
|
| 13 |
+
# ============================================================
|
| 14 |
+
|
| 15 |
+
print("Loading embedder: all-MiniLM-L6-v2 ...")
|
| 16 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 17 |
+
|
| 18 |
+
# ============================================================
|
| 19 |
+
# Model Registry — maps category → HF repo + file
|
| 20 |
+
# ============================================================
|
| 21 |
|
| 22 |
+
HF_USER = "ClergeF"
|
| 23 |
+
|
| 24 |
+
MODEL_REPOS = {
|
| 25 |
+
"value": "value-impact-model",
|
| 26 |
+
"impact": "impact-model",
|
| 27 |
+
"family": "family-model",
|
| 28 |
+
"community": "community-model",
|
| 29 |
+
"education": "education-model",
|
| 30 |
+
"health": "health-model",
|
| 31 |
+
"environment": "environment-model",
|
| 32 |
+
"business": "business-model",
|
| 33 |
+
"finance": "finance-model",
|
| 34 |
+
"history": "history-model",
|
| 35 |
+
"spirituality":"spirituality-model",
|
| 36 |
+
"innovation": "innovation-model"
|
| 37 |
+
}
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
MODEL_FILES = {
|
| 40 |
+
"value": "value_impact.json",
|
| 41 |
+
"impact": "impact.json",
|
| 42 |
+
"family": "family_level.json",
|
| 43 |
+
"community": "community_level.json",
|
| 44 |
+
"education": "education_level.json",
|
| 45 |
+
"health": "health_level.json",
|
| 46 |
"environment": "environment_level.json",
|
| 47 |
+
"business": "business_level.json",
|
| 48 |
+
"finance": "finance_level.json",
|
| 49 |
+
"history": "history_level.json",
|
| 50 |
+
"spirituality":"spirituality_level.json",
|
| 51 |
+
"innovation": "innovation_level.json"
|
| 52 |
}
|
| 53 |
|
| 54 |
+
# ============================================================
|
| 55 |
+
# Load all category models into memory
|
| 56 |
+
# ============================================================
|
| 57 |
+
|
| 58 |
+
loaded_models = {}
|
| 59 |
+
|
| 60 |
+
def load_single_model(category: str):
|
| 61 |
+
"""Download & load one model's JSON coefficients."""
|
| 62 |
+
repo = MODEL_REPOS[category]
|
| 63 |
+
file = MODEL_FILES[category]
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
path = hf_hub_download(
|
| 66 |
+
repo_id=f"{HF_USER}/{repo}",
|
| 67 |
+
filename=file
|
| 68 |
)
|
| 69 |
+
|
| 70 |
with open(path, "r") as f:
|
| 71 |
+
data = json.load(f)
|
| 72 |
|
| 73 |
+
model = {
|
| 74 |
+
"weights": np.array(data["weights"]),
|
| 75 |
+
"bias": float(data["bias"])
|
| 76 |
+
}
|
| 77 |
+
return model
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
print("Loading all 12 models...")
|
| 81 |
+
for cat in MODEL_REPOS:
|
| 82 |
+
loaded_models[cat] = load_single_model(cat)
|
| 83 |
+
print("All models loaded successfully.")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# ============================================================
|
| 87 |
+
# Input schema
|
| 88 |
+
# ============================================================
|
| 89 |
+
|
| 90 |
+
class InputText(BaseModel):
|
| 91 |
text: str
|
| 92 |
|
| 93 |
+
|
| 94 |
+
# ============================================================
|
| 95 |
+
# Predict function per model
|
| 96 |
+
# ============================================================
|
| 97 |
+
|
| 98 |
+
def predict_single(text: str, model_dict):
|
| 99 |
+
embedding = embedder.encode([text])[0] # vector
|
| 100 |
+
score = float(np.dot(embedding, model_dict["weights"]) + model_dict["bias"])
|
| 101 |
+
return max(0.0, min(1.0, score)) # clamp 0–1
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# ============================================================
|
| 105 |
+
# API Routes
|
| 106 |
+
# ============================================================
|
| 107 |
+
|
| 108 |
+
@app.get("/")
|
| 109 |
+
def root():
|
| 110 |
+
return {"message": "MVT Category API is running."}
|
| 111 |
+
|
| 112 |
+
|
| 113 |
@app.post("/predict")
|
| 114 |
+
def predict(payload: InputText):
|
| 115 |
+
text = payload.text
|
|
|
|
| 116 |
|
| 117 |
+
results = {}
|
| 118 |
|
| 119 |
+
for category, model in loaded_models.items():
|
| 120 |
+
results[category] = predict_single(text, model)
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
# Also return categories >= 0.85
|
| 123 |
+
high_cats = [c for c, s in results.items() if s >= 0.85]
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
return {
|
| 126 |
+
"input": text,
|
| 127 |
+
"scores": results,
|
| 128 |
+
"high_confidence_categories": high_cats
|
| 129 |
+
}
|