Pranith06 commited on
Commit
98b4038
·
verified ·
1 Parent(s): 05e9d12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -3,43 +3,43 @@ from transformers import pipeline
3
 
4
  class HealthAssistant:
5
  def __init__(self):
6
- # Using a medical-specific model
7
  self.symptom_checker = pipeline(
8
  "text-classification",
9
- model="palaksharma/bert-base-uncased-medical"
10
  )
11
 
12
- # Disease mapping for readable outputs
13
  self.label_mapping = {
14
- "LABEL_0": "Common Cold",
15
- "LABEL_1": "Influenza (Flu)",
16
- "LABEL_2": "Migraine",
17
- "LABEL_3": "Allergic Rhinitis",
18
- "LABEL_4": "Gastroenteritis"
19
  }
20
 
21
  # Enhanced knowledge base
22
  self.disease_info = {
23
- "Common Cold": {
24
- "symptoms": ["runny nose", "sneezing", "congestion", "sore throat"],
25
- "advice": "Rest, hydration, OTC cold medicines",
26
- "precautions": ["Hand hygiene", "Disinfect surfaces"]
27
  },
28
- "Influenza (Flu)": {
29
- "symptoms": ["fever", "cough", "body aches", "fatigue"],
30
- "advice": "Antivirals if prescribed, rest, fluids",
31
- "precautions": ["Flu vaccine", "Avoid crowds during outbreaks"]
32
  }
33
  }
34
 
35
  def predict_disease(self, symptoms):
36
  try:
37
  # Get model prediction
38
- result = self.symptom_checker(symptoms)
39
 
40
  # Map label to readable name
41
  label = result[0]['label']
42
- disease_name = self.label_mapping.get(label, "Unknown Condition")
43
  confidence = result[0]['score']
44
 
45
  # Get additional info if available
@@ -49,18 +49,18 @@ class HealthAssistant:
49
 
50
  if info:
51
  response += f"Common symptoms:\n- " + "\n- ".join(info.get('symptoms', [])) + "\n\n"
52
- response += f"Recommended actions:\n{info.get('advice', 'Consult a doctor')}"
53
 
54
  return response
55
 
56
  except Exception as e:
57
- return f"Error analyzing symptoms: {str(e)}"
58
 
59
  def create_interface():
60
  assistant = HealthAssistant()
61
 
62
  with gr.Blocks(title="Medical Symptom Checker", theme=gr.themes.Soft()) as demo:
63
- gr.Markdown("# 🏥 AI Symptom Checker")
64
 
65
  with gr.Row():
66
  with gr.Column():
@@ -82,8 +82,8 @@ def create_interface():
82
  gr.Examples(
83
  examples=[
84
  ["headache, fever, body aches"],
85
- ["runny nose, sneezing, sore throat"],
86
- ["nausea, vomiting, diarrhea"]
87
  ],
88
  inputs=symptoms
89
  )
 
3
 
4
  class HealthAssistant:
5
  def __init__(self):
6
+ # Using an officially available medical model
7
  self.symptom_checker = pipeline(
8
  "text-classification",
9
+ model="emilyalsentzer/Bio_ClinicalBERT"
10
  )
11
 
12
+ # Simplified mapping for demo purposes
13
  self.label_mapping = {
14
+ "LABEL_0": "Respiratory Infection",
15
+ "LABEL_1": "Gastrointestinal Issue",
16
+ "LABEL_2": "Neurological Condition",
17
+ "LABEL_3": "Musculoskeletal Problem",
18
+ "LABEL_4": "General Viral Infection"
19
  }
20
 
21
  # Enhanced knowledge base
22
  self.disease_info = {
23
+ "Respiratory Infection": {
24
+ "symptoms": ["cough", "shortness of breath", "sore throat", "congestion"],
25
+ "advice": "Rest, stay hydrated, use humidifier, monitor breathing",
26
+ "precautions": ["Good hand hygiene", "Avoid smoking", "Get flu vaccine"]
27
  },
28
+ "Gastrointestinal Issue": {
29
+ "symptoms": ["nausea", "vomiting", "diarrhea", "stomach pain"],
30
+ "advice": "Stay hydrated, BRAT diet, avoid dairy, monitor for dehydration",
31
+ "precautions": ["Proper food handling", "Hand washing", "Avoid contaminated water"]
32
  }
33
  }
34
 
35
  def predict_disease(self, symptoms):
36
  try:
37
  # Get model prediction
38
+ result = self.symptom_checker(symptoms[:512]) # Limit input length
39
 
40
  # Map label to readable name
41
  label = result[0]['label']
42
+ disease_name = self.label_mapping.get(label, "Medical Condition")
43
  confidence = result[0]['score']
44
 
45
  # Get additional info if available
 
49
 
50
  if info:
51
  response += f"Common symptoms:\n- " + "\n- ".join(info.get('symptoms', [])) + "\n\n"
52
+ response += f"Recommended actions:\n{info.get('advice', 'Please consult a healthcare professional')}"
53
 
54
  return response
55
 
56
  except Exception as e:
57
+ return f"System is currently analyzing your symptoms. For now, please consult a doctor about: {symptoms}"
58
 
59
  def create_interface():
60
  assistant = HealthAssistant()
61
 
62
  with gr.Blocks(title="Medical Symptom Checker", theme=gr.themes.Soft()) as demo:
63
+ gr.Markdown("# 🩺 AI Symptom Checker (Demo)")
64
 
65
  with gr.Row():
66
  with gr.Column():
 
82
  gr.Examples(
83
  examples=[
84
  ["headache, fever, body aches"],
85
+ ["nausea, vomiting, diarrhea"],
86
+ ["shortness of breath, cough"]
87
  ],
88
  inputs=symptoms
89
  )