Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,55 +5,38 @@ from pydantic import BaseModel
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
|
| 8 |
-
# ============================================================
|
| 9 |
-
# CONFIG
|
| 10 |
-
# ============================================================
|
| 11 |
-
|
| 12 |
HF_USER = "ClergeF"
|
| 13 |
IMPACT_REPO = "impact-model"
|
| 14 |
IMPACT_FILE = "impact.json"
|
| 15 |
|
| 16 |
-
# ============================================================
|
| 17 |
-
# EMBEDDER
|
| 18 |
-
# ============================================================
|
| 19 |
-
|
| 20 |
print("Loading embedder: all-MiniLM-L6-v2 …")
|
| 21 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 22 |
|
| 23 |
def embed(text: str):
|
| 24 |
return embedder.encode([text])[0]
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# ============================================================
|
| 29 |
-
|
| 30 |
-
def load_model(repo, filename):
|
| 31 |
-
print(f"Loading: {repo}/{filename}")
|
| 32 |
path = hf_hub_download(
|
| 33 |
-
repo_id=f"{HF_USER}/{
|
| 34 |
-
filename=
|
| 35 |
-
revision="main",
|
| 36 |
-
force_download=True
|
| 37 |
)
|
| 38 |
with open(path, "r") as f:
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
print("✔ Impact model loaded!")
|
| 44 |
|
| 45 |
-
|
| 46 |
-
# LINEAR PREDICT
|
| 47 |
-
# ============================================================
|
| 48 |
|
| 49 |
def linear_predict(model_json, vec):
|
| 50 |
coef = np.array(model_json["coef"])
|
| 51 |
intercept = np.array(model_json["intercept"])
|
| 52 |
return float(np.dot(coef, vec) + intercept)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
app = FastAPI(title="Impact Rating API")
|
| 59 |
|
|
@@ -69,19 +52,11 @@ def rate(payload: InputText):
|
|
| 69 |
text = payload.text
|
| 70 |
vec = embed(text)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
score = linear_predict(impact_model, vec)
|
| 74 |
-
result = {
|
| 75 |
-
"impact_score": score,
|
| 76 |
-
"matched_keyword": None
|
| 77 |
-
}
|
| 78 |
-
except Exception as e:
|
| 79 |
-
result = {
|
| 80 |
-
"impact_score": impact_model.get("default_score", 0),
|
| 81 |
-
"matched_keyword": None
|
| 82 |
-
}
|
| 83 |
|
| 84 |
return {
|
| 85 |
"input": text,
|
| 86 |
-
"result":
|
|
|
|
|
|
|
| 87 |
}
|
|
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
HF_USER = "ClergeF"
|
| 9 |
IMPACT_REPO = "impact-model"
|
| 10 |
IMPACT_FILE = "impact.json"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
print("Loading embedder: all-MiniLM-L6-v2 …")
|
| 13 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 14 |
|
| 15 |
def embed(text: str):
|
| 16 |
return embedder.encode([text])[0]
|
| 17 |
|
| 18 |
+
def load_model():
|
| 19 |
+
print(f"Loading {IMPACT_REPO}/{IMPACT_FILE}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
path = hf_hub_download(
|
| 21 |
+
repo_id=f"{HF_USER}/{IMPACT_REPO}",
|
| 22 |
+
filename=IMPACT_FILE
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
with open(path, "r") as f:
|
| 25 |
+
data = json.load(f)
|
| 26 |
|
| 27 |
+
# 🔥 REMOVE unwanted fields BEFORE storing the model
|
| 28 |
+
data.pop("matched_keyword", None)
|
|
|
|
| 29 |
|
| 30 |
+
return data
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def linear_predict(model_json, vec):
|
| 33 |
coef = np.array(model_json["coef"])
|
| 34 |
intercept = np.array(model_json["intercept"])
|
| 35 |
return float(np.dot(coef, vec) + intercept)
|
| 36 |
|
| 37 |
+
print("Loading impact model...")
|
| 38 |
+
impact_model = load_model()
|
| 39 |
+
print("✔ Impact model loaded!")
|
| 40 |
|
| 41 |
app = FastAPI(title="Impact Rating API")
|
| 42 |
|
|
|
|
| 52 |
text = payload.text
|
| 53 |
vec = embed(text)
|
| 54 |
|
| 55 |
+
score = linear_predict(impact_model, vec)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return {
|
| 58 |
"input": text,
|
| 59 |
+
"result": {
|
| 60 |
+
"impact_score": score # 🔥 cleaned output (NO matched_keyword)
|
| 61 |
+
}
|
| 62 |
}
|