med-chatbot / app.py
sec23ad137's picture
Upload 2 files
950a88a verified
import gradio as gr
import os
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
# Logic to process the uploaded report and extract tumor details
def process_report(file):
# Replace this with actual logic to analyze the uploaded file
# Example output
tumor_details = {
"has_tumor": True,
"tumor_type": "Glioma",
"size": "1.2 cm",
"location": "Right occipital lobe",
"confidence": 0.9,
"analysis_details": {
"anomalous_regions": 2,
"edge_complexity": 151,
"tumor_score": 1,
"region_intensities": [
220.39,
158.87,
210.12,
176.93,
134.04,
160.1,
176.74,
124.37,
171.66
]
}
}
return tumor_details
def generate_pdf_report(tumor_details):
# Path to save the tumor analysis PDF report
pdf_path = "tumor_analysis_report.pdf"
# Create a PDF canvas
c = canvas.Canvas(pdf_path, pagesize=letter)
width, height = letter
# Title
c.setFont("Helvetica-Bold", 16)
c.drawCentredString(width / 2, height - 50, "MRI BRAIN REPORT")
# Patient and scan details
c.setFont("Helvetica", 12)
c.drawString(50, height - 100, "Patient Name: ______________________")
c.drawString(50, height - 120, "Scan Type: MRI Brain")
c.drawString(50, height - 140, "Date: _____________________________")
# Findings
c.setFont("Helvetica-Bold", 12)
c.drawString(50, height - 180, "FINDINGS:")
c.setFont("Helvetica", 12)
c.drawString(70, height - 200, f"- Tumor Presence: {tumor_details['has_tumor']}")
c.drawString(70, height - 220, f"- Tumor Type: {tumor_details['tumor_type']}")
c.drawString(70, height - 240, f"- Location: {tumor_details['location']}")
c.drawString(70, height - 260, f"- Size: {tumor_details['size']}")
c.drawString(70, height - 280, f"- Confidence: {tumor_details['confidence']}")
# Radiological Description
c.setFont("Helvetica-Bold", 12)
c.drawString(50, height - 320, "Radiological Description:")
c.setFont("Helvetica", 12)
description = []
if tumor_details['has_tumor']:
description.append("The tumor appears well-defined with irregular margins.")
description.append(f"The lesion measures approximately {tumor_details['size']} and is located in the {tumor_details['location']}.")
if tumor_details['analysis_details']['anomalous_regions'] > 0:
description.append("There are areas of abnormal signal intensity, suggestive of hyperintense regions.")
if tumor_details['analysis_details']['edge_complexity'] > 100:
description.append("The lesion demonstrates heterogeneous signal patterns with irregular margins.")
description.append("Region intensities indicate varying levels of hyperintensity, consistent with the observed tumor characteristics.")
else:
description.append("No evidence of tumor detected.")
y_position = height - 340
for line in description:
c.drawString(70, y_position, f"• {line}")
y_position -= 20
# Impression
c.setFont("Helvetica-Bold", 12)
c.drawString(50, y_position - 20, "IMPRESSION:")
c.setFont("Helvetica", 12)
impression = "Features are suggestive of a low-grade glioma in the right occipital lobe. Follow-up MRI or biopsy is recommended."
c.drawString(70, y_position - 40, impression)
# Note
c.setFont("Times-Italic", 10)
c.drawString(50, y_position - 80, "NOTE:")
c.drawString(70, y_position - 100, "This is an AI-assisted preliminary report. Final confirmation should be made by a licensed radiologist.")
# Save the PDF
c.save()
return pdf_path
def generate_prescription_form():
# Path to save the prescription form PDF
pdf_path = "prescription_form.pdf"
# Create a PDF canvas
c = canvas.Canvas(pdf_path, pagesize=letter)
width, height = letter
# Prescription Form Title
c.setFont("Helvetica-Bold", 16)
c.drawCentredString(width / 2, height - 50, "PRESCRIPTION FORM")
# Prescription Details
c.setFont("Helvetica", 12)
c.drawString(50, height - 100, "Patient Name: ______________________")
c.drawString(50, height - 120, "Date: _____________________________")
c.drawString(50, height - 140, "Prescribed By: _____________________")
c.drawString(50, height - 180, "Medications:")
c.drawString(70, height - 200, "1. ______________________________")
c.drawString(70, height - 220, "2. ______________________________")
c.drawString(70, height - 240, "3. ______________________________")
c.drawString(50, height - 280, "Instructions:")
c.drawString(70, height - 300, "• ______________________________")
c.drawString(70, height - 320, "• ______________________________")
c.drawString(50, height - 360, "Follow-up Appointment:")
c.drawString(70, height - 380, "Date: _____________________________")
# Save the PDF
c.save()
return pdf_path
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Tumor Report Scanner")
gr.Markdown("Upload a scan report to extract detailed tumor analysis and generate separate PDF reports.")
with gr.Row():
file_input = gr.File(label="Upload Scan Report")
analysis_output = gr.JSON(label="Tumor Analysis Results")
report_download = gr.File(label="Download Tumor Analysis Report")
prescription_download = gr.File(label="Download Prescription Form")
submit_btn = gr.Button("Analyze Report")
def analyze_and_generate(file):
tumor_details = process_report(file)
analysis_pdf = generate_pdf_report(tumor_details)
prescription_pdf = generate_prescription_form()
return tumor_details, analysis_pdf, prescription_pdf
submit_btn.click(analyze_and_generate, inputs=file_input, outputs=[analysis_output, report_download, prescription_download])
if __name__ == "__main__":
demo.launch()