Spaces:
Runtime error
Runtime error
Update utils/specialist_predictor.py
Browse files- utils/specialist_predictor.py +18 -17
utils/specialist_predictor.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
| 1 |
-
from sentence_transformers import SentenceTransformer, util
|
| 2 |
-
import torch
|
| 3 |
-
import joblib
|
| 4 |
-
|
| 5 |
-
# Load model components once
|
| 6 |
-
bundle = joblib.load("semantic_specialist_model.pkl")
|
| 7 |
-
model = SentenceTransformer(
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer, util
|
| 2 |
+
import torch
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
# Load model components once
|
| 6 |
+
bundle = joblib.load("semantic_specialist_model.pkl")
|
| 7 |
+
model = SentenceTransformer("./models/all-MiniLM-L6-v2")
|
| 8 |
+
|
| 9 |
+
known_embeddings = bundle["known_embeddings"]
|
| 10 |
+
symptom_specialist_pairs = bundle["symptom_specialist_pairs"]
|
| 11 |
+
|
| 12 |
+
def predict_specialist(symptom_text: str):
|
| 13 |
+
input_embedding = model.encode(symptom_text, convert_to_tensor=True)
|
| 14 |
+
similarities = util.pytorch_cos_sim(input_embedding, known_embeddings)[0]
|
| 15 |
+
top_idx = similarities.argmax().item()
|
| 16 |
+
specialist = symptom_specialist_pairs[top_idx][1]
|
| 17 |
+
score = similarities[top_idx].item()
|
| 18 |
+
return specialist, score
|