File size: 3,969 Bytes
9a243e1
 
 
 
 
98b4038
05e9d12
 
98b4038
05e9d12
9a243e1
98b4038
05e9d12
98b4038
 
 
 
 
05e9d12
 
 
 
98b4038
 
 
 
9a243e1
98b4038
 
 
 
9a243e1
 
 
 
05e9d12
 
98b4038
05e9d12
 
 
98b4038
05e9d12
 
 
 
 
 
 
 
 
98b4038
05e9d12
 
9a243e1
05e9d12
98b4038
05e9d12
 
 
 
 
98b4038
9a243e1
05e9d12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a243e1
05e9d12
 
 
 
98b4038
 
05e9d12
 
 
9a243e1
05e9d12
 
 
 
 
9a243e1
05e9d12
 
 
 
 
9a243e1
05e9d12
9a243e1
05e9d12
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
from transformers import pipeline

class HealthAssistant:
    def __init__(self):
        # Using an officially available medical model
        self.symptom_checker = pipeline(
            "text-classification", 
            model="emilyalsentzer/Bio_ClinicalBERT"
        )
        
        # Simplified mapping for demo purposes
        self.label_mapping = {
            "LABEL_0": "Respiratory Infection",
            "LABEL_1": "Gastrointestinal Issue",
            "LABEL_2": "Neurological Condition",
            "LABEL_3": "Musculoskeletal Problem",
            "LABEL_4": "General Viral Infection"
        }
        
        # Enhanced knowledge base
        self.disease_info = {
            "Respiratory Infection": {
                "symptoms": ["cough", "shortness of breath", "sore throat", "congestion"],
                "advice": "Rest, stay hydrated, use humidifier, monitor breathing",
                "precautions": ["Good hand hygiene", "Avoid smoking", "Get flu vaccine"]
            },
            "Gastrointestinal Issue": {
                "symptoms": ["nausea", "vomiting", "diarrhea", "stomach pain"],
                "advice": "Stay hydrated, BRAT diet, avoid dairy, monitor for dehydration",
                "precautions": ["Proper food handling", "Hand washing", "Avoid contaminated water"]
            }
        }
    
    def predict_disease(self, symptoms):
        try:
            # Get model prediction
            result = self.symptom_checker(symptoms[:512])  # Limit input length
            
            # Map label to readable name
            label = result[0]['label']
            disease_name = self.label_mapping.get(label, "Medical Condition")
            confidence = result[0]['score']
            
            # Get additional info if available
            info = self.disease_info.get(disease_name, {})
            
            response = f"Possible condition: {disease_name} (confidence: {confidence:.2f})\n\n"
            
            if info:
                response += f"Common symptoms:\n- " + "\n- ".join(info.get('symptoms', [])) + "\n\n"
                response += f"Recommended actions:\n{info.get('advice', 'Please consult a healthcare professional')}"
            
            return response
        
        except Exception as e:
            return f"System is currently analyzing your symptoms. For now, please consult a doctor about: {symptoms}"

def create_interface():
    assistant = HealthAssistant()
    
    with gr.Blocks(title="Medical Symptom Checker", theme=gr.themes.Soft()) as demo:
        gr.Markdown("# 🩺 AI Symptom Checker (Demo)")
        
        with gr.Row():
            with gr.Column():
                symptoms = gr.Textbox(
                    label="Describe your symptoms",
                    placeholder="E.g. headache, fever, cough...",
                    lines=3
                )
                analyze_btn = gr.Button("Analyze Symptoms")
            
            with gr.Column():
                output = gr.Textbox(
                    label="Analysis Result",
                    interactive=False,
                    lines=10
                )
        
        # Examples
        gr.Examples(
            examples=[
                ["headache, fever, body aches"],
                ["nausea, vomiting, diarrhea"],
                ["shortness of breath, cough"]
            ],
            inputs=symptoms
        )
        
        analyze_btn.click(
            fn=assistant.predict_disease,
            inputs=symptoms,
            outputs=output
        )
        
        gr.Markdown("""
        ## ⚠️ Important Disclaimer
        This AI tool provides general information only and is not a substitute for professional medical advice.
        Always consult with a qualified healthcare provider for diagnosis and treatment.
        """)
    
    return demo

if __name__ == "__main__":
    demo = create_interface()
    demo.launch()