Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,21 @@
|
|
1 |
-
import gradio as gr
|
2 |
import fitz # PyMuPDF
|
3 |
import re
|
|
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
st.title("π©Ί Medical Report Analyzer")
|
10 |
-
uploaded_file = st.file_uploader("Upload Medical PDF Report", type=["pdf"])
|
11 |
-
|
12 |
normal_ranges = {
|
13 |
"Hemoglobin": (13.5, 17.5), # g/dL
|
14 |
-
"WBC": (4000, 11000), #
|
15 |
-
"Platelets": (150000, 450000), #
|
16 |
-
"RBC": (4.7, 6.1), # million
|
17 |
"Creatinine": (0.6, 1.3), # mg/dL
|
18 |
-
"Glucose": (70, 99), # mg/dL
|
19 |
}
|
20 |
|
21 |
-
def
|
22 |
try:
|
23 |
doc = fitz.open(stream=file.read(), filetype="pdf")
|
24 |
text = ""
|
@@ -26,10 +23,13 @@ def extract_text_from_pdf(file):
|
|
26 |
text += page.get_text()
|
27 |
return text
|
28 |
except Exception as e:
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
def parse_report(text):
|
33 |
results = {}
|
34 |
for test, (low, high) in normal_ranges.items():
|
35 |
pattern = rf"{test}[^0-9\-]*([\d\.]+)"
|
@@ -38,41 +38,46 @@ def parse_report(text):
|
|
38 |
value = float(match.group(1))
|
39 |
status = "Normal" if low <= value <= high else "Abnormal"
|
40 |
results[test] = {"value": value, "low": low, "high": high, "status": status}
|
41 |
-
return results
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
ax.
|
|
|
|
|
50 |
ax.set_title("Medical Report Values")
|
51 |
ax.set_ylabel("Measured Value")
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
"Normal Range": f'{data["low"]} - {data["high"]}',
|
66 |
-
"Status": data["status"]
|
67 |
-
}
|
68 |
-
for test, data in results.items()
|
69 |
-
])
|
70 |
-
def highlight_abnormal(row):
|
71 |
-
return ['background-color: red; color: white' if row.Status == 'Abnormal' else '' for _ in row]
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
st.subheader("π Visual Comparison")
|
76 |
-
plot_results(results)
|
77 |
-
else:
|
78 |
-
st.warning("No known medical values found.")
|
|
|
|
|
1 |
import fitz # PyMuPDF
|
2 |
import re
|
3 |
+
import gradio as gr
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
6 |
+
import io
|
7 |
|
8 |
+
# Normal reference ranges
|
|
|
|
|
|
|
|
|
9 |
normal_ranges = {
|
10 |
"Hemoglobin": (13.5, 17.5), # g/dL
|
11 |
+
"WBC": (4000, 11000), # /Β΅L
|
12 |
+
"Platelets": (150000, 450000), # /Β΅L
|
13 |
+
"RBC": (4.7, 6.1), # million/Β΅L
|
14 |
"Creatinine": (0.6, 1.3), # mg/dL
|
15 |
+
"Glucose": (70, 99), # mg/dL (fasting)
|
16 |
}
|
17 |
|
18 |
+
def extract_text(file):
|
19 |
try:
|
20 |
doc = fitz.open(stream=file.read(), filetype="pdf")
|
21 |
text = ""
|
|
|
23 |
text += page.get_text()
|
24 |
return text
|
25 |
except Exception as e:
|
26 |
+
return f"β Error: {str(e)}"
|
27 |
+
|
28 |
+
def analyze_report(file):
|
29 |
+
text = extract_text(file)
|
30 |
+
if "β Error" in text:
|
31 |
+
return text, None
|
32 |
|
|
|
33 |
results = {}
|
34 |
for test, (low, high) in normal_ranges.items():
|
35 |
pattern = rf"{test}[^0-9\-]*([\d\.]+)"
|
|
|
38 |
value = float(match.group(1))
|
39 |
status = "Normal" if low <= value <= high else "Abnormal"
|
40 |
results[test] = {"value": value, "low": low, "high": high, "status": status}
|
|
|
41 |
|
42 |
+
if not results:
|
43 |
+
return "No known lab values found in report.", None
|
44 |
+
|
45 |
+
# Create dataframe
|
46 |
+
df = pd.DataFrame([
|
47 |
+
{
|
48 |
+
"Test": test,
|
49 |
+
"Value": data["value"],
|
50 |
+
"Normal Range": f'{data["low"]} - {data["high"]}',
|
51 |
+
"Status": data["status"]
|
52 |
+
}
|
53 |
+
for test, data in results.items()
|
54 |
+
])
|
55 |
|
56 |
+
# Plot chart
|
57 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
58 |
+
colors = ["green" if row["Status"] == "Normal" else "red" for _, row in df.iterrows()]
|
59 |
+
ax.bar(df["Test"], df["Value"], color=colors)
|
60 |
ax.set_title("Medical Report Values")
|
61 |
ax.set_ylabel("Measured Value")
|
62 |
+
ax.tick_params(axis='x', rotation=45)
|
63 |
+
|
64 |
+
buf = io.BytesIO()
|
65 |
+
plt.tight_layout()
|
66 |
+
plt.savefig(buf, format="png")
|
67 |
+
buf.seek(0)
|
68 |
+
|
69 |
+
return df, buf
|
70 |
|
71 |
+
demo = gr.Interface(
|
72 |
+
fn=analyze_report,
|
73 |
+
inputs=gr.File(label="Upload Medical PDF Report"),
|
74 |
+
outputs=[
|
75 |
+
gr.Dataframe(label="Extracted Values"),
|
76 |
+
gr.Image(type="pil", label="Visualization")
|
77 |
+
],
|
78 |
+
title="π©Ί Medical Report Analyzer",
|
79 |
+
description="Upload a medical lab report (PDF). Extracted lab values will be compared with normal reference ranges. Abnormal values are flagged and visualized in red."
|
80 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.launch()
|
|
|
|
|
|
|
|