ClergeF commited on
Commit
c0a6b07
·
verified ·
1 Parent(s): c44e7fa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import numpy as np
4
+ from fastapi import FastAPI
5
+ from pydantic import BaseModel
6
+ from sentence_transformers import SentenceTransformer
7
+ from huggingface_hub import hf_hub_download
8
+
9
+ # ============================================================
10
+ # CONFIG
11
+ # ============================================================
12
+
13
+ REPO_USER = "ClergeF"
14
+
15
+ MODEL_REPOS = {
16
+ "value_impact": "value-impact-model",
17
+ "impact": "impact-model",
18
+ "family": "family-model",
19
+ "community": "community-model",
20
+ "education": "education-model",
21
+ "health": "health-model",
22
+ "environment": "environment-model",
23
+ "business": "business-model",
24
+ "finance": "finance-model",
25
+ "history": "history-model",
26
+ "spirituality": "spirituality-model",
27
+ "innovation": "innovation-model",
28
+ }
29
+
30
+ # Embedder location in your HF repo:
31
+ EMBEDDER_REPO = "MVT-models"
32
+ EMBEDDER_SUBFOLDER = "universal_embedder"
33
+
34
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
35
+
36
+ # ============================================================
37
+ # LOAD UNIVERSAL EMBEDDER
38
+ # ============================================================
39
+
40
+ print("Loading universal embedder from HuggingFace Hub...")
41
+
42
+ embedder = SentenceTransformer(
43
+ f"{REPO_USER}/{EMBEDDER_REPO}",
44
+ subfolder=EMBEDDER_SUBFOLDER,
45
+ use_auth_token=HF_TOKEN
46
+ )
47
+
48
+ # ============================================================
49
+ # MODEL LOADING HELPER
50
+ # ============================================================
51
+
52
+ def load_model(repo_name, filename):
53
+ """Download & load .json linear regression model from HF Hub."""
54
+ model_path = hf_hub_download(
55
+ repo_id=f"{REPO_USER}/{repo_name}",
56
+ filename=filename,
57
+ token=HF_TOKEN,
58
+ )
59
+ with open(model_path, "r") as f:
60
+ return json.load(f)
61
+
62
+ print("Loading all 12 models from Hugging Face Hub...")
63
+
64
+ models = {
65
+ "value_impact": load_model(MODEL_REPOS["value_impact"], "value_impact.json"),
66
+ "impact": load_model(MODEL_REPOS["impact"], "impact.json"),
67
+ "family": load_model(MODEL_REPOS["family"], "family_level.json"),
68
+ "community": load_model(MODEL_REPOS["community"], "community_level.json"),
69
+ "education": load_model(MODEL_REPOS["education"], "education_level.json"),
70
+ "health": load_model(MODEL_REPOS["health"], "health_level.json"),
71
+ "environment": load_model(MODEL_REPOS["environment"], "environment_level.json"),
72
+ "business": load_model(MODEL_REPOS["business"], "business_level.json"),
73
+ "finance": load_model(MODEL_REPOS["finance"], "finance_level.json"),
74
+ "history": load_model(MODEL_REPOS["history"], "history_level.json"),
75
+ "spirituality": load_model(MODEL_REPOS["spirituality"], "spirituality_level.json"),
76
+ "innovation": load_model(MODEL_REPOS["innovation"], "innovation_level.json"),
77
+ }
78
+
79
+ # ============================================================
80
+ # FASTAPI
81
+ # ============================================================
82
+
83
+ app = FastAPI(title="MVT Community Value Model API")
84
+
85
+ class InputText(BaseModel):
86
+ text: str
87
+
88
+ # ============================================================
89
+ # PREDICTION HELPERS
90
+ # ============================================================
91
+
92
+ def embed(text: str):
93
+ return embedder.encode([text])[0]
94
+
95
+ def linear_predict(model_json, vec):
96
+ coef = np.array(model_json["coef"])
97
+ intercept = np.array(model_json["intercept"])
98
+
99
+ # Multi-output (value + impact)
100
+ if coef.ndim == 2:
101
+ return coef @ vec + intercept
102
+
103
+ # Single scalar output
104
+ return float(np.dot(coef, vec) + intercept)
105
+
106
+ # ============================================================
107
+ # API ROUTE
108
+ # ============================================================
109
+
110
+ @app.post("/predict")
111
+ def predict(payload: InputText):
112
+ text = payload.text
113
+ vec = embed(text)
114
+
115
+ result = {}
116
+
117
+ # Two-output regression model
118
+ value_pred, impact_pred = linear_predict(models["value_impact"], vec)
119
+ result["estimated_value"] = float(value_pred)
120
+ result["impact_level"] = float(impact_pred)
121
+
122
+ # Individual category models
123
+ for key in [
124
+ "impact", "family", "community", "education", "health",
125
+ "environment", "business", "finance", "history",
126
+ "spirituality", "innovation"
127
+ ]:
128
+ result[key] = float(linear_predict(models[key], vec))
129
+
130
+ return {
131
+ "input": text,
132
+ "predictions": result
133
+ }