matesoft commited on
Commit
0eb63a5
Β·
verified Β·
1 Parent(s): e4da22c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -103
app.py CHANGED
@@ -1,138 +1,113 @@
1
  import gradio as gr
 
2
 
3
- # Mock data for demonstration
4
- MOCK_SYMPTOMS = [
5
- "Headache", "Fever", "Cough", "Fatigue",
6
- "Nausea", "Dizziness", "Sore Throat", "Shortness of Breath"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  ]
8
 
9
- MOCK_KNOWLEDGE = {
10
- "headache": "Try resting in a quiet, dark room and drink plenty of water.",
11
- "fever": "Stay hydrated and consider taking fever-reducing medication.",
12
- "cough": "Drink warm liquids and consider cough drops.",
13
- "general": "For serious symptoms, please consult a healthcare professional."
 
 
 
 
14
  }
15
 
16
- # --- Mock Prediction Function ---
17
  def predict_disease(selected_labels):
18
  if not selected_labels or len(selected_labels) < 4:
19
  return "⚠️ Please select at least 4 symptoms for accurate results."
20
 
21
- # Mock prediction results
22
  return f"""
23
- <div style='background: #001a33; padding: 15px; border-radius: 8px; color: white;'>
24
- <h3 style='color: #4fc3f7; margin-top: 0;'>🩺 Predicted Condition</h3>
25
- <p>Based on your symptoms, this appears to be a <b>common viral infection</b>.</p>
26
- <p>Confidence: 85% (mock data)</p>
27
-
28
- <h3 style='color: #4fc3f7;'>πŸ” Recommended Actions</h3>
29
- <ul>
30
- <li>Get plenty of rest</li>
31
- <li>Stay hydrated</li>
32
- <li>Monitor your symptoms</li>
33
- <li>Consult a doctor if symptoms worsen</li>
34
  </ul>
35
  </div>
36
  """
37
 
38
- # --- Mock Chat Responder ---
39
  def chatbot_respond(message, chat_history):
40
- message_lower = message.lower()
41
- response = MOCK_KNOWLEDGE.get("general")
42
-
43
- for condition, advice in MOCK_KNOWLEDGE.items():
44
- if condition in message_lower:
45
  response = advice
46
  break
47
-
48
  return chat_history + [(message, response)], ""
49
 
50
- # --- UI Setup ---
51
- custom_css = """
52
- :root {
53
- --primary: #4fc3f7;
54
- --secondary: #001a33;
55
- --text: #ffffff;
56
- --bg: #0a192f;
57
- --card-bg: #0a2342;
58
- --error: #ff6b6b;
59
- }
60
- body, .gradio-container {
61
- background: var(--bg) !important;
62
- color: var(--text) !important;
63
- font-family: 'Segoe UI', Roboto, sans-serif;
64
- }
65
- .gr-button {
66
- background: var(--primary) !important;
67
- color: var(--secondary) !important;
68
- border: none !important;
69
- }
70
- .gr-button:hover {
71
- opacity: 0.9 !important;
72
- }
73
- .gr-checkbox {
74
- background: var(--card-bg) !important;
75
- border-color: var(--primary) !important;
76
- }
77
- .gr-checkbox label {
78
- color: var(--text) !important;
79
- }
80
- """
81
-
82
- with gr.Blocks(css=custom_css, title="Medical Diagnosis Demo") as demo:
83
  gr.Markdown("""
84
- <div style="text-align: center; margin-bottom: 20px;">
85
- <h1 style="margin-bottom: 5px; color: #4fc3f7;">🧬 Medical Diagnosis Demo</h1>
86
- <p style="color: #4fc3f7; font-size: 16px;">UI demonstration without backend functionality</p>
87
  </div>
88
  """)
89
 
90
- with gr.Row(equal_height=True):
91
- with gr.Column(scale=1, min_width=300):
92
- gr.Markdown("### πŸ” Symptom Checker (Demo)")
93
  symptoms_input = gr.CheckboxGroup(
94
- choices=MOCK_SYMPTOMS,
95
- label="Select symptoms:",
96
  interactive=True
97
  )
98
  predict_btn = gr.Button("Analyze Symptoms", variant="primary")
99
  prediction_output = gr.HTML(
100
- label="Results will show mock data",
101
- value="<div style='padding: 20px; background: #001a33; border-radius: 8px; color: white;'>Select symptoms and click analyze to see mock results</div>"
102
  )
