Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,64 @@
|
|
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
|
11 |
-
|
12 |
|
13 |
-
# Function to extract text
|
14 |
def extract_text(file_path):
|
15 |
-
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
text
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
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
|
36 |
-
|
37 |
c.save()
|
38 |
return output_path
|
39 |
|
40 |
# Function to analyze blood test reports
|
41 |
def analyze_blood_test(file):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
analysis_report += f"- {item['label']}: {item['score']:.2f}\n"
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
|
59 |
# Gradio interface setup
|
60 |
interface = gr.Interface(
|
61 |
fn=analyze_blood_test,
|
62 |
-
inputs=gr.File(label="Upload your
|
63 |
outputs=[
|
64 |
gr.Textbox(label="Analysis Results"),
|
65 |
gr.File(label="Download PDF Report")
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
4 |
from PyPDF2 import PdfReader
|
5 |
from reportlab.lib.pagesizes import letter
|
6 |
from reportlab.pdfgen import canvas
|
|
|
7 |
|
8 |
+
# Load Hugging Face OCR model
|
9 |
+
ocr_model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
10 |
|
11 |
+
# Function to extract text using Hugging Face OCR model
|
12 |
def extract_text(file_path):
|
13 |
+
try:
|
14 |
+
# Handle PDFs
|
15 |
+
if file_path.endswith(".pdf"):
|
16 |
+
reader = PdfReader(file_path)
|
17 |
+
text = "".join(page.extract_text() for page in reader.pages)
|
18 |
+
return text.strip()
|
19 |
+
# Handle images
|
20 |
+
result = ocr_model(Image.open(file_path))
|
21 |
+
return result[0]['generated_text']
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error processing the file: {e}"
|
24 |
|
25 |
# Function to generate a PDF report
|
26 |
def create_pdf_report(analysis, output_path):
|
27 |
c = canvas.Canvas(output_path, pagesize=letter)
|
28 |
c.drawString(100, 750, "Blood Test Report Analysis")
|
29 |
c.drawString(100, 730, "---------------------------")
|
|
|
30 |
y_position = 700
|
31 |
for line in analysis.split("\n"):
|
32 |
c.drawString(100, y_position, line)
|
33 |
+
y_position -= 20
|
|
|
34 |
c.save()
|
35 |
return output_path
|
36 |
|
37 |
# Function to analyze blood test reports
|
38 |
def analyze_blood_test(file):
|
39 |
+
try:
|
40 |
+
# Step 1: Extract text
|
41 |
+
extracted_text = extract_text(file)
|
42 |
+
if not extracted_text.strip():
|
43 |
+
return "No readable text found in the uploaded file.", None
|
44 |
|
45 |
+
# Step 2: Generate dummy analysis results (replace with actual logic)
|
46 |
+
analysis_report = "π Analysis Results:\n"
|
47 |
+
analysis_report += "- Example: High Cholesterol Detected\n"
|
48 |
+
analysis_report += "- Example: Low Hemoglobin Levels\n"
|
|
|
49 |
|
50 |
+
# Step 3: Generate PDF report
|
51 |
+
output_pdf = "analysis_report.pdf"
|
52 |
+
create_pdf_report(f"Extracted Text:\n{extracted_text}\n\n{analysis_report}", output_pdf)
|
53 |
+
|
54 |
+
return analysis_report, output_pdf
|
55 |
+
except Exception as e:
|
56 |
+
return f"Error processing file: {e}", None
|
57 |
|
58 |
# Gradio interface setup
|
59 |
interface = gr.Interface(
|
60 |
fn=analyze_blood_test,
|
61 |
+
inputs=gr.File(label="Upload your Blood Test Report (PNG, JPG, JPEG, or PDF)"),
|
62 |
outputs=[
|
63 |
gr.Textbox(label="Analysis Results"),
|
64 |
gr.File(label="Download PDF Report")
|