ClergeF commited on
Commit
5d10a46
·
verified ·
1 Parent(s): 645222d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -67
app.py CHANGED
@@ -1,91 +1,87 @@
 
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
  from huggingface_hub import hf_hub_download
4
- import json
5
-
6
- app = FastAPI(
7
- title="Impact Rating API",
8
- description="Rates an action using your impact.json scoring rules",
9
- version="1.0"
10
- )
11
-
12
- # ---------------------------------------------------------
13
- # Load the JSON model from HuggingFace Hub
14
- # ---------------------------------------------------------
15
 
16
- REPO_ID = "ClergeF/impact-model"
17
- FILENAME = "impact.json"
 
18
 
19
- print("Downloading impact.json from Hugging Face…")
 
 
20
 
21
- json_path = hf_hub_download(
22
- repo_id=REPO_ID,
23
- filename=FILENAME,
24
- repo_type="model"
25
- )
26
 
27
- with open(json_path, "r") as f:
28
- impact_model = json.load(f)
29
 
30
- print("Impact model loaded successfully!")
 
31
 
 
 
 
32
 
33
- # ---------------------------------------------------------
34
- # Request Body
35
- # ---------------------------------------------------------
36
-
37
- class Input(BaseModel):
38
- text: str
 
 
 
 
39
 
 
 
 
40
 
41
- # ---------------------------------------------------------
42
- # Rating Function
43
- # ---------------------------------------------------------
44
 
45
- def compute_impact(text: str):
46
- """
47
- Super simple rule engine:
48
- - Look through `impact_model`
49
- - If text contains a keyword → return score
50
- - If none match → return neutral
51
- """
52
 
53
- text_lower = text.lower()
 
 
54
 
55
- for rule in impact_model.get("rules", []):
56
- keywords = rule.get("keywords", [])
57
- score = rule.get("score", 0)
58
 
59
- for kw in keywords:
60
- if kw.lower() in text_lower:
61
- return {
62
- "impact_score": score,
63
- "matched_keyword": kw
64
- }
65
-
66
- # default fallback
67
- return {
68
- "impact_score": impact_model.get("default_score", 0),
69
- "matched_keyword": None
70
- }
71
-
72
-
73
- # ---------------------------------------------------------
74
- # Endpoints
75
- # ---------------------------------------------------------
76
 
77
  @app.get("/")
78
  def home():
79
- return {
80
- "message": "Impact Model API Running",
81
- "model": REPO_ID,
82
- "status": "ok"
83
- }
84
 
85
  @app.post("/rate")
86
- def rate(item: Input):
87
- result = compute_impact(item.text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  return {
89
- "input": item.text,
90
  "result": result
91
  }
 
1
+ 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
+ # CONFIG
10
+ # ============================================================
11
 
12
+ HF_USER = "ClergeF"
13
+ IMPACT_REPO = "impact-model"
14
+ IMPACT_FILE = "impact.json"
15
 
16
+ # ============================================================
17
+ # EMBEDDER
18
+ # ============================================================
 
 
19
 
20
+ print("Loading embedder: all-MiniLM-L6-v2 …")
21
+ embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
22
 
23
+ def embed(text: str):
24
+ return embedder.encode([text])[0]
25
 
26
+ # ============================================================
27
+ # LOAD IMPACT MODEL
28
+ # ============================================================
29
 
30
+ def load_model(repo, filename):
31
+ print(f"Loading: {repo}/{filename}")
32
+ path = hf_hub_download(
33
+ repo_id=f"{HF_USER}/{repo}",
34
+ filename=filename,
35
+ revision="main",
36
+ force_download=True
37
+ )
38
+ with open(path, "r") as f:
39
+ return json.load(f)
40
 
41
+ print("Loading impact model…")
42
+ impact_model = load_model(IMPACT_REPO, IMPACT_FILE)
43
+ print("✔ Impact model loaded!")
44
 
45
+ # ============================================================
46
+ # LINEAR PREDICT
47
+ # ============================================================
48
 
49
+ def linear_predict(model_json, vec):
50
+ coef = np.array(model_json["coef"])
51
+ intercept = np.array(model_json["intercept"])
52
+ return float(np.dot(coef, vec) + intercept)
 
 
 
53
 
54
+ # ============================================================
55
+ # API
56
+ # ============================================================
57
 
58
+ app = FastAPI(title="Impact Rating API")
 
 
59
 
60
+ class InputText(BaseModel):
61
+ text: str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  @app.get("/")
64
  def home():
65
+ return {"status": "ok", "message": "Impact API running"}
 
 
 
 
66
 
67
  @app.post("/rate")
68
+ def rate(payload: InputText):
69
+ text = payload.text
70
+ vec = embed(text)
71
+
72
+ try:
73
+ score = linear_predict(impact_model, vec)
74
+ result = {
75
+ "impact_score": score,
76
+ "matched_keyword": None
77
+ }
78
+ except Exception as e:
79
+ result = {
80
+ "impact_score": impact_model.get("default_score", 0),
81
+ "matched_keyword": None
82
+ }
83
+
84
  return {
85
+ "input": text,
86
  "result": result
87
  }