Spaces:
Runtime error
Runtime error
File size: 5,168 Bytes
7530fd9 0cfec23 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 abcf46a 7530fd9 4737a5c |
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 |
# Install necessary libraries
#!pip install gradio transformers pandas PyPDF2 pdfplumber torch torchvision timm sentencepiece
import gradio as gr
from transformers import pipeline
import pandas as pd
import PyPDF2
import pdfplumber
import torch
import timm
from PIL import Image
# Load pre-trained model for zero-shot classification
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
# Pre-trained model for X-ray analysis (example with a model from timm library)
image_model = timm.create_model('resnet50', pretrained=True)
image_model.eval()
# Initialize patient database
patients_db = []
# Function to register patients
def register_patient(name, age, gender):
patient_id = len(patients_db) + 1
patients_db.append({
"ID": patient_id,
"Name": name,
"Age": age,
"Gender": gender,
"Symptoms": "",
"Diagnosis": "",
"Action Plan": "",
"Medications": "",
"Tests": ""
})
return f"β
Patient {name} registered successfully. Patient ID: {patient_id}"
# Function to analyze text reports
def analyze_report(patient_id, report_text):
candidate_labels = ["anemia", "viral infection", "liver disease", "kidney disease", "diabetes"]
result = classifier(report_text, candidate_labels)
diagnosis = result['labels'][0]
action_plan = f"Based on your report, you might have {diagnosis}. Please consult a doctor for confirmation."
# Store diagnosis in the database
for patient in patients_db:
if patient["ID"] == patient_id:
patient["Diagnosis"] = diagnosis
patient["Action Plan"] = action_plan
break
return f"π Diagnosis: {diagnosis}. {action_plan}"
# Function to extract text from PDF reports
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
# Function to analyze uploaded images (X-ray/CT-scan)
def analyze_image(patient_id, img):
image = Image.open(img).convert('RGB')
transform = torch.nn.Sequential(
torch.nn.Upsample(size=(224, 224)),
torch.nn.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
)
image_tensor = transform(torch.unsqueeze(torch.tensor(image), 0))
# Run the image through the model (for simplicity, assuming ResNet50 output)
output = image_model(image_tensor)
_, predicted = torch.max(output, 1)
# Map prediction to a label (this is just a placeholder example)
labels = {0: "Normal", 1: "Pneumonia", 2: "Liver Disorder", 3: "COVID-19"}
diagnosis = labels.get(predicted.item(), "Unknown")
# Store diagnosis in the database
for patient in patients_db:
if patient["ID"] == patient_id:
patient["Diagnosis"] = diagnosis
break
return f"π Diagnosis from image: {diagnosis}"
# Function to display the dashboard
def show_dashboard():
if not patients_db:
return "No patient records available."
return pd.DataFrame(patients_db)
# Gradio interface for patient registration
patient_interface = gr.Interface(
fn=register_patient,
inputs=[
gr.Textbox(label="Patient Name", placeholder="Enter the patient's full name"),
gr.Number(label="Age"),
gr.Radio(label="Gender", choices=["Male", "Female", "Other"])
],
outputs="text",
description="Register a new patient"
)
# Gradio interface for report analysis (text input)
report_interface = gr.Interface(
fn=analyze_report,
inputs=[
gr.Number(label="Patient ID"),
gr.Textbox(label="Report Text", placeholder="Paste the text from your report here")
],
outputs="text",
description="Analyze blood, LFT, or other medical reports"
)
# Gradio interface for PDF report analysis (PDF upload)
pdf_report_interface = gr.Interface(
fn=lambda pdf: extract_pdf_report(pdf),
inputs=gr.File(label="Upload PDF Report"),
outputs="text",
description="Extract and analyze text from PDF reports"
)
# Gradio interface for X-ray/CT-scan image analysis
image_interface = gr.Interface(
fn=analyze_image,
inputs=[
gr.Number(label="Patient ID"),
gr.Image(type="filepath", label="Upload X-ray or CT-Scan Image")
],
outputs="text",
description="Analyze X-ray or CT-scan images for diagnosis"
)
# Gradio interface for the dashboard
dashboard_interface = gr.Interface(
fn=show_dashboard,
inputs=None,
outputs="dataframe",
description="View patient reports and history"
)
# Organize the layout using Blocks
with gr.Blocks() as demo:
gr.Markdown("# Medical Report and Image Analyzer")
with gr.TabItem("Patient Registration"):
patient_interface.render()
with gr.TabItem("Analyze Report (Text)"):
report_interface.render()
with gr.TabItem("Analyze Report (PDF)"):
pdf_report_interface.render()
with gr.TabItem("Analyze Image (X-ray/CT)"):
image_interface.render()
with gr.TabItem("Dashboard"):
dashboard_interface.render()
demo.launch(share=True)
|