ahmed-7124 commited on
Commit
030af93
Β·
verified Β·
1 Parent(s): 05d848d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -42
app.py CHANGED
@@ -3,6 +3,8 @@ import tensorflow as tf
3
  import pdfplumber
4
  from transformers import pipeline
5
  import timm
 
 
6
 
7
  # Load pre-trained zero-shot model for text classification
8
  classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
@@ -25,6 +27,9 @@ disease_details = {
25
  "diabetes": {"medication": "Metformin or insulin", "precaution": "Monitor sugar levels", "doctor": "Endocrinologist"},
26
  }
27
 
 
 
 
28
  # Functions
29
  def register_patient(name, age, gender, password):
30
  patient_id = len(patients_db) + 1
@@ -62,55 +67,138 @@ def extract_pdf_report(pdf):
62
  text += page.extract_text()
63
  return text
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  # Gradio App Layout
66
  with gr.Blocks() as app:
67
  gr.Markdown("# Medico GPT")
68
 
69
- # Shared state for extracted text
70
- extracted_text_state = gr.State("")
71
-
72
  with gr.Tab("Patient Registration"):
73
- name = gr.Textbox(label="Patient Name")
74
- age = gr.Number(label="Age")
75
- gender = gr.Radio(label="Gender", choices=["Male", "Female", "Other"])
76
- password = gr.Textbox(label="Set Password", type="password")
77
- register_button = gr.Button("Register")
78
- register_output = gr.Textbox(label="Output")
79
-
80
- register_button.click(
81
- fn=register_patient,
82
- inputs=[name, age, gender, password],
83
- outputs=register_output
84
- )
85
 
86
  with gr.Tab("Extract PDF Report"):
87
- pdf_file = gr.File(label="Upload PDF")
88
- extract_button = gr.Button("Extract")
89
- extract_output = gr.Textbox(label="Extracted Text")
90
-
91
- extract_button.click(
92
- fn=extract_pdf_report,
93
- inputs=pdf_file,
94
- outputs=[extract_output, extracted_text_state]
95
- )
96
 
97
- with gr.Tab("Analyze Medical Report"):
98
- patient_id = gr.Number(label="Patient ID")
99
- report_text = gr.Textbox(label="Report Text")
100
- analyze_button = gr.Button("Analyze")
101
- analyze_output = gr.Textbox(label="Output")
102
-
103
- analyze_button.click(
104
- fn=analyze_report,
105
- inputs=[patient_id, report_text],
106
- outputs=analyze_output
107
- )
108
-
109
- # Autofill extracted text into Analyze Medical Report tab
110
- extract_button.click(
111
- fn=lambda extracted_text: extracted_text,
112
- inputs=extracted_text_state,
113
- outputs=report_text
114
- )
115
 
116
  app.launch(share=True)
 
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")
 
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
 
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)