ashhal commited on
Commit
7a0cfef
Β·
verified Β·
1 Parent(s): 9bd632e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -46
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
- st.set_page_config(page_title="Medical Report Analyzer", layout="centered")
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), # cells/mcL
15
- "Platelets": (150000, 450000), # cells/mcL
16
- "RBC": (4.7, 6.1), # million cells/mcL
17
  "Creatinine": (0.6, 1.3), # mg/dL
18
- "Glucose": (70, 99), # mg/dL
19
  }
20
 
21
- def extract_text_from_pdf(file):
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
- st.error(f"❌ Error reading PDF: {e}")
30
- return None
 
 
 
 
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
- def plot_results(results):
44
- labels = list(results.keys())
45
- values = [results[k]["value"] for k in labels]
46
- colors = ["green" if results[k]["status"] == "Normal" else "red" for k in labels]
 
 
 
 
 
 
 
 
 
47
 
48
- fig, ax = plt.subplots()
49
- ax.bar(labels, values, color=colors)
 
 
50
  ax.set_title("Medical Report Values")
51
  ax.set_ylabel("Measured Value")
52
- plt.xticks(rotation=45)
53
- st.pyplot(fig)
 
 
 
 
 
 
54
 
55
- if uploaded_file:
56
- with st.spinner("πŸ” Analyzing report..."):
57
- text = extract_text_from_pdf(uploaded_file)
58
- if text:
59
- results = parse_report(text)
60
- if results:
61
- df = pd.DataFrame([
62
- {
63
- "Test": test,
64
- "Value": data["value"],
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
- st.subheader("πŸ“‹ Extracted Test Results")
74
- st.dataframe(df.style.apply(highlight_abnormal, axis=1), use_container_width=True)
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()