erayman09 commited on
Commit
34f285a
Β·
verified Β·
1 Parent(s): 4f23bf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -33
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 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")
 
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")