MedAI_Analyzer / app.py
erayman09's picture
Update app.py
3594567 verified
raw
history blame
2.89 kB
import gradio as gr
from transformers import pipeline
from PIL import Image
from PyPDF2 import PdfReader
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
# Load Hugging Face OCR model
ocr_model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
# Load BioGPT or another medical model
medical_analyzer = pipeline("text-classification", model="microsoft/biogpt")
# Function to extract text using Hugging Face OCR model
def extract_text(file_path):
try:
# Handle PDFs
if file_path.endswith(".pdf"):
reader = PdfReader(file_path)
text = "".join(page.extract_text() for page in reader.pages)
return text.strip()
# Handle images
result = ocr_model(Image.open(file_path))
return result[0]['generated_text']
except Exception as e:
return f"Error processing the file: {e}"
# Function to generate a PDF report
def create_pdf_report(analysis, output_path):
c = canvas.Canvas(output_path, pagesize=letter)
c.drawString(100, 750, "Blood Test Report Analysis")
c.drawString(100, 730, "---------------------------")
y_position = 700
for line in analysis.split("\n"):
c.drawString(100, y_position, line)
y_position -= 20
c.save()
return output_path
# Function to analyze blood test reports
def analyze_blood_test(file):
try:
# Step 1: Extract text
extracted_text = extract_text(file)
if not extracted_text.strip():
return "No readable text found in the uploaded file.", None
# Step 2: Analyze extracted text using BioGPT
analysis_results = medical_analyzer(extracted_text)
# Step 3: Generate analysis report
analysis_report = "πŸ” Analysis Results:\n"
for item in analysis_results:
analysis_report += f"- {item['label']}: {item['score']:.2f}\n"
# Step 4: Generate PDF report
output_pdf = "analysis_report.pdf"
create_pdf_report(f"Extracted Text:\n{extracted_text}\n\n{analysis_report}", output_pdf)
return analysis_report, output_pdf
except Exception as e:
return f"Error processing file: {e}", None
# Gradio interface setup
interface = gr.Interface(
fn=analyze_blood_test,
inputs=gr.File(label="Upload your Blood Test Report (PNG, JPG, JPEG, or PDF)"),
outputs=[
gr.Textbox(label="Analysis Results"),
gr.File(label="Download PDF Report")
],
title="MedAI Analyzer",
description=(
"Upload your blood test report in image (PNG, JPG, JPEG) or PDF format. "
"The app will extract and analyze the values, flag abnormalities, and provide health recommendations. "
"You can also download a detailed PDF report of the analysis."
),
allow_flagging="never"
)
if __name__ == "__main__":
interface.launch()