Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
|
|
4 |
import json
|
5 |
from reportlab.lib.pagesizes import letter
|
6 |
from reportlab.pdfgen import canvas
|
|
|
7 |
|
8 |
# Load BioGPT model for recommendations
|
9 |
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT")
|
@@ -19,33 +20,52 @@ def load_reference_ranges(file_path="dataset.json"):
|
|
19 |
|
20 |
reference_ranges = load_reference_ranges()
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Extract text from uploaded image using Hugging Face OCR
|
23 |
def extract_text_from_image(image_path):
|
24 |
try:
|
25 |
image = Image.open(image_path)
|
26 |
text = ocr_model(image)[0]["generated_text"]
|
|
|
27 |
return text
|
28 |
except Exception as e:
|
29 |
return f"Error extracting text: {e}"
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Analyze extracted text and compare against reference ranges
|
32 |
def analyze_blood_report(text):
|
33 |
abnormalities = []
|
34 |
analysis = "Blood Test Analysis Results:\n\n"
|
35 |
|
36 |
-
for
|
37 |
-
if
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
if value < ranges["low"]:
|
42 |
-
abnormalities.append(f"{
|
43 |
elif value > ranges["high"]:
|
44 |
-
abnormalities.append(f"{
|
45 |
else:
|
46 |
-
analysis += f"{
|
47 |
-
|
48 |
-
analysis += f"{
|
49 |
|
50 |
# Flag abnormalities
|
51 |
if abnormalities:
|
@@ -111,7 +131,7 @@ interface = gr.Interface(
|
|
111 |
css="""
|
112 |
body {
|
113 |
font-family: 'Arial', sans-serif;
|
114 |
-
background-color: #
|
115 |
}
|
116 |
.gradio-container {
|
117 |
color: #333;
|
|
|
4 |
import json
|
5 |
from reportlab.lib.pagesizes import letter
|
6 |
from reportlab.pdfgen import canvas
|
7 |
+
import re
|
8 |
|
9 |
# Load BioGPT model for recommendations
|
10 |
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT")
|
|
|
20 |
|
21 |
reference_ranges = load_reference_ranges()
|
22 |
|
23 |
+
# Parameter name mapping
|
24 |
+
parameter_mapping = {
|
25 |
+
"hgb": "hemoglobin",
|
26 |
+
"hemoglobin": "hemoglobin",
|
27 |
+
"rbc": "rbc",
|
28 |
+
"wbc": "wbc",
|
29 |
+
"plt": "platelet",
|
30 |
+
"platelets": "platelet",
|
31 |
+
# Add more mappings as needed
|
32 |
+
}
|
33 |
+
|
34 |
# Extract text from uploaded image using Hugging Face OCR
|
35 |
def extract_text_from_image(image_path):
|
36 |
try:
|
37 |
image = Image.open(image_path)
|
38 |
text = ocr_model(image)[0]["generated_text"]
|
39 |
+
print("Extracted Text:", text) # Debugging
|
40 |
return text
|
41 |
except Exception as e:
|
42 |
return f"Error extracting text: {e}"
|
43 |
|
44 |
+
# Extract value using regex
|
45 |
+
def extract_value(text, param):
|
46 |
+
match = re.search(rf"{param}\s*:\s*([\d.]+)", text, re.IGNORECASE)
|
47 |
+
if match:
|
48 |
+
return float(match.group(1))
|
49 |
+
return None
|
50 |
+
|
51 |
# Analyze extracted text and compare against reference ranges
|
52 |
def analyze_blood_report(text):
|
53 |
abnormalities = []
|
54 |
analysis = "Blood Test Analysis Results:\n\n"
|
55 |
|
56 |
+
for key, standard_param in parameter_mapping.items():
|
57 |
+
if key in text.lower():
|
58 |
+
ranges = reference_ranges[standard_param]
|
59 |
+
value = extract_value(text, key)
|
60 |
+
if value is not None:
|
61 |
if value < ranges["low"]:
|
62 |
+
abnormalities.append(f"{standard_param.capitalize()} is LOW ({value} {ranges['unit']}).")
|
63 |
elif value > ranges["high"]:
|
64 |
+
abnormalities.append(f"{standard_param.capitalize()} is HIGH ({value} {ranges['unit']}).")
|
65 |
else:
|
66 |
+
analysis += f"{standard_param.capitalize()} is NORMAL ({value} {ranges['unit']}).\n"
|
67 |
+
else:
|
68 |
+
analysis += f"{standard_param.capitalize()} could not be analyzed.\n"
|
69 |
|
70 |
# Flag abnormalities
|
71 |
if abnormalities:
|
|
|
131 |
css="""
|
132 |
body {
|
133 |
font-family: 'Arial', sans-serif;
|
134 |
+
background-color: #f9f9f9;
|
135 |
}
|
136 |
.gradio-container {
|
137 |
color: #333;
|