Update app.py
Browse files
app.py
CHANGED
|
@@ -3,43 +3,43 @@ from transformers import pipeline
|
|
| 3 |
|
| 4 |
class HealthAssistant:
|
| 5 |
def __init__(self):
|
| 6 |
-
# Using
|
| 7 |
self.symptom_checker = pipeline(
|
| 8 |
"text-classification",
|
| 9 |
-
model="
|
| 10 |
)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
self.label_mapping = {
|
| 14 |
-
"LABEL_0": "
|
| 15 |
-
"LABEL_1": "
|
| 16 |
-
"LABEL_2": "
|
| 17 |
-
"LABEL_3": "
|
| 18 |
-
"LABEL_4": "
|
| 19 |
}
|
| 20 |
|
| 21 |
# Enhanced knowledge base
|
| 22 |
self.disease_info = {
|
| 23 |
-
"
|
| 24 |
-
"symptoms": ["
|
| 25 |
-
"advice": "Rest,
|
| 26 |
-
"precautions": ["
|
| 27 |
},
|
| 28 |
-
"
|
| 29 |
-
"symptoms": ["
|
| 30 |
-
"advice": "
|
| 31 |
-
"precautions": ["
|
| 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, "
|
| 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', '
|
| 53 |
|
| 54 |
return response
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
-
return f"
|
| 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("#
|
| 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 |
-
["
|
| 86 |
-
["
|
| 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 |
)
|