erayman09 commited on
Commit
cfb52b3
·
verified ·
1 Parent(s): 6ba5552

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -66
app.py CHANGED
@@ -1,75 +1,70 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
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 OCR model for extracting text from images
9
- ocr_model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
10
 
11
- # Load medical AI model (BioGPT or similar) for prescription validation
12
- medical_model_name = "microsoft/BioGPT"
13
- medical_tokenizer = AutoTokenizer.from_pretrained(medical_model_name)
14
- medical_model = AutoModelForSequenceClassification.from_pretrained(medical_model_name)
 
15
 
16
- # Function to extract text from images or PDFs
17
- def extract_text(file_path):
 
 
18
  try:
19
- # Handle PDFs
20
- if file_path.endswith(".pdf"):
21
- reader = PdfReader(file_path)
22
- text = "".join(page.extract_text() for page in reader.pages)
23
- return text.strip()
24
- # Handle images
25
- result = ocr_model(Image.open(file_path))
26
- return result[0]['generated_text']
27
  except Exception as e:
28
- return f"Error processing the file: {e}"
29
-
30
- # Function to validate prescription using the medical model
31
- def validate_prescription_with_model(extracted_text):
32
- # Tokenize and process with the AI model
33
- inputs = medical_tokenizer(extracted_text, return_tensors="pt", truncation=True, padding=True)
34
- outputs = medical_model(**inputs)
35
- logits = outputs.logits
36
- predictions = logits.softmax(dim=-1)
37
-
38
- # Generate model-driven validation results
39
- validation_report = "🔍 Prescription Validation Results:\n"
40
- for i, score in enumerate(predictions[0]):
41
- token = medical_tokenizer.decode([i]).strip()
42
- if token not in ["[PAD]", "[unused1]"]: # Ignore invalid tokens
43
- validation_report += f"- {token}: {score.item():.2f}\n"
44
- return validation_report
45
-
46
- # Main function to handle prescription analysis
47
- def analyze_prescription(file):
48
- try:
49
- # Step 1: Extract text
50
- extracted_text = extract_text(file)
51
- if not extracted_text.strip():
52
- return "No readable text found in the uploaded file.", None
53
 
54
- # Step 2: Validate prescription using AI model
55
- validation_report = validate_prescription_with_model(extracted_text)
 
 
56
 
57
- # Combine the extracted text and validation results
58
- full_report = f"Extracted Text:\n{extracted_text}\n\n{validation_report}"
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- # Step 3: Generate a PDF report
61
- output_pdf = "prescription_validation_report.pdf"
62
- create_pdf_report(full_report, output_pdf)
 
 
63
 
64
- return full_report, output_pdf
65
- except Exception as e:
66
- return f"Error processing file: {e}", None
67
 
68
- # Function to create a PDF report
69
- def create_pdf_report(content, output_path):
 
 
 
 
 
 
 
 
70
  c = canvas.Canvas(output_path, pagesize=letter)
71
- c.drawString(100, 750, "Prescription Validation Report")
72
- c.drawString(100, 730, "------------------------------")
73
  y_position = 700
74
  for line in content.split("\n"):
75
  c.drawString(100, y_position, line)
@@ -77,20 +72,59 @@ def create_pdf_report(content, output_path):
77
  c.save()
78
  return output_path
79
 
80
- # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  interface = gr.Interface(
82
- fn=analyze_prescription,
83
- inputs=gr.File(label="Upload your Prescription (PNG, JPG, JPEG, or PDF)"),
84
  outputs=[
85
- gr.Textbox(label="Validation Results"),
86
- gr.File(label="Download PDF Report")
87
  ],
88
- title="AI-Powered Prescription Validator",
89
  description=(
90
- "Upload your medical prescription in image (PNG, JPG, JPEG) or PDF format. "
91
- "The app will extract the text, analyze it using advanced AI models, and validate the prescription. "
92
- "Download a comprehensive PDF report of the validation results."
93
  ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  allow_flagging="never"
95
  )
96
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  from PIL import Image
4
+ import pytesseract
5
+ import json
6
  from reportlab.lib.pagesizes import letter
7
  from reportlab.pdfgen import canvas
8
 
9
+ # Load BioGPT model for recommendations
10
+ bio_gpt = pipeline("text-generation", model="microsoft/BioGPT")
11
 
12
+ # Load reference ranges from dataset.json
13
+ def load_reference_ranges(file_path="dataset.json"):
14
+ with open(file_path, "r") as file:
15
+ reference_ranges = json.load(file)
16
+ return reference_ranges
17
 
