ahmed-7124 commited on
Commit
8e73ae2
Β·
verified Β·
1 Parent(s): e8a12b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -17
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("harishussain12/PastelMed", use_fast=False)
40
  except Exception as e:
41
  print(f"Tokenizer error: {e}")
42
- tokenizer = AutoTokenizer.from_pretrained("harishussain12/PastelMed", use_fast=False)
43
 
44
- model = AutoModelForCausalLM.from_pretrained("harishussain12/PastelMed")
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 analyze_report(patient_id, report_text):
 
 
 
 
 
 
 
 
 
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
- def extract_pdf_report(pdf):
84
- text = ""
85
- with pdfplumber.open(pdf.name) as pdf_file:
86
- for page in pdf_file.pages:
87
- text += page.extract_text()
88
- return text
 
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
- fn=analyze_report,
156
- inputs=[
 
 
 
 
 
 
 
 
 
 
157
  gr.Number(label="Patient ID"),
158
- gr.Textbox(label="Report Text"),
 
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
- report_analysis_interface.render()
 
 
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()