Upload interface.py
Browse files- interface.py +40 -0
interface.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# inference.py
|
| 2 |
+
import joblib
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# -----------------------------
|
| 7 |
+
# Load model + embedder at container startup
|
| 8 |
+
# -----------------------------
|
| 9 |
+
MODEL_FILENAME = "/Users/mazamessomeba/Desktop/Projects/Soulprint_snapshot/kinara-regression/Kinara_xgb_model.pkl" # change per repo
|
| 10 |
+
model = joblib.load(MODEL_FILENAME)
|
| 11 |
+
embedder = SentenceTransformer("all-mpnet-base-v2")
|
| 12 |
+
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# Hugging Face Inference API entrypoint
|
| 15 |
+
# -----------------------------
|
| 16 |
+
def predict(inputs: dict) -> dict:
|
| 17 |
+
"""
|
| 18 |
+
Expected input:
|
| 19 |
+
{
|
| 20 |
+
"text": "During a heated family argument, I stayed calm and reminded everyone of our values."
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
{
|
| 25 |
+
"score": 0.752
|
| 26 |
+
}
|
| 27 |
+
"""
|
| 28 |
+
if "text" not in inputs:
|
| 29 |
+
return {"error": "Missing required field 'text'"}
|
| 30 |
+
|
| 31 |
+
text = inputs["text"]
|
| 32 |
+
|
| 33 |
+
# Convert text to embedding
|
| 34 |
+
embedding = embedder.encode([text])
|
| 35 |
+
|
| 36 |
+
# Predict with model
|
| 37 |
+
score = model.predict(embedding)[0]
|
| 38 |
+
|
| 39 |
+
# Ensure it's a float for JSON serialization
|
| 40 |
+
return {"score": float(score)}
|