Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import torch
|
|
| 7 |
import torchxrayvision as xrv
|
| 8 |
from torchvision import transforms
|
| 9 |
import re
|
|
|
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
# Imaging Agent (Chest X-ray, proxy for lung cancer risk)
|
|
@@ -30,7 +31,12 @@ def imaging_agent(image_path: str):
|
|
| 30 |
if arr.max() > 1:
|
| 31 |
arr /= 255.0
|
| 32 |
arr = xrv.datasets.normalize(arr, 4096)
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
arr = torch.from_numpy(arr).unsqueeze(0).unsqueeze(0).to(DEVICE)
|
| 35 |
|
| 36 |
with torch.no_grad():
|
|
@@ -86,24 +92,38 @@ def coordinator(imaging_txt, lab_txt):
|
|
| 86 |
return summary
|
| 87 |
|
| 88 |
# -----------------------------
|
| 89 |
-
#
|
| 90 |
# -----------------------------
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
SAMPLE_LABS = "PSA: 8 ng/mL\nCA125: 20 U/mL\nAFP: 15 ng/mL"
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
def run_all(image, labs):
|
| 95 |
txt, raw = imaging_agent(image) if image else ("No image.", None)
|
| 96 |
lab = lab_agent(labs)
|
| 97 |
coord = coordinator(txt, lab)
|
| 98 |
return txt, raw, lab, coord
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
with gr.Blocks(theme="soft") as demo:
|
| 101 |
gr.Markdown("# 🏥 AI Diagnostics Agent: Early Cancer Discovery (Demo)")
|
| 102 |
-
gr.Markdown("Upload a chest X-ray or
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column():
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
imaging_out = gr.Textbox(label="Imaging Agent Output")
|
| 108 |
imaging_raw = gr.Code(label="Probabilities JSON", language="json")
|
| 109 |
with gr.Column():
|
|
@@ -113,6 +133,12 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 113 |
run_btn = gr.Button("Run Agents")
|
| 114 |
coord_out = gr.Textbox(label="Coordinator Summary", lines=10)
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
run_btn.click(run_all, inputs=[img_in, lab_in], outputs=[imaging_out, imaging_raw, lab_out, coord_out])
|
| 117 |
|
| 118 |
demo.launch()
|
|
@@ -120,3 +146,4 @@ demo.launch()
|
|
| 120 |
|
| 121 |
|
| 122 |
|
|
|
|
|
|
| 7 |
import torchxrayvision as xrv
|
| 8 |
from torchvision import transforms
|
| 9 |
import re
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
# -----------------------------
|
| 13 |
# Imaging Agent (Chest X-ray, proxy for lung cancer risk)
|
|
|
|
| 31 |
if arr.max() > 1:
|
| 32 |
arr /= 255.0
|
| 33 |
arr = xrv.datasets.normalize(arr, 4096)
|
| 34 |
+
|
| 35 |
+
# TorchXRayVision expects dict
|
| 36 |
+
sample = {"img": arr}
|
| 37 |
+
sample = resize(sample)
|
| 38 |
+
arr = sample["img"]
|
| 39 |
+
|
| 40 |
arr = torch.from_numpy(arr).unsqueeze(0).unsqueeze(0).to(DEVICE)
|
| 41 |
|
| 42 |
with torch.no_grad():
|
|
|
|
| 92 |
return summary
|
| 93 |
|
| 94 |
# -----------------------------
|
| 95 |
+
# Demo samples
|
| 96 |
# -----------------------------
|
| 97 |
+
SAMPLES = {
|
| 98 |
+
"Normal X-ray": "samples/sample_xray1.png",
|
| 99 |
+
"Suspicious X-ray": "samples/sample_xray2.png",
|
| 100 |
+
}
|
| 101 |
SAMPLE_LABS = "PSA: 8 ng/mL\nCA125: 20 U/mL\nAFP: 15 ng/mL"
|
| 102 |
|
| 103 |
+
# -----------------------------
|
| 104 |
+
# Runner
|
| 105 |
+
# -----------------------------
|
| 106 |
def run_all(image, labs):
|
| 107 |
txt, raw = imaging_agent(image) if image else ("No image.", None)
|
| 108 |
lab = lab_agent(labs)
|
| 109 |
coord = coordinator(txt, lab)
|
| 110 |
return txt, raw, lab, coord
|
| 111 |
|
| 112 |
+
# -----------------------------
|
| 113 |
+
# Gradio UI
|
| 114 |
+
# -----------------------------
|
| 115 |
with gr.Blocks(theme="soft") as demo:
|
| 116 |
gr.Markdown("# 🏥 AI Diagnostics Agent: Early Cancer Discovery (Demo)")
|
| 117 |
+
gr.Markdown("Upload a chest X-ray, or pick a demo sample. Paste tumor marker labs.\n\n⚠️ Research demo only. Not for clinical use.")
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
with gr.Column():
|
| 121 |
+
sample_dropdown = gr.Dropdown(
|
| 122 |
+
choices=list(SAMPLES.keys()),
|
| 123 |
+
value="Normal X-ray",
|
| 124 |
+
label="Select Sample X-ray"
|
| 125 |
+
)
|
| 126 |
+
img_in = gr.Image(type="filepath", label="Chest X-ray (PNG/JPG)")
|
| 127 |
imaging_out = gr.Textbox(label="Imaging Agent Output")
|
| 128 |
imaging_raw = gr.Code(label="Probabilities JSON", language="json")
|
| 129 |
with gr.Column():
|
|
|
|
| 133 |
run_btn = gr.Button("Run Agents")
|
| 134 |
coord_out = gr.Textbox(label="Coordinator Summary", lines=10)
|
| 135 |
|
| 136 |
+
# Link dropdown to image input
|
| 137 |
+
def load_sample(choice):
|
| 138 |
+
return SAMPLES.get(choice, None)
|
| 139 |
+
sample_dropdown.change(load_sample, inputs=sample_dropdown, outputs=img_in)
|
| 140 |
+
|
| 141 |
+
# Main button
|
| 142 |
run_btn.click(run_all, inputs=[img_in, lab_in], outputs=[imaging_out, imaging_raw, lab_out, coord_out])
|
| 143 |
|
| 144 |
demo.launch()
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
+
|