SumayyaMalik commited on
Commit
015f479
·
verified ·
1 Parent(s): 51b4703

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -47
app.py CHANGED
@@ -1,76 +1,57 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from sentence_transformers import SentenceTransformer, util
4
  import numpy as np
5
 
6
- # Load a freely available medical model from Hugging Face
7
- # We'll use a smaller model for faster inference in the Space
8
- MODEL_NAME = "d4data/biomedical-clinical-trials-bert"
9
  CLASSES = [
10
  "Cardiology", "Neurology", "Oncology", "Pediatrics",
11
  "Orthopedics", "Dermatology", "Gastroenterology",
12
  "Endocrinology", "Psychiatry", "Pulmonology"
13
  ]
14
 
15
- # Few-shot examples for each specialty
16
- EXAMPLES = {
17
- "Cardiology": [
18
- "chest pain", "shortness of breath", "palpitations",
19
- "high blood pressure", "irregular heartbeat"
20
- ],
21
- "Neurology": [
22
- "headache", "dizziness", "numbness in limbs",
23
- "seizures", "memory problems"
24
- ],
25
- "Oncology": [
26
- "unexplained weight loss", "persistent lumps",
27
- "unusual bleeding", "chronic fatigue", "skin changes"
28
- ],
29
- # Add more examples for other specialties...
30
- }
31
-
32
- # Initialize models
33
  classifier = pipeline(
34
  "text-classification",
35
- model=MODEL_NAME,
36
- tokenizer=MODEL_NAME
37
  )
38
- embedder = SentenceTransformer('all-MiniLM-L6-v2')
39
 
40
  def predict_specialty(symptoms):
41
  """
42
  Predict the most relevant medical specialty based on symptoms.
43
- Uses both classification and semantic similarity for robust predictions.
44
  """
45
  # Get classification prediction
46
  pred = classifier(symptoms)
47
  predicted_class = pred[0]['label']
48
 
49
- # Enhance with semantic similarity to example symptoms
50
- symptom_embedding = embedder.encode(symptoms, convert_to_tensor=True)
51
-
52
- similarities = {}
53
- for specialty, examples in EXAMPLES.items():
54
- example_embeddings = embedder.encode(examples, convert_to_tensor=True)
55
- similarity = util.pytorch_cos_sim(symptom_embedding, example_embeddings).mean().item()
56
- similarities[specialty] = similarity
 
57
 
58
- # Combine predictions (simple average for demo)
59
- combined_scores = {}
60
- for specialty in CLASSES:
61
- class_score = 1.0 if specialty == predicted_class else 0.0
62
- sim_score = similarities.get(specialty, 0.0)
63
- combined_scores[specialty] = (class_score + sim_score) / 2
64
 
65
- # Get top 3 predictions
66
- sorted_specialties = sorted(combined_scores.items(), key=lambda x: x[1], reverse=True)
67
- top_3 = sorted_specialties[:3]
 
 
 
 
 
68
 
69
- # Format output
70
  result = {
71
- "Primary Specialty": top_3[0][0],
72
- "Confidence": f"{top_3[0][1]*100:.1f}%",
73
- "Alternative Suggestions": [s[0] for s in top_3[1:]]
74
  }
75
 
76
  return result
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
  import numpy as np
4
 
5
+ # Use a simpler approach that doesn't require sentence-transformers
6
+ MODEL_NAME = "samrawal/bert-base-uncased_clinical-ner"
 
7
  CLASSES = [
8
  "Cardiology", "Neurology", "Oncology", "Pediatrics",
9
  "Orthopedics", "Dermatology", "Gastroenterology",
10
  "Endocrinology", "Psychiatry", "Pulmonology"
11
  ]
12
 
13
+ # Initialize model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  classifier = pipeline(
15
  "text-classification",
16
+ model="bhadresh-savani/bert-base-uncased-emotion",
17
+ tokenizer="bhadresh-savani/bert-base-uncased-emotion"
18
  )
 
19
 
20
  def predict_specialty(symptoms):
21
  """
22
  Predict the most relevant medical specialty based on symptoms.
23
+ Simplified version without sentence-transformers.
24
  """
25
  # Get classification prediction
26
  pred = classifier(symptoms)
27
  predicted_class = pred[0]['label']
28
 
29
+ # Simple mapping - in a real app you'd want more sophisticated logic
30
+ specialty_map = {
31
+ 'sadness': 'Psychiatry',
32
+ 'joy': 'Pediatrics', # Just example mapping
33
+ 'love': 'Cardiology',
34
+ 'anger': 'Neurology',
35
+ 'fear': 'Psychiatry',
36
+ 'surprise': 'Emergency Medicine'
37
+ }
38
 
39
+ primary = specialty_map.get(predicted_class, "General Practice")
40
+ confidence = f"{pred[0]['score']*100:.1f}%"
 
 
 
 
41
 
42
+ # Simple alternative suggestions
43
+ alternatives = []
44
+ if primary == "Psychiatry":
45
+ alternatives = ["Neurology", "Endocrinology"]
46
+ elif primary == "Cardiology":
47
+ alternatives = ["Pulmonology", "Gastroenterology"]
48
+ else:
49
+ alternatives = ["General Practice", "Internal Medicine"]
50
 
 
51
  result = {
52
+ "Primary Specialty": primary,
53
+ "Confidence": confidence,
54
+ "Alternative Suggestions": alternatives
55
  }
56
 
57
  return result