Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import pytesseract
|
5 |
+
from PyPDF2 import PdfReader
|
6 |
+
from reportlab.lib.pagesizes import letter
|
7 |
+
from reportlab.pdfgen import canvas
|
8 |
+
import os
|
9 |
+
|
10 |
+
# Load the medical analysis model (e.g., BioGPT or PubMedBERT)
|
11 |
+
medical_analyzer = pipeline("text-classification", model="microsoft/biogpt")
|
12 |
+
|
13 |
+
# Function to extract text from images or PDFs
|
14 |
+
def extract_text(file_path):
|
15 |
+
if file_path.endswith(".pdf"):
|
16 |
+
# Extract text from PDF
|
17 |
+
reader = PdfReader(file_path)
|
18 |
+
text = ""
|
19 |
+
for page in reader.pages:
|
20 |
+
text += page.extract_text()
|
21 |
+
return text.strip()
|
22 |
+
else:
|
23 |
+
# Extract text from image
|
24 |
+
return pytesseract.image_to_string(Image.open(file_path))
|
25 |
+
|
26 |
+
# Function to generate a PDF report
|
27 |
+
def create_pdf_report(analysis, output_path):
|
28 |
+
c = canvas.Canvas(output_path, pagesize=letter)
|
29 |
+
c.drawString(100, 750, "Blood Test Report Analysis")
|
30 |
+
c.drawString(100, 730, "---------------------------")
|
31 |
+
|
32 |
+
y_position = 700
|
33 |
+
for line in analysis.split("\n"):
|
34 |
+
c.drawString(100, y_position, line)
|
35 |
+
y_position -= 20 # Move down for the next line
|
36 |
+
|
37 |
+
c.save()
|
38 |
+
return output_path
|
39 |
+
|
40 |
+
# Function to analyze blood test reports
|
41 |
+
def analyze_blood_test(file):
|
42 |
+
# Step 1: Extract text
|
43 |
+
extracted_text = extract_text(file)
|
44 |
+
if not extracted_text:
|
45 |
+
return "Could not extract text. Please upload a valid file.", None
|
46 |
+
|
47 |
+
# Step 2: Use medical model to analyze extracted text
|
48 |
+
analysis_results = medical_analyzer(extracted_text)
|
49 |
+
analysis_report = "🔍 Analysis Results:\n"
|
50 |
+
for item in analysis_results[:5]: # Limit results for simplicity
|
51 |
+
analysis_report += f"- {item['label']}: {item['score']:.2f}\n"
|
52 |
+
|
53 |
+
# Step 3: Generate downloadable PDF report
|
54 |
+
output_pdf = "analysis_report.pdf"
|
55 |
+
create_pdf_report(f"Extracted Text:\n{extracted_text}\n\n{analysis_report}", output_pdf)
|
56 |
+
|
57 |
+
return analysis_report, output_pdf
|
58 |
+
|
59 |
+
# Gradio interface setup
|
60 |
+
interface = gr.Interface(
|
61 |
+
fn=analyze_blood_test,
|
62 |
+
inputs=gr.File(label="Upload your blood test report (PNG, JPG, JPEG, or PDF)"),
|
63 |
+
outputs=[
|
64 |
+
gr.Textbox(label="Analysis Results"),
|
65 |
+
gr.File(label="Download PDF Report")
|
66 |
+
],
|
67 |
+
title="MedAI Analyzer",
|
68 |
+
description=(
|
69 |
+
"Upload your blood test report in image (PNG, JPG, JPEG) or PDF format. "
|
70 |
+
"The app will extract and analyze the values, flag abnormalities, and provide health recommendations. "
|
71 |
+
"You can also download a detailed PDF report of the analysis."
|
72 |
+
),
|
73 |
+
allow_flagging="never"
|
74 |
+
)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
interface.launch()
|