103
 
104
- with gr.Column(scale=1, min_width=400):
105
- gr.Markdown("### πŸ’¬ Medical Advisor (Demo)")
106
- chatbot = gr.Chatbot(
107
- label="Chat with Medical Advisor",
108
- show_label=False,
109
- bubble_full_width=False
110
- )
111
  with gr.Row():
112
- user_input = gr.Textbox(
113
- placeholder="Try: 'headache' or 'fever'",
114
- label="",
115
- show_label=False,
116
- container=False,
117
- scale=7
118
- )
119
- send_btn = gr.Button("Send", scale=1, min_width=80)
120
 
121
- # Event handlers
122
- predict_btn.click(
123
- fn=predict_disease,
124
- inputs=symptoms_input,
125
- outputs=prediction_output
126
- )
127
- send_btn.click(
128
- fn=chatbot_respond,
129
- inputs=[user_input, chatbot],
130
- outputs=[chatbot, user_input]
131
- )
132
- user_input.submit(
133
- fn=chatbot_respond,
134
- inputs=[user_input, chatbot],
135
- outputs=[chatbot, user_input]
136
- )
137
 
138
  demo.launch()
 
1
  import gradio as gr
2
+ import numpy as np
3
 
4
+ # Your original symptom list (copied from your code)
5
+ all_symptoms = [
6
+ 'abdominal_pain', 'abnormal_menstruation', 'acidity', 'acute_liver_failure',
7
+ 'altered_sensorium', 'anxiety', 'back_pain', 'belly_pain', 'blackheads',
8
+ 'bladder_discomfort', 'blister', 'blood_in_sputum', 'bloody_stool',
9
+ 'blurred_and_distorted_vision', 'breathlessness', 'brittle_nails',
10
+ 'bruising', 'burning_micturition', 'chest_pain', 'chills', 'cold_hands_and_feets',
11
+ 'coma', 'congestion', 'constipation', 'continuous_feel_of_urine',
12
+ 'continuous_sneezing', 'cough', 'cramps', 'dark_urine', 'dehydration',
13
+ 'depression', 'diarrhoea', 'dischromic _patches', 'distention_of_abdomen',
14
+ 'dizziness', 'drying_and_tingling_lips', 'enlarged_thyroid', 'excessive_hunger',
15
+ 'extra_marital_contacts', 'family_history', 'fast_heart_rate', 'fatigue',
16
+ 'fluid_overload', 'fluid_overload.1', 'foul_smell_of urine', 'headache',
17
+ 'high_fever', 'hip_joint_pain', 'history_of_alcohol_consumption', 'increased_appetite',
18
+ 'indigestion', 'inflammatory_nails', 'internal_itching', 'irregular_sugar_level',
19
+ 'irritability', 'irritation_in_anus', 'itching', 'joint_pain', 'knee_pain',
20
+ 'lack_of_concentration', 'lethargy', 'loss_of_appetite', 'loss_of_balance',
21
+ 'loss_of_smell', 'malaise', 'mild_fever', 'mood_swings', 'movement_stiffness',
22
+ 'mucoid_sputum', 'muscle_pain', 'muscle_wasting', 'muscle_weakness',
23
+ 'nausea', 'neck_pain', 'nodal_skin_eruptions', 'obesity', 'pain_behind_the_eyes',
24
+ 'pain_during_bowel_movements', 'pain_in_anal_region', 'painful_walking',
25
+ 'palpitations', 'passage_of_gases', 'patches_in_throat', 'phlegm',
26
+ 'polyuria', 'prominent_veins_on_calf', 'puffy_face_and_eyes', 'pus_filled_pimples',
27
+ 'receiving_blood_transfusion', 'receiving_unsterile_injections', 'red_sore_around_nose',
28
+ 'red_spots_over_body', 'redness_of_eyes', 'restlessness', 'runny_nose',
29
+ 'rusty_sputum', 'scurring', 'shivering', 'silver_like_dusting', 'sinus_pressure',
30
+ 'skin_peeling', 'skin_rash', 'slurred_speech', 'small_dents_in_nails',
31
+ 'spinning_movements', 'spotting_ urination', 'stiff_neck', 'stomach_bleeding',
32
+ 'stomach_pain', 'sunken_eyes', 'sweating', 'swelled_lymph_nodes',
33
+ 'swelling_joints', 'swelling_of_stomach', 'swollen_blood_vessels',
34
+ 'swollen_extremeties', 'swollen_legs', 'throat_irritation', 'toxic_look_(typhos)',
35
+ 'ulcers_on_tongue', 'unsteadiness', 'visual_disturbances', 'vomiting',
36
+ 'watering_from_eyes', 'weakness_in_limbs', 'weakness_of_one_body_side',
37
+ 'weight_gain', 'weight_loss', 'yellow_crust_ooze', 'yellow_urine',
38
+ 'yellowish_skin', 'yellowing_of_eyes'
39
  ]
