Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,87 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
app = FastAPI(
|
| 7 |
-
title="Impact Rating API",
|
| 8 |
-
description="Rates an action using your impact.json scoring rules",
|
| 9 |
-
version="1.0"
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
# ---------------------------------------------------------
|
| 13 |
-
# Load the JSON model from HuggingFace Hub
|
| 14 |
-
# ---------------------------------------------------------
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
repo_type="model"
|
| 25 |
-
)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
#
|
| 43 |
-
#
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
- If text contains a keyword → return score
|
| 50 |
-
- If none match → return neutral
|
| 51 |
-
"""
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
keywords = rule.get("keywords", [])
|
| 57 |
-
score = rule.get("score", 0)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
return {
|
| 62 |
-
"impact_score": score,
|
| 63 |
-
"matched_keyword": kw
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
# default fallback
|
| 67 |
-
return {
|
| 68 |
-
"impact_score": impact_model.get("default_score", 0),
|
| 69 |
-
"matched_keyword": None
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# ---------------------------------------------------------
|
| 74 |
-
# Endpoints
|
| 75 |
-
# ---------------------------------------------------------
|
| 76 |
|
| 77 |
@app.get("/")
|
| 78 |
def home():
|
| 79 |
-
return {
|
| 80 |
-
"message": "Impact Model API Running",
|
| 81 |
-
"model": REPO_ID,
|
| 82 |
-
"status": "ok"
|
| 83 |
-
}
|
| 84 |
|
| 85 |
@app.post("/rate")
|
| 86 |
-
def rate(
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return {
|
| 89 |
-
"input":
|
| 90 |
"result": result
|
| 91 |
}
|
|
|
|
| 1 |
+
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 |
+
# 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 |
+
# LOAD IMPACT MODEL
|
| 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}/{repo}",
|
| 34 |
+
filename=filename,
|
| 35 |
+
revision="main",
|
| 36 |
+
force_download=True
|
| 37 |
+
)
|
| 38 |
+
with open(path, "r") as f:
|
| 39 |
+
return json.load(f)
|
| 40 |
|
| 41 |
+
print("Loading impact model…")
|
| 42 |
+
impact_model = load_model(IMPACT_REPO, IMPACT_FILE)
|
| 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 |
+
# API
|
| 56 |
+
# ============================================================
|
| 57 |
|
| 58 |
+
app = FastAPI(title="Impact Rating API")
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
class InputText(BaseModel):
|
| 61 |
+
text: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
@app.get("/")
|
| 64 |
def home():
|
| 65 |
+
return {"status": "ok", "message": "Impact API running"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
@app.post("/rate")
|
| 68 |
+
def rate(payload: InputText):
|
| 69 |
+
text = payload.text
|
| 70 |
+
vec = embed(text)
|
| 71 |
+
|
| 72 |
+
try:
|
| 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": result
|
| 87 |
}
|