Spaces:
Sleeping
Sleeping
File size: 13,856 Bytes
8df396a afd3d9f 0f8851c afd3d9f 030af93 8df396a 4f6f95c 8df396a 4f6f95c afd3d9f ca891f7 e8a12b5 afd3d9f 8df396a 9791b6d ef08ed2 030af93 4f6f95c e218653 4f6f95c 8e73ae2 e218653 8e73ae2 e218653 8e73ae2 1d09b2b b804667 1d09b2b 4f6f95c f2b1e48 8df396a f2b1e48 8df396a 1f303f9 8df396a 1d09b2b 8df396a 8e73ae2 8df396a 1f303f9 8df396a 1f303f9 8e73ae2 8df396a 8e73ae2 09a4c0c e8a12b5 030af93 1d09b2b 030af93 1d09b2b 030af93 1d09b2b 030af93 1d09b2b 030af93 1d09b2b 030af93 0e1fd0a 9c593fe 0e1fd0a b4f0dee 0e1fd0a 8e73ae2 663e901 e43ac0f 663e901 e43ac0f 8faae00 f393d97 f02b1d8 f393d97 acf00ee f393d97 77e2743 f393d97 8faae00 8e73ae2 0e1fd0a 9711794 0e1fd0a e8a12b5 0e1fd0a 9791b6d 0e1fd0a 8faae00 124f9a0 0e1fd0a 9c593fe 0e1fd0a 124f9a0 0e1fd0a 124f9a0 1d09b2b 0f8851c 8df396a 205a28b 1d09b2b 205a28b 030af93 1d09b2b 8e73ae2 1c59abf 8e73ae2 8faae00 1d09b2b 52a0cf1 1d09b2b 030af93 1d09b2b 030af93 9791b6d 030af93 1d09b2b 030af93 9791b6d 0f8851c 9c593fe 0f8851c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
import gradio as gr
import tensorflow as tf
import pdfplumber
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
import timm
import torch
import pandas as pd
# Load pre-trained zero-shot model for text classification (using PyTorch for compatibility)
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli", framework="pt")
# Pre-trained ResNet50 model for X-ray or image analysis using Timm
image_model = timm.create_model('resnet50', pretrained=True)
image_model.eval()
from tensorflow import keras
from tensorflow.keras.layers import TFSMLayer
# Load the model as a layer (in the SavedModel format)
#eye_model = TFSMLayer('model.h5')
# Patient database
patients_db = []
# Disease details for medical report analyzer
disease_details = {
"anemia": {
"medication": (
"Iron supplements (e.g., ferrous sulfate), "
"Vitamin B12 injections (for pernicious anemia), "
"Folic acid supplements."
),
"precaution": (
"Consume iron-rich foods like spinach, red meat, and lentils. "
"Pair iron-rich foods with vitamin C to enhance absorption. "
"Avoid tea or coffee with meals as they inhibit iron absorption."
),
"doctor": "Hematologist",
},
"liver disease": {
"medication": (
"Hepatoprotective drugs (e.g., ursodeoxycholic acid, silymarin). "
"Antiviral therapy for viral hepatitis. "
"Diuretics for managing fluid retention (e.g., spironolactone)."
),
"precaution": (
"Avoid alcohol and hepatotoxic drugs. "
"Follow a low-fat diet and avoid processed foods. "
"Regularly monitor liver function tests."
),
"doctor": "Hepatologist",
},
"diabetes": {
"medication": (
"Oral hypoglycemics (e.g., metformin). "
"Insulin therapy for Type 1 diabetes or advanced Type 2 diabetes. "
"GLP-1 receptor agonists (e.g., liraglutide) for improving blood sugar control."
),
"precaution": (
"Monitor blood glucose levels daily. "
"Follow a low-carb, high-fiber diet. "
"Engage in regular physical activity. "
"Avoid sugary foods and beverages."
),
"doctor": "Endocrinologist",
},
"hypertension": {
"medication": (
"ACE inhibitors (e.g., lisinopril). "
"Beta-blockers (e.g., metoprolol). "
"Calcium channel blockers (e.g., amlodipine). "
"Diuretics (e.g., hydrochlorothiazide)."
),
"precaution": (
"Reduce salt intake to less than 2g per day. "
"Engage in at least 150 minutes of moderate exercise weekly. "
"Avoid smoking and excessive alcohol consumption. "
"Manage stress through relaxation techniques like yoga or meditation."
),
"doctor": "Cardiologist",
},
"pneumonia": {
"medication": (
"Antibiotics (e.g., amoxicillin or azithromycin for bacterial pneumonia). "
"Antiviral therapy if caused by viruses like influenza. "
"Supplemental oxygen in severe cases."
),
"precaution": (
"Get plenty of rest and stay hydrated. "
"Use a humidifier to ease breathing. "
"Avoid smoking or exposure to pollutants. "
"Ensure vaccination against influenza and pneumococcus."
),
"doctor": "Pulmonologist",
},
"kidney disease": {
"medication": (
"ACE inhibitors or ARBs (e.g., losartan) for controlling blood pressure. "
"Erythropoietin-stimulating agents for anemia management. "
"Phosphate binders (e.g., sevelamer) to manage high phosphate levels."
),
"precaution": (
"Limit salt, potassium, and phosphorus in the diet. "
"Stay hydrated but avoid overhydration. "
"Avoid NSAIDs and other nephrotoxic drugs. "
"Monitor kidney function and blood pressure regularly."
),
"doctor": "Nephrologist",
},
"depression": {
"medication": (
"Selective serotonin reuptake inhibitors (SSRIs, e.g., sertraline). "
"Serotonin-norepinephrine reuptake inhibitors (SNRIs, e.g., venlafaxine). "
"Tricyclic antidepressants (e.g., amitriptyline) in specific cases."
),
"precaution": (
"Engage in regular physical exercise. "
"Maintain a routine and avoid isolation. "
"Consider therapy (e.g., CBT or psychotherapy). "
"Avoid alcohol and recreational drugs."
),
"doctor": "Psychiatrist",},
}
# Passwords
doctor_password = "doctor123"
# Loading the custom model for consultation with the doctor
try:
# Force using the slow tokenizer for compatibility
tokenizer = AutoTokenizer.from_pretrained("ahmed-7124/NeuraMedAW", use_fast=False)
except Exception as e:
print(f"Tokenizer error: {e}")
tokenizer = AutoTokenizer.from_pretrained("ahmed-7124/NeuraMedAW", use_fast=False)
model = AutoModelForCausalLM.from_pretrained("ahmed-7124/NeuraMedAW")
def consult_doctor(prompt):
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=100)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
# Functions for the app
def register_patient(name, age, gender, password):
patient_id = len(patients_db) + 1
patients_db.append({
"ID": patient_id,
"Name": name,
"Age": age,
"Gender": gender,
"Password": password,
"Diagnosis": "",
"Medications": "",
"Precautions": "",
"Doctor": ""
})
return f"β
Patient {name} registered successfully. Patient ID: {patient_id}"
def analyze_or_extract_report(patient_id, pdf=None, report_text=None):
if pdf:
# Extract text from PDF
with pdfplumber.open(pdf.name) as pdf_file:
report_text = "".join([page.extract_text() for page in pdf_file.pages])
if not report_text:
return "β Please provide a report text or upload a PDF."
# Analyze the report
candidate_labels = list(disease_details.keys())
result = classifier(report_text, candidate_labels)
diagnosis = result['labels'][0]
# Update patient's record
medication = disease_details[diagnosis]['medication']
precaution = disease_details[diagnosis]['precaution']
doctor = disease_details[diagnosis]['doctor']
for patient in patients_db:
if patient['ID'] == patient_id:
patient.update(Diagnosis=diagnosis, Medications=medication, Precautions=precaution, Doctor=doctor)
return f"π Diagnosis: {diagnosis}\nπ Medication: {medication}\nβ Precaution: {precaution}\nπ©ββ Recommended Doctor: {doctor}"
# def extract_pdf_report(pdf):
# text = ""
# with pdfplumber.open(pdf.name) as pdf_file:
# for page in pdf_file.pages:
# text += page.extract_text()
# return text
#
# def predict_eye_disease(input_image):
# input_image = tf.image.resize(input_image, [224, 224]) / 255.0
# input_image = tf.expand_dims(input_image, 0)
# predictions = eye_model(input_image)
# labels = ['Cataract', 'Conjunctivitis', 'Glaucoma', 'Normal']
# confidence_scores = {labels[i]: round(predictions[i] * 100, 2) for i in range(len(labels))}
# if confidence_scores['Normal'] > 50:
# return f"Congrats! No disease detected. Confidence: {confidence_scores['Normal']}%"
# return "\n".join([f"{label}: {confidence}%" for label, confidence in confidence_scores.items()])
def doctor_space(patient_id):
for patient in patients_db:
if patient["ID"] == patient_id:
return f"β Precautions: {patient['Precautions']}\nπ©ββ Recommended Doctor: {patient['Doctor']}"
return "β Patient not found. Please check the ID."
def pharmacist_space(patient_id):
for patient in patients_db:
if patient["ID"] == patient_id:
return f"π Medications: {patient['Medications']}"
return "β Patient not found. Please check the ID."
def patient_dashboard(patient_id, password):
for patient in patients_db:
if patient["ID"] == patient_id and patient["Password"] == password:
return (f"π©Ί Name: {patient['Name']}\n"
f"π Diagnosis: {patient['Diagnosis']}\n"
f"π Medications: {patient['Medications']}\n"
f"β Precautions: {patient['Precautions']}\n"
f"π©ββ Recommended Doctor: {patient['Doctor']}")
return "β Access Denied: Invalid ID or Password."
def doctor_dashboard(password):
if password != doctor_password:
return "β Access Denied: Incorrect Password"
if not patients_db:
return "No patient records available."
details = []
for patient in patients_db:
details.append(f"π©Ί Name: {patient['Name']}\n"
f"π Diagnosis: {patient['Diagnosis']}\n"
f"π Medications: {patient['Medications']}\n"
f"β Precautions: {patient['Precautions']}\n"
f"π©ββ Recommended Doctor: {patient['Doctor']}")
return "\n\n".join(details)
# Gradio Interfaces
registration_interface = gr.Interface(
fn=register_patient,
inputs=[
gr.Textbox(label="Patient Name"),
gr.Number(label="Age"),
gr.Radio(label="Gender", choices=["Male", "Female", "Other"]),
gr.Textbox(label="Set Password", type="password"),
],
outputs="text",
)
#pdf_extraction_interface = gr.Interface(
# fn=extract_pdf_report,
# inputs=gr.File(label="Upload PDF Report"),
# outputs="text",
#)
# report_analysis_interface = gr.Interface(
# fn=analyze_report,
# inputs=[
# gr.Number(label="Patient ID"),
# gr.Textbox(label="Report Text"),
# ],
# outputs="text",
# )
from transformers import AutoTokenizer, AutoModelForCausalLM
try:
tokenizer = AutoTokenizer.from_pretrained("ahmed-7124/dgptAW")
model = AutoModelForCausalLM.from_pretrained("ahmed-7124/dgptAW")
print("Model and tokenizer loaded successfully!")
except Exception as e:
print(f"Error loading tokenizer or model: {e}")
print("Trying with GPT-2 tokenizer as a fallback...")
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("ahmed-7124/dgptAW")
from transformers import AutoTokenizer, AutoModelForCausalLM
def answer_medical_query(query):
try:
inputs = tokenizer(query, return_tensors="pt")
outputs = model.generate(
inputs.input_ids,
max_length=50,
temperature=0.7,
num_return_sequences=1,
pad_token_id=tokenizer.eos_token_id # Avoid errors if padding is required
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
except Exception as e:
return f"An error occurred while generating a response: {e}"
# Unified Gradio Interface
analyze_report_interface = gr.Interface(
fn=analyze_or_extract_report,
inputs=[
gr.Number(label="Patient ID"),
gr.File(label="Upload PDF Report"), # Removed optional=True
gr.Textbox(label="Report Text (Optional)"),
],
outputs="text",
)
# eye_disease_interface = gr.Interface(
# fn=predict_eye_disease,
# inputs=gr.Image(label="Upload an Eye Image", type="numpy"),
# outputs="text",
# )
# Gradio Interface for Medical Query Answering
medical_query_interface = gr.Interface(
fn=answer_medical_query,
inputs=gr.Textbox(label="Ask a Medical Term"),
outputs="text",
)
doctor_space_interface = gr.Interface(
fn=doctor_space,
inputs=gr.Number(label="Patient ID"),
outputs="text",
)
pharmacist_space_interface = gr.Interface(
fn=pharmacist_space,
inputs=gr.Number(label="Patient ID"),
outputs="text",
)
# Gradio Interface for Medical Query Answering
medical_query_interface = gr.Interface(
fn=answer_medical_query,
inputs=gr.Textbox(label="Ask a Medical Term"),
outputs="text",
)
patient_dashboard_interface = gr.Interface(
fn=patient_dashboard,
inputs=[
gr.Number(label="Patient ID"),
gr.Textbox(label="Password", type="password"),
],
outputs="text",
)
doctor_dashboard_interface = gr.Interface(
fn=doctor_dashboard,
inputs=gr.Textbox(label="Doctor Password", type="password"),
outputs="text",
)
consult_doctor_interface = gr.Interface(
fn=consult_doctor,
inputs=gr.Textbox(label="Enter Your Query for the Doctor"),
outputs="text",
)
# Gradio App Layout
with gr.Blocks() as app:
gr.Markdown("# Medico GPT")
with gr.Tab("Patient Registration"):
registration_interface.render()
# with gr.Tab("Analyze Medical Report"):
# report_analysis_interface.render()
with gr.Tab("Analyze Medical Report"):
analyze_report_interface.render()
with gr.Tab("Doctor Consult"):
consult_doctor_interface.render()
#with gr.Tab("Extract PDF Report"):
# pdf_extraction_interface.render()
with gr.Tab("Doctor Space"):
doctor_space_interface.render()
with gr.Tab("Pharmacist Space"):
pharmacist_space_interface.render()
with gr.Tab("Encyclopedia"):
medical_query_interface.render()
with gr.Tab("Patient Dashboard"):
patient_dashboard_interface.render()
with gr.Tab("Doctor Dashboard"):
doctor_dashboard_interface.render()
# Launch the app
app.launch(share=True)
|