ClergeF commited on
Commit
a67fa1c
·
verified ·
1 Parent(s): d92a275

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -58
app.py CHANGED
@@ -1,17 +1,22 @@
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",
@@ -27,60 +32,63 @@ MODEL_REPOS = {
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
@@ -90,21 +98,22 @@ class InputText(BaseModel):
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")
@@ -114,20 +123,26 @@ def predict(payload: InputText):
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
- }
 
 
1
  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",
 
32
  "innovation": "innovation-model",
33
  }
34
 
35
+ # Files inside each repo
36
+ MODEL_FILES = {
37
+ "value_impact": "value_impact.json",
38
+ "impact": "impact.json",
39
+ "family": "family_level.json",
40
+ "community": "community_level.json",
41
+ "education": "education_level.json",
42
+ "health": "health_level.json",
43
+ "environment": "environment_level.json",
44
+ "business": "business_level.json",
45
+ "finance": "finance_level.json",
46
+ "history": "history_level.json",
47
+ "spirituality": "spirituality_level.json",
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
 
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")
 
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
+ }