Spaces:
Sleeping
Sleeping
File size: 3,001 Bytes
c0a6b07 2ebb489 13c4bb6 2ebb489 2895d5c 2ebb489 c0a6b07 2ebb489 2895d5c 13c4bb6 2ebb489 13c4bb6 2ebb489 c0a6b07 2895d5c 13c4bb6 a67fa1c 13c4bb6 a67fa1c c0a6b07 2ebb489 2895d5c 2ebb489 13c4bb6 2ebb489 13c4bb6 2895d5c 13c4bb6 2895d5c f2b95ae 2895d5c a67fa1c 2895d5c 13c4bb6 c0a6b07 a67fa1c 13c4bb6 2895d5c 2ebb489 2895d5c 2ebb489 2895d5c 13c4bb6 2895d5c 2ebb489 2895d5c 2ebb489 2895d5c 2ebb489 2895d5c 13c4bb6 2ebb489 2895d5c 2ebb489 c0a6b07 2ebb489 13c4bb6 c0a6b07 13c4bb6 2895d5c c0a6b07 2ebb489 2895d5c 2ebb489 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
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
# ============================================================
# CONFIG — ONLY CATEGORY MODELS
# ============================================================
HF_USER = "ClergeF"
CATEGORY_REPOS = {
"family": "family-model",
"community": "community-model",
"education": "education-model",
"health": "health-model",
"environment": "environment-model",
"business": "business-model",
"finance": "finance-model",
"history": "history-model",
"spirituality": "spirituality-model",
"innovation": "innovation-model",
}
CATEGORY_FILES = {
"family": "family_level.json",
"community": "community_level.json",
"education": "education_level.json",
"health": "health_level.json",
"environment": "environment_level.json",
"business": "business_level.json",
"finance": "finance_level.json",
"history": "history_level.json",
"spirituality": "spirituality_level.json",
"innovation": "innovation_level.json",
}
# ============================================================
# EMBEDDER
# ============================================================
print("Loading embedder: all-MiniLM-L6-v2 …")
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
# ============================================================
# HELPERS
# ============================================================
def embed(text: str):
return embedder.encode([text])[0]
def load_model(repo, filename):
print(f"Loading: {repo}/{filename}")
path = hf_hub_download(
repo_id=f"{HF_USER}/{repo}",
filename=filename
)
with open(path, "r") as f:
return json.load(f)
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)
# ============================================================
# LOAD 10 CATEGORY MODELS
# ============================================================
print("Loading category models…")
models = {}
for key in CATEGORY_REPOS:
repo = CATEGORY_REPOS[key]
file = CATEGORY_FILES[key]
models[key] = load_model(repo, file)
print("✔ Category models loaded!")
# ============================================================
# API
# ============================================================
app = FastAPI(title="Category Classification API")
class InputText(BaseModel):
text: str
@app.get("/")
def home():
return {"status": "ok", "message": "Category API running"}
@app.post("/predict")
def predict(payload: InputText):
text = payload.text
vec = embed(text)
out = {}
for cat in CATEGORY_REPOS:
out[f"{cat}_score"] = linear_predict(models[cat], vec)
return {
"input": text,
"categories": out
}
|