ClergeF commited on
Commit
2ebb489
·
verified ·
1 Parent(s): 0d7ac4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -68
app.py CHANGED
@@ -2,92 +2,128 @@ import json
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",
21
- "family": "family_level.json",
22
- "community": "community_level.json",
23
- "education": "education_level.json",
24
- "health": "health_level.json",
25
  "environment": "environment_level.json",
26
- "business": "business_level.json",
27
- "finance": "finance_level.json",
28
- "history": "history_level.json",
29
- "spirituality": "spirituality_level.json",
30
- "innovation": "innovation_level.json",
31
  }
32
 
33
- # ===============================
34
- # LOAD EMBEDDER (NO RENAMING)
35
- # ===============================
36
- embedder = SentenceTransformer("ClergeF/MVT-embedder")
 
 
 
 
 
 
37
 
38
- # ===============================
39
- # LOAD ALL MODELS
40
- # ===============================
41
- def load_json_model(filename):
42
  path = hf_hub_download(
43
- repo_id=REPO_ID,
44
- filename=f"{MODEL_FOLDER}/{filename}"
45
  )
 
46
  with open(path, "r") as f:
47
- return json.load(f)
48
 
49
- models = {key: load_json_model(file) for key, file in MODEL_FILES.items()}
 
 
 
 
50
 
51
- # ===============================
52
- # FASTAPI SETUP
53
- # ===============================
54
- app = FastAPI(title="MVT Category + Impact API")
55
 
56
- class Input(BaseModel):
 
 
 
 
 
 
 
 
 
 
57
  text: str
58
 
59
- # ===============================
60
- # HELPERS
61
- # ===============================
62
- def embed(text):
63
- return embedder.encode([text])[0]
64
-
65
- def linear_predict(model, vec):
66
- coef = np.array(model["coef"])
67
- intercept = np.array(model["intercept"])
68
- if coef.ndim == 2:
69
- return coef @ vec + intercept
70
- return float(np.dot(coef, vec) + intercept)
71
-
72
- # ===============================
73
- # API ROUTE
74
- # ===============================
 
 
 
 
75
  @app.post("/predict")
76
- def predict_text(data: Input):
77
- text = data.text
78
- vec = embed(text)
79
 
80
- output = {}
81
 
82
- # Value + Impact (2-output regression)
83
- value, imp = linear_predict(models["value_impact"], vec)
84
- output["estimated_value"] = float(value)
85
- output["impact_level"] = float(imp)
86
 
87
- # All 10 category levels + single impact model
88
- for key in MODEL_FILES.keys():
89
- if key == "value_impact":
90
- continue
91
- output[key] = float(linear_predict(models[key], vec))
92
 
93
- return {"input": text, "predictions": output}
 
 
 
 
 
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
+ app = FastAPI()
10
+
11
+ # ============================================================
12
+ # Load Embedder (NO MORE CUSTOM REPO — USE BASE MODEL)
13
+ # ============================================================
14
+
15
+ print("Loading embedder: all-MiniLM-L6-v2 ...")
16
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
17
+
18
+ # ============================================================
19
+ # Model Registry — maps category → HF repo + file
20
+ # ============================================================
21
 
22
+ HF_USER = "ClergeF"
23
+
24
+ MODEL_REPOS = {
25
+ "value": "value-impact-model",
26
+ "impact": "impact-model",
27
+ "family": "family-model",
28
+ "community": "community-model",
29
+ "education": "education-model",
30
+ "health": "health-model",
31
+ "environment": "environment-model",
32
+ "business": "business-model",
33
+ "finance": "finance-model",
34
+ "history": "history-model",
35
+ "spirituality":"spirituality-model",
36
+ "innovation": "innovation-model"
37
+ }
38
 
 
 
 
39
  MODEL_FILES = {
40
+ "value": "value_impact.json",
41
+ "impact": "impact.json",
42
+ "family": "family_level.json",
43
+ "community": "community_level.json",
44
+ "education": "education_level.json",
45
+ "health": "health_level.json",
46
  "environment": "environment_level.json",
47
+ "business": "business_level.json",
48
+ "finance": "finance_level.json",
49
+ "history": "history_level.json",
50
+ "spirituality":"spirituality_level.json",
51
+ "innovation": "innovation_level.json"
52
  }
53
 
54
+ # ============================================================
55
+ # Load all category models into memory
56
+ # ============================================================
57
+
58
+ loaded_models = {}
59
+
60
+ def load_single_model(category: str):
61
+ """Download & load one model's JSON coefficients."""
62
+ repo = MODEL_REPOS[category]
63
+ file = MODEL_FILES[category]
64
 
 
 
 
 
65
  path = hf_hub_download(
66
+ repo_id=f"{HF_USER}/{repo}",
67
+ filename=file
68
  )
69
+
70
  with open(path, "r") as f:
71
+ data = json.load(f)
72
 
73
+ model = {
74
+ "weights": np.array(data["weights"]),
75
+ "bias": float(data["bias"])
76
+ }
77
+ return model
78
 
 
 
 
 
79
 
80
+ print("Loading all 12 models...")
81
+ for cat in MODEL_REPOS:
82
+ loaded_models[cat] = load_single_model(cat)
83
+ print("All models loaded successfully.")
84
+
85
+
86
+ # ============================================================
87
+ # Input schema
88
+ # ============================================================
89
+
90
+ class InputText(BaseModel):
91
  text: str
92
 
93
+
94
+ # ============================================================
95
+ # Predict function per model
96
+ # ============================================================
97
+
98
+ def predict_single(text: str, model_dict):
99
+ embedding = embedder.encode([text])[0] # vector
100
+ score = float(np.dot(embedding, model_dict["weights"]) + model_dict["bias"])
101
+ return max(0.0, min(1.0, score)) # clamp 0–1
102
+
103
+
104
+ # ============================================================
105
+ # API Routes
106
+ # ============================================================
107
+
108
+ @app.get("/")
109
+ def root():
110
+ return {"message": "MVT Category API is running."}
111
+
112
+
113
  @app.post("/predict")
114
+ def predict(payload: InputText):
115
+ text = payload.text
 
116
 
117
+ results = {}
118
 
119
+ for category, model in loaded_models.items():
120
+ results[category] = predict_single(text, model)
 
 
121
 
122
+ # Also return categories >= 0.85
123
+ high_cats = [c for c, s in results.items() if s >= 0.85]
 
 
 
124
 
125
+ return {
126
+ "input": text,
127
+ "scores": results,
128
+ "high_confidence_categories": high_cats
129
+ }