Radiology / app.py
Rammohan0504's picture
Update app.py
02c0b0a verified
import easyocr
import numpy as np
from transformers import pipeline
from PIL import Image
import gradio as gr
# Initialize EasyOCR Reader (for OCR)
reader = easyocr.Reader(['en'])
# Use the BioGPT or any other relevant medical model for summarization
summarizer = pipeline("summarization", model="microsoft/BioGPT")
# OCR + Analyzed Report Function
def process_report(file):
if file.name.endswith(('.jpg', '.png', '.jpeg')):
# Open the image using PIL
image = Image.open(file)
# Convert the PIL image to a numpy array
image_np = np.array(image)
# Use EasyOCR to extract text from the numpy array image
text = reader.readtext(image_np, detail=0, paragraph=True) # Extracts text as a list of strings
text = ' '.join(text) # Join the list into a single string
else:
# If the file is already text, read the content
text = file.read().decode("utf-8")
# Clean up the extracted text (remove extra spaces or unwanted characters)
text = text.strip().replace("\n", " ")
# Ensure that the text is in the right format for the summarizer (e.g., remove non-relevant text)
unwanted_phrases = ["CNN.com", "iReport", "Submit your best photos", "Travel Snapshots", "gallery", "U.S.", "Travel"]
for phrase in unwanted_phrases:
text = text.replace(phrase, "")
# Optional: Print extracted text for debugging
print("Extracted Text:", text)
# Ensure there's no extra spaces left
text = ' '.join(text.split())
# Use the summarizer model to provide a meaningful summary of the radiology report
# Using BioGPT or another medical NLP model to analyze the report's content
summary = summarizer(text[:1024], max_length=300, min_length=150, do_sample=False)
# Return the summarized analysis of the radiology report
return summary[0]['summary_text']
# Gradio UI
iface = gr.Interface(
fn=process_report,
inputs=gr.File(label="Upload Radiology Report (Image or Text)"),
outputs="text",
title="Radiology Report Analyzer",
description="Upload a radiology report (either as an image or a text file) to get a concise, medically relevant summary and analysis."
)
# Launch the Gradio app
iface.launch()