File size: 796 Bytes
1cb88dd
 
 
 
 
 
 
 
9c80776
 
1cb88dd
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from sentence_transformers import SentenceTransformer, util
import torch
import joblib
import os

os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
# Load model components once
bundle = joblib.load("semantic_specialist_model.pkl")
local_model_path = "models/all-MiniLM-L6-v2"
model = SentenceTransformer(local_model_path)
known_embeddings = bundle["known_embeddings"]
symptom_specialist_pairs = bundle["symptom_specialist_pairs"]

def predict_specialist(symptom_text: str):
    input_embedding = model.encode(symptom_text, convert_to_tensor=True)
    similarities = util.pytorch_cos_sim(input_embedding, known_embeddings)[0]
    top_idx = similarities.argmax().item()
    specialist = symptom_specialist_pairs[top_idx][1]
    score = similarities[top_idx].item()
    return specialist, score