Spaces:
Sleeping
Sleeping
| import json | |
| import numpy as np | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from huggingface_hub import hf_hub_download | |
| from sentence_transformers import SentenceTransformer | |
| HF_USER = "ClergeF" | |
| IMPACT_REPO = "impact-model" | |
| IMPACT_FILE = "impact.json" | |
| print("Loading embedder: all-MiniLM-L6-v2 …") | |
| embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") | |
| def embed(text: str): | |
| return embedder.encode([text])[0] | |
| def load_model(): | |
| print(f"Loading {IMPACT_REPO}/{IMPACT_FILE}") | |
| path = hf_hub_download( | |
| repo_id=f"{HF_USER}/{IMPACT_REPO}", | |
| filename=IMPACT_FILE | |
| ) | |
| with open(path, "r") as f: | |
| data = json.load(f) | |
| # 🔥 REMOVE unwanted fields BEFORE storing the model | |
| data.pop("matched_keyword", None) | |
| return data | |
| def linear_predict(model_json, vec): | |
| coef = np.array(model_json["coef"]) | |
| intercept = np.array(model_json["intercept"]) | |
| return float(np.dot(coef, vec) + intercept) | |
| print("Loading impact model...") | |
| impact_model = load_model() | |
| print("✔ Impact model loaded!") | |
| app = FastAPI(title="Impact Rating API") | |
| class InputText(BaseModel): | |
| text: str | |
| def home(): | |
| return {"status": "ok", "message": "Impact API running"} | |
| def rate(payload: InputText): | |
| text = payload.text | |
| vec = embed(text) | |
| score = linear_predict(impact_model, vec) | |
| return { | |
| "input": text, | |
| "result": { | |
| "impact_score": score # 🔥 cleaned output (NO matched_keyword) | |
| } | |
| } | |