File size: 2,714 Bytes
add57fc
 
 
 
 
 
 
34f285a
 
add57fc
34f285a
add57fc
34f285a
 
 
 
 
 
 
 
 
 
 
add57fc
 
 
 
 
 
 
 
 
34f285a
add57fc
 
 
 
 
34f285a
 
 
 
 
add57fc
34f285a
 
 
 
add57fc
34f285a
 
 
 
 
 
 
add57fc
 
 
 
34f285a
add57fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")

# 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: Generate dummy analysis results (replace with actual logic)
        analysis_report = "πŸ” Analysis Results:\n"
        analysis_report += "- Example: High Cholesterol Detected\n"
        analysis_report += "- Example: Low Hemoglobin Levels\n"

        # Step 3: 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()