ClergeF commited on
Commit
f2b95ae
·
verified ·
1 Parent(s): d65390f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -107
app.py CHANGED
@@ -2,37 +2,19 @@ import json
2
  import numpy as np
3
  from fastapi import FastAPI
4
  from pydantic import BaseModel
5
- from sentence_transformers import SentenceTransformer
6
  from huggingface_hub import hf_hub_download
7
- import os
8
-
9
- # ============================================================
10
- # CONFIG — UPDATE ONLY IF YOU CHANGE REPO NAMES
11
- # ============================================================
12
-
13
- # Your username
14
- HF_USER = "ClergeF"
15
-
16
- # Your embedder repo
17
- EMBEDDER_REPO = "MVT-embedder"
18
 
19
- # Your 12 model repos
20
- MODEL_REPOS = {
21
- "value_impact": "value-impact-model",
22
- "impact": "impact-model",
23
- "family": "family-model",
24
- "community": "community-model",
25
- "education": "education-model",
26
- "health": "health-model",
27
- "environment": "environment-model",
28
- "business": "business-model",
29
- "finance": "finance-model",
30
- "history": "history-model",
31
- "spirituality": "spirituality-model",
32
- "innovation": "innovation-model",
33
- }
34
 
35
- # Files inside each repo
 
 
36
  MODEL_FILES = {
37
  "value_impact": "value_impact.json",
38
  "impact": "impact.json",
@@ -48,101 +30,67 @@ MODEL_FILES = {
48
  "innovation": "innovation_level.json",
49
  }
50
 
51
- # ============================================================
52
- # LOAD EMBEDDER (automatically works in Spaces)
53
- # ============================================================
54
-
55
- print("Loading SentenceTransformer embedder...")
56
- embedder = SentenceTransformer(f"{HF_USER}/{EMBEDDER_REPO}")
57
-
58
- # ============================================================
59
- # MODEL LOADING HELPERS
60
- # ============================================================
61
-
62
- def load_json_model(repo_name: str, filename: str):
63
- """Download and load model JSON from HuggingFace Hub."""
64
- print(f"Loading {repo_name}/{filename} ...")
65
-
66
  path = hf_hub_download(
67
- repo_id=f"{HF_USER}/{repo_name}",
68
- filename=filename
69
  )
70
-
71
  with open(path, "r") as f:
72
  return json.load(f)
73
 
74
- # ============================================================
75
- # LOAD ALL 12 MODELS ONCE
76
- # ============================================================
77
 
78
- print("Loading linear regression weight files...")
79
-
80
- models = {}
81
-
82
- for key, repo in MODEL_REPOS.items():
83
- models[key] = load_json_model(repo, MODEL_FILES[key])
84
-
85
- print("All models successfully loaded!")
86
-
87
- # ============================================================
88
  # FASTAPI SETUP
89
- # ============================================================
90
-
91
- app = FastAPI(title="MVT Category Scoring API")
92
 
93
- class InputText(BaseModel):
94
  text: str
95
 
96
- # ============================================================
97
- # PREDICTION HELPERS
98
- # ============================================================
99
-
100
- def embed(text: str):
101
- """Convert text → embedding."""
102
  return embedder.encode([text])[0]
103
 
104
- def linear_predict(model_json, vec):
105
- """Apply manual linear regression: dot(coef, x) + intercept."""
106
- coef = np.array(model_json["coef"])
107
- intercept = model_json["intercept"]
108
-
109
- if isinstance(intercept, list):
110
- intercept = np.array(intercept)
111
-
112
- pred = np.dot(coef, vec) + intercept
113
- return float(pred)
114
-
115
- # ============================================================
116
- # MAIN API ROUTE
117
- # ============================================================
118
 
 
 
 
119
  @app.post("/predict")
120
- def predict(payload: InputText):
121
- text = payload.text
122
  vec = embed(text)
123
 
124
- result = {}
125
-
126
- # --- Value + Impact model (2 outputs) ---
127
- value_impact_model = models["value_impact"]
128
- coef = np.array(value_impact_model["coef"])
129
- intercept = np.array(value_impact_model["intercept"])
130
-
131
- value_pred, impact_pred = coef @ vec + intercept
132
- result["estimated_value"] = float(value_pred)
133
- result["impact_level"] = float(impact_pred)
134
 
135
- # --- 10 category models ---
136
- for name in [
137
- "family", "community", "education", "health", "environment",
138
- "business", "finance", "history", "spirituality", "innovation"
139
- ]:
140
- result[f"{name}_score"] = linear_predict(models[name], vec)
141
 
142
- # --- Standalone impact model ---
143
- result["impact_model_score"] = linear_predict(models["impact"], vec)
 
 
 
144
 
145
- return {
146
- "input": text,
147
- "predictions": result
148
- }
 
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
+ # REPO + FOLDERS (REAL STRUCTURE)
10
+ # ===============================
11
+ REPO_ID = "ClergeF/MVT-models"
12
+ EMBEDDER_FOLDER = "universal_embedder"
13
+ MODEL_FOLDER = "models"
 
 
 
 
 
 
 
 
 
14
 
15
+ # ===============================
16
+ # JSON FILES FOR YOUR 12 MODELS
17
+ # ===============================
18
  MODEL_FILES = {
19
  "value_impact": "value_impact.json",
20
  "impact": "impact.json",
 
30
  "innovation": "innovation_level.json",
31
  }
32
 
33
+ # ===============================
34
+ # LOAD EMBEDDER (NO RENAMING)
35
+ # ===============================
36
+ embedder = SentenceTransformer(
37
+ REPO_ID,
38
+ subfolder=EMBEDDER_FOLDER
39
+ )
40
+
41
+ # ===============================
42
+ # LOAD ALL MODELS
43
+ # ===============================
44
+ def load_json_model(filename):
 
 
 
45
  path = hf_hub_download(
46
+ repo_id=REPO_ID,
47
+ filename=f"{MODEL_FOLDER}/{filename}"
48
  )
 
49
  with open(path, "r") as f:
50
  return json.load(f)
51
 
52
+ models = {key: load_json_model(file) for key, file in MODEL_FILES.items()}
 
 
53
 
54
+ # ===============================
 
 
 
 
 
 
 
 
 
55
  # FASTAPI SETUP
56
+ # ===============================
57
+ app = FastAPI(title="MVT Category + Impact API")
 
58
 
59
+ class Input(BaseModel):
60
  text: str
61
 
62
+ # ===============================
63
+ # HELPERS
64
+ # ===============================
65
+ def embed(text):
 
 
66
  return embedder.encode([text])[0]
67
 
68
+ def linear_predict(model, vec):
69
+ coef = np.array(model["coef"])
70
+ intercept = np.array(model["intercept"])
71
+ if coef.ndim == 2:
72
+ return coef @ vec + intercept
73
+ return float(np.dot(coef, vec) + intercept)
 
 
 
 
 
 
 
 
74
 
75
+ # ===============================
76
+ # API ROUTE
77
+ # ===============================
78
  @app.post("/predict")
79
+ def predict_text(data: Input):
80
+ text = data.text
81
  vec = embed(text)
82
 
83
+ output = {}
 
 
 
 
 
 
 
 
 
84
 
85
+ # Value + Impact (2-output regression)
86
+ value, imp = linear_predict(models["value_impact"], vec)
87
+ output["estimated_value"] = float(value)
88
+ output["impact_level"] = float(imp)
 
 
89
 
90
+ # All 10 category levels + single impact model
91
+ for key in MODEL_FILES.keys():
92
+ if key == "value_impact":
93
+ continue
94
+ output[key] = float(linear_predict(models[key], vec))
95
 
96
+ return {"input": text, "predictions": output}