ahmed-7124 commited on
Commit
5650753
Β·
verified Β·
1 Parent(s): f9d840b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +204 -0
app.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import pdfplumber
4
+ from transformers import pipeline
5
+ import timm
6
+ import torch
7
+ import pandas as pd
8
+
9
+ # Load pre-trained zero-shot model for text classification
10
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
11
+
12
+ # Pre-trained ResNet50 model for X-ray or image analysis
13
+ image_model = timm.create_model('resnet50', pretrained=True)
14
+ image_model.eval()
15
+
16
+ # Load saved TensorFlow eye disease detection model
17
+ eye_model = tf.keras.models.load_model('model.h5')
18
+
19
+ # Patient database
20
+ patients_db = []
21
+
22
+ # Disease details for medical report analyzer
23
+ disease_details = {
24
+ "anemia": {"medication": "Iron supplements", "precaution": "Eat iron-rich foods", "doctor": "Hematologist"},
25
+ "viral infection": {"medication": "Antiviral drugs", "precaution": "Stay hydrated", "doctor": "Infectious Disease Specialist"},
26
+ "liver disease": {"medication": "Hepatoprotective drugs", "precaution": "Avoid alcohol", "doctor": "Hepatologist"},
27
+ "diabetes": {"medication": "Metformin or insulin", "precaution": "Monitor sugar levels", "doctor": "Endocrinologist"},
28
+ }
29
+
30
+ # Passwords
31
+ doctor_password = "doctor123"
32
+
33
+ # Functions
34
+ def register_patient(name, age, gender, password):
35
+ patient_id = len(patients_db) + 1
36
+ patients_db.append({
37
+ "ID": patient_id,
38
+ "Name": name,
39
+ "Age": age,
40
+ "Gender": gender,
41
+ "Password": password,
42
+ "Diagnosis": "",
43
+ "Medications": "",
44
+ "Precautions": "",
45
+ "Doctor": ""
46
+ })
47
+ return f"βœ… Patient {name} registered successfully. Patient ID: {patient_id}"
48
+
49
+ def analyze_report(patient_id, report_text):
50
+ candidate_labels = list(disease_details.keys())
51
+ result = classifier(report_text, candidate_labels)
52
+ diagnosis = result['labels'][0]
53
+
54
+ # Update patient's record
55
+ medication = disease_details[diagnosis]['medication']
56
+ precaution = disease_details[diagnosis]['precaution']
57
+ doctor = disease_details[diagnosis]['doctor']
58
+ for patient in patients_db:
59
+ if patient['ID'] == patient_id:
60
+ patient.update(Diagnosis=diagnosis, Medications=medication, Precautions=precaution, Doctor=doctor)
61
+ return f"πŸ” Diagnosis: {diagnosis}"
62
+
63
+ def extract_pdf_report(pdf):
64
+ text = ""
65
+ with pdfplumber.open(pdf.name) as pdf_file:
66
+ for page in pdf_file.pages:
67
+ text += page.extract_text()
68
+ return text
69
+
70
+ def predict_eye_disease(input_image):
71
+ input_image = tf.image.resize(input_image, [224, 224]) / 255.0
72
+ input_image = tf.expand_dims(input_image, 0)
73
+ predictions = eye_model.predict(input_image)
74
+ labels = ['Cataract', 'Conjunctivitis', 'Glaucoma', 'Normal']
75
+ confidence_scores = {labels[i]: round(predictions[0][i] * 100, 2) for i in range(len(labels))}
76
+ if confidence_scores['Normal'] > 50:
77
+ return f"Congrats! No disease detected. Confidence: {confidence_scores['Normal']}%"
78
+ return "\n".join([f"{label}: {confidence}%" for label, confidence in confidence_scores.items()])
79
+
80
+ def doctor_space(patient_id):
81
+ for patient in patients_db:
82
+ if patient["ID"] == patient_id:
83
+ return f"⚠ Precautions: {patient['Precautions']}\nπŸ‘©β€βš• Recommended Doctor: {patient['Doctor']}"
84
+ return "❌ Patient not found. Please check the ID."
85
+
86
+ def pharmacist_space(patient_id):
87
+ for patient in patients_db:
88
+ if patient["ID"] == patient_id:
89
+ return f"πŸ’Š Medications: {patient['Medications']}"
90
+ return "❌ Patient not found. Please check the ID."
91
+
92
+ def patient_dashboard(patient_id, password):
93
+ for patient in patients_db:
94
+ if patient["ID"] == patient_id and patient["Password"] == password:
95
+ return (f"🩺 Name: {patient['Name']}\n"
96
+ f"πŸ“‹ Diagnosis: {patient['Diagnosis']}\n"
97
+ f"πŸ’Š Medications: {patient['Medications']}\n"
98
+ f"⚠ Precautions: {patient['Precautions']}\n"
99
+ f"πŸ‘©β€βš• Recommended Doctor: {patient['Doctor']}")
100
+ return "❌ Access Denied: Invalid ID or Password."
101
+
102
+ def doctor_dashboard(password):
103
+ if password != doctor_password:
104
+ return "❌ Access Denied: Incorrect Password"
105
+ if not patients_db:
106
+ return "No patient records available."
107
+ details = []
108
+ for patient in patients_db:
109
+ details.append(f"🩺 Name: {patient['Name']}\n"
110
+ f"πŸ“‹ Diagnosis: {patient['Diagnosis']}\n"
111
+ f"πŸ’Š Medications: {patient['Medications']}\n"
112
+ f"⚠ Precautions: {patient['Precautions']}\n"
113
+ f"πŸ‘©β€βš• Recommended Doctor: {patient['Doctor']}")
114
+ return "\n\n".join(details)
115
+
116
+ # Gradio Interfaces
117
+ registration_interface = gr.Interface(
118
+ fn=register_patient,
119
+ inputs=[
120
+ gr.Textbox(label="Patient Name"),
121
+ gr.Number(label="Age"),
122
+ gr.Radio(label="Gender", choices=["Male", "Female", "Other"]),
123
+ gr.Textbox(label="Set Password", type="password"),
124
+ ],
125
+ outputs="text",
126
+ )
127
+
128
+ pdf_extraction_interface = gr.Interface(
129
+ fn=extract_pdf_report,
130
+ inputs=gr.File(label="Upload PDF Report"),
131
+ outputs="text",
132
+ )
133
+
134
+ report_analysis_interface = gr.Interface(
135
+ fn=analyze_report,
136
+ inputs=[
137
+ gr.Number(label="Patient ID"),
138
+ gr.Textbox(label="Report Text"),
139
+ ],
140
+ outputs="text",
141
+ )
142
+
143
+ eye_disease_interface = gr.Interface(
144
+ fn=predict_eye_disease,
145
+ inputs=gr.Image(label="Upload an Eye Image", type="numpy"),
146
+ outputs="text",
147
+ )
148
+
149
+ doctor_space_interface = gr.Interface(
150
+ fn=doctor_space,
151
+ inputs=gr.Number(label="Patient ID"),
152
+ outputs="text",
153
+ )
154
+
155
+ pharmacist_space_interface = gr.Interface(
156
+ fn=pharmacist_space,
157
+ inputs=gr.Number(label="Patient ID"),
158
+ outputs="text",
159
+ )
160
+
161
+ patient_dashboard_interface = gr.Interface(
162
+ fn=patient_dashboard,
163
+ inputs=[
164
+ gr.Number(label="Patient ID"),
165
+ gr.Textbox(label="Password", type="password"),
166
+ ],
167
+ outputs="text",
168
+ )
169
+
170
+ doctor_dashboard_interface = gr.Interface(
171
+ fn=doctor_dashboard,
172
+ inputs=gr.Textbox(label="Doctor Password", type="password"),
173
+ outputs="text",
174
+ )
175
+
176
+ # Gradio App Layout
177
+ with gr.Blocks() as app:
178
+ gr.Markdown("# Medico GPT")
179
+
180
+ with gr.Tab("Patient Registration"):
181
+ registration_interface.render()
182
+
183
+ with gr.Tab("Analyze Medical Report"):
184
+ report_analysis_interface.render()
185
+
186
+ with gr.Tab("Extract PDF Report"):
187
+ pdf_extraction_interface.render()
188
+
189
+ with gr.Tab("Ophthalmologist Space"):
190
+ eye_disease_interface.render()
191
+
192
+ with gr.Tab("Doctor Space"):
193
+ doctor_space_interface.render()
194
+
195
+ with gr.Tab("Pharmacist Space"):
196
+ pharmacist_space_interface.render()
197
+
198
+ with gr.Tab("Patient Dashboard"):
199
+ patient_dashboard_interface.render()
200
+
201
+ with gr.Tab("Doctor Dashboard"):
202
+ doctor_dashboard_interface.render()
203
+
204
+ app.launch(share=True)