40
 
41
+ # Convert to display format (your original formatting)
42
+ display_symptoms = [symptom.replace('_', ' ').title() for symptom in all_symptoms]
43
+
44
+ # Medical knowledge base (simplified but with your structure)
45
+ MEDICAL_KNOWLEDGE = {
46
+ "fungal infection": "Antifungal creams or oral medications may be needed",
47
+ "allergy": "Antihistamines can help relieve symptoms",
48
+ "cold": "Rest and fluids are recommended",
49
+ # Add more conditions as needed
50
  }
51
 
 
52
  def predict_disease(selected_labels):
53
  if not selected_labels or len(selected_labels) < 4:
54
  return "⚠️ Please select at least 4 symptoms for accurate results."
55
 
56
+ # Mock prediction with your symptom list
57
  return f"""
58
+ <div style='background: #001a33; padding: 15px; border-radius: 8px; margin-bottom: 15px;'>
59
+ <h3 style='color: #4fc3f7; margin-top: 0;'>🩺 Predicted Disease</h3>
60
+ <p style='font-size: 18px; color: white;'>Common Cold <span style='color: #4fc3f7'>(85% confidence)</span></p>
61
+ </div>
62
+ <div style='background: #001a33; padding: 15px; border-radius: 8px;'>
63
+ <h3 style='color: #4fc3f7; margin-top: 0;'>πŸ” Top 3 Possible Conditions</h3>
64
+ <ul style='color: white; padding-left: 20px;'>
65
+ <li><b>1. Common Cold</b> β€” 85%</li>
66
+ <li><b>2. Allergic Reaction</b> β€” 65%</li>
67
+ <li><b>3. Viral Infection</b> β€” 45%</li>
 
68
  </ul>
69
  </div>
70
  """
71
 
 
72
  def chatbot_respond(message, chat_history):
73
+ response = "This is a demo response. In a real app, this would provide medical advice."
74
+ for condition, advice in MEDICAL_KNOWLEDGE.items():
75
+ if condition in message.lower():
 
 
76
  response = advice
77
  break
 
78
  return chat_history + [(message, response)], ""
79
 
80
+ # Your original UI with all symptoms
81
+ with gr.Blocks(title="Medical Diagnosis Assistant") as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  gr.Markdown("""
83
+ <div style='text-align: center; margin-bottom: 20px;'>
84
+ <h1 style='margin-bottom: 5px; color: #4fc3f7;'>🧬 Medical Diagnosis Assistant</h1>
85
+ <p style='color: #4fc3f7; font-size: 16px;'>Select symptoms for diagnosis</p>
86
  </div>
87
  """)
88
 
89
+ with gr.Row():
90
+ with gr.Column():
91
+ gr.Markdown("### πŸ” Symptom Checker")
92
  symptoms_input = gr.CheckboxGroup(
93
+ choices=display_symptoms,
94
+ label="Select your symptoms:",
95
  interactive=True
96
  )
97
  predict_btn = gr.Button("Analyze Symptoms", variant="primary")
98
  prediction_output = gr.HTML(
99
+ value="<div style='padding: 20px; background: #001a33; border-radius: 8px; color: white;'>Your results will appear here</div>"
 
100
  )
101
 
102
+ with gr.Column():
103
+ gr.Markdown("### πŸ’¬ Medical Advisor")
104
+ chatbot = gr.Chatbot(bubble_full_width=False)
 
 
 
 
105
  with gr.Row():
106
+ user_input = gr.Textbox(placeholder="Ask about symptoms...")
107
+ send_btn = gr.Button("Send")
 
 
 
 
 
 
108
 
109
+ predict_btn.click(predict_disease, symptoms_input, prediction_output)
110
+ send_btn.click(chatbot_respond, [user_input, chatbot], [chatbot, user_input])
111
+ user_input.submit(chatbot_respond, [user_input, chatbot], [chatbot, user_input])
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  demo.launch()