Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,12 +36,12 @@ doctor_password = "doctor123"
|
|
36 |
# Loading the custom model for consultation with the doctor
|
37 |
try:
|
38 |
# Force using the slow tokenizer for compatibility
|
39 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
40 |
except Exception as e:
|
41 |
print(f"Tokenizer error: {e}")
|
42 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
43 |
|
44 |
-
model = AutoModelForCausalLM.from_pretrained("
|
45 |
|
46 |
def consult_doctor(prompt):
|
47 |
inputs = tokenizer(prompt, return_tensors="pt")
|
@@ -66,7 +66,16 @@ def register_patient(name, age, gender, password):
|
|
66 |
})
|
67 |
return f"β
Patient {name} registered successfully. Patient ID: {patient_id}"
|
68 |
|
69 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
candidate_labels = list(disease_details.keys())
|
71 |
result = classifier(report_text, candidate_labels)
|
72 |
diagnosis = result['labels'][0]
|
@@ -78,14 +87,15 @@ def analyze_report(patient_id, report_text):
|
|
78 |
for patient in patients_db:
|
79 |
if patient['ID'] == patient_id:
|
80 |
patient.update(Diagnosis=diagnosis, Medications=medication, Precautions=precaution, Doctor=doctor)
|
81 |
-
return f"π Diagnosis: {diagnosis}"
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
89 |
|
90 |
# def predict_eye_disease(input_image):
|
91 |
# input_image = tf.image.resize(input_image, [224, 224]) / 255.0
|
@@ -151,11 +161,22 @@ pdf_extraction_interface = gr.Interface(
|
|
151 |
outputs="text",
|
152 |
)
|
153 |
|
154 |
-
report_analysis_interface = gr.Interface(
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
gr.Number(label="Patient ID"),
|
158 |
-
gr.
|
|
|
159 |
],
|
160 |
outputs="text",
|
161 |
)
|
@@ -206,8 +227,10 @@ with gr.Blocks() as app:
|
|
206 |
with gr.Tab("Patient Registration"):
|
207 |
registration_interface.render()
|
208 |
|
209 |
-
with gr.Tab("Analyze Medical Report"):
|
210 |
-
|
|
|
|
|
211 |
|
212 |
with gr.Tab("Extract PDF Report"):
|
213 |
pdf_extraction_interface.render()
|
|
|
36 |
# Loading the custom model for consultation with the doctor
|
37 |
try:
|
38 |
# Force using the slow tokenizer for compatibility
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained("ahmed-7124/NeuraMedAW", use_fast=False)
|
40 |
except Exception as e:
|
41 |
print(f"Tokenizer error: {e}")
|
42 |
+
tokenizer = AutoTokenizer.from_pretrained("ahmed-7124/NeuraMedAW", use_fast=False)
|
43 |
|
44 |
+
model = AutoModelForCausalLM.from_pretrained("ahmed-7124/NeuraMedAW")
|
45 |
|
46 |
def consult_doctor(prompt):
|
47 |
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
66 |
})
|
67 |
return f"β
Patient {name} registered successfully. Patient ID: {patient_id}"
|
68 |
|
69 |
+
def analyze_or_extract_report(patient_id, pdf=None, report_text=None):
|
70 |
+
if pdf:
|
71 |
+
# Extract text from PDF
|
72 |
+
with pdfplumber.open(pdf.name) as pdf_file:
|
73 |
+
report_text = "".join([page.extract_text() for page in pdf_file.pages])
|
74 |
+
|
75 |
+
if not report_text:
|
76 |
+
return "β Please provide a report text or upload a PDF."
|
77 |
+
|
78 |
+
# Analyze the report
|
79 |
candidate_labels = list(disease_details.keys())
|
80 |
result = classifier(report_text, candidate_labels)
|
81 |
diagnosis = result['labels'][0]
|
|
|
87 |
for patient in patients_db:
|
88 |
if patient['ID'] == patient_id:
|
89 |
patient.update(Diagnosis=diagnosis, Medications=medication, Precautions=precaution, Doctor=doctor)
|
90 |
+
return f"π Diagnosis: {diagnosis}\nπ Medication: {medication}\nβ Precaution: {precaution}\nπ©ββ Recommended Doctor: {doctor}"
|
91 |
|
92 |
+
|
93 |
+
# def extract_pdf_report(pdf):
|
94 |
+
# text = ""
|
95 |
+
# with pdfplumber.open(pdf.name) as pdf_file:
|
96 |
+
# for page in pdf_file.pages:
|
97 |
+
# text += page.extract_text()
|
98 |
+
# return text
|
99 |
|
100 |
# def predict_eye_disease(input_image):
|
101 |
# input_image = tf.image.resize(input_image, [224, 224]) / 255.0
|
|
|
161 |
outputs="text",
|
162 |
)
|
163 |
|
164 |
+
# report_analysis_interface = gr.Interface(
|
165 |
+
# fn=analyze_report,
|
166 |
+
# inputs=[
|
167 |
+
# gr.Number(label="Patient ID"),
|
168 |
+
# gr.Textbox(label="Report Text"),
|
169 |
+
# ],
|
170 |
+
# outputs="text",
|
171 |
+
# )
|
172 |
+
|
173 |
+
# Unified Gradio Interface
|
174 |
+
analyze_report_interface = gr.Interface(
|
175 |
+
fn=analyze_or_extract_report,
|
176 |
+
inputs=[
|
177 |
gr.Number(label="Patient ID"),
|
178 |
+
gr.File(label="Upload PDF Report", optional=True),
|
179 |
+
gr.Textbox(label="Report Text (Optional)", optional=True),
|
180 |
],
|
181 |
outputs="text",
|
182 |
)
|
|
|
227 |
with gr.Tab("Patient Registration"):
|
228 |
registration_interface.render()
|
229 |
|
230 |
+
# with gr.Tab("Analyze Medical Report"):
|
231 |
+
# report_analysis_interface.render()
|
232 |
+
with gr.Tab("Analyze Medical Report"):
|
233 |
+
analyze_report_interface.render()
|
234 |
|
235 |
with gr.Tab("Extract PDF Report"):
|
236 |
pdf_extraction_interface.render()
|