18
+ reference_ranges = load_reference_ranges()
19
+
20
+ # Extract text from uploaded image using OCR
21
+ def extract_text_from_image(image):
22
  try:
23
+ text = pytesseract.image_to_string(Image.open(image))
24
+ return text
 
 
 
 
 
 
25
  except Exception as e:
26
+ return f"Error extracting text: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ # Analyze extracted text and compare against reference ranges
29
+ def analyze_blood_report(text):
30
+ abnormalities = []
31
+ analysis = "Blood Test Analysis Results:\n\n"
32
 
33
+ for param, ranges in reference_ranges.items():
34
+ if param in text.lower():
35
+ try:
36
+ # Mock parsing logic to extract the value
37
+ value = float(text.split(param)[1].split()[0]) # Extract value after the parameter name
38
+ if value < ranges["low"]:
39
+ abnormalities.append(f"{param.capitalize()} is LOW ({value} {ranges['unit']}).")
40
+ elif value > ranges["high"]:
41
+ abnormalities.append(f"{param.capitalize()} is HIGH ({value} {ranges['unit']}).")
42
+ else:
43
+ analysis += f"{param.capitalize()} is NORMAL ({value} {ranges['unit']}).\n"
44
+ except Exception:
45
+ analysis += f"{param.capitalize()} could not be analyzed.\n"
46
 
47
+ # Flag abnormalities
48
+ if abnormalities:
49
+ analysis += "\nAbnormalities Detected:\n" + "\n".join(abnormalities) + "\n"
50
+ else:
51
+ analysis += "\nNo abnormalities detected.\n"
52
 
53
+ return analysis, abnormalities
 
 
54
 
55
+ # Generate recommendations using BioGPT
56
+ def get_recommendations(abnormalities):
57
+ if not abnormalities:
58
+ return "No recommendations needed."
59
+ query = " ".join(abnormalities) + " Provide medical recommendations."
60
+ recommendations = bio_gpt(query, max_length=100, num_return_sequences=1)[0]["generated_text"]
61
+ return recommendations
62
+
63
+ # Create a PDF report
64
+ def create_pdf_report(content, output_path="blood_test_report.pdf"):
65
  c = canvas.Canvas(output_path, pagesize=letter)
66
+ c.drawString(100, 750, "Blood Test Report")
67
+ c.drawString(100, 730, "-----------------")
68
  y_position = 700
69
  for line in content.split("\n"):
70
  c.drawString(100, y_position, line)
 
72
  c.save()
73
  return output_path
74
 
75
+ # Main function to process blood test image
76
+ def process_blood_test(image):
77
+ # Step 1: Extract text
78
+ extracted_text = extract_text_from_image(image)
79
+ if "Error" in extracted_text:
80
+ return extracted_text, None
81
+
82
+ # Step 2: Analyze extracted text
83
+ analysis, abnormalities = analyze_blood_report(extracted_text)
84
+
85
+ # Step 3: Generate recommendations
86
+ recommendations = get_recommendations(abnormalities)
87
+
88
+ # Step 4: Combine results and create PDF
89
+ full_report = analysis + "\nRecommendations:\n" + recommendations
90
+ pdf_path = create_pdf_report(full_report)
91
+
92
+ return full_report, pdf_path
93
+
94
+ # Gradio Interface
95
  interface = gr.Interface(
96
+ fn=process_blood_test,
97
+ inputs=gr.Image(type="file", label="Upload Blood Test Report Image (PNG, JPG, JPEG)"),
98
  outputs=[
99
+ gr.Textbox(label="Analysis and Recommendations"),
100
+ gr.File(label="Download PDF Report"),
101
  ],
102
+ title="AI Blood Test Analyzer",
103
  description=(
104
+ "Upload a blood test report image (PNG, JPG, JPEG), and the app will analyze the values, flag abnormalities, "
105
+ "and provide recommendations using BioGPT. You can also download a PDF report."
 
106
  ),
107
+ theme="compact",
108
+ css="""
109
+ body {
110
+ font-family: 'Arial', sans-serif;
111
+ background-color: #f9f9f9;
112
+ }
113
+ .gradio-container {
114
+ color: #333;
115
+ max-width: 800px;
116
+ margin: 0 auto;
117
+ }
118
+ .gradio-container .label {
119
+ font-weight: bold;
120
+ font-size: 18px;
121
+ }
122
+ .gradio-container .output {
123
+ background-color: #eef;
124
+ padding: 10px;
125
+ border-radius: 10px;
126
+ }
127
+ """,
128
  allow_flagging="never"
129
  )
130