Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,14 +68,14 @@ processor = AutoProcessor.from_pretrained(MODEL_ID)
|
|
| 68 |
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
-
#
|
| 72 |
# -----------------------------
|
| 73 |
@spaces.GPU
|
| 74 |
-
def
|
| 75 |
if image is None:
|
| 76 |
-
|
| 77 |
-
return
|
| 78 |
|
|
|
|
| 79 |
messages = [
|
| 80 |
{"role": "user", "content": [
|
| 81 |
{"type": "text", "text": CLINICAL_PROMPT},
|
|
@@ -83,95 +83,82 @@ def analyze_ecg_stream(image: Image.Image):
|
|
| 83 |
]}
|
| 84 |
]
|
| 85 |
|
| 86 |
-
# Prepare inputs (try chat template; fallback to plain text+image)
|
| 87 |
try:
|
| 88 |
chat_text = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 89 |
-
|
| 90 |
except Exception:
|
| 91 |
-
# Fallback
|
| 92 |
-
|
| 93 |
-
yield "[Note] Using fallback prompt packing.\n"
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
t = threading.Thread(target=_generate, daemon=True)
|
| 115 |
-
t.start()
|
| 116 |
-
|
| 117 |
-
buf = ""
|
| 118 |
-
for piece in streamer:
|
| 119 |
-
buf += piece
|
| 120 |
-
yield buf
|
| 121 |
|
| 122 |
def reset():
|
| 123 |
return None, ""
|
| 124 |
|
| 125 |
# -----------------------------
|
| 126 |
-
# UI
|
| 127 |
# -----------------------------
|
| 128 |
theme = gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
}
|
|
|
|
|
|
|
| 135 |
.header {
|
| 136 |
display:flex; align-items:center; justify-content:space-between;
|
| 137 |
-
padding: 16px
|
| 138 |
-
background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
|
| 139 |
-
color: #fff; box-shadow: 0 6px 20px rgba(0,0,0,0.25);
|
| 140 |
-
}
|
| 141 |
-
.brand { font-size: 18px; font-weight: 700; letter-spacing: 0.3px; }
|
| 142 |
-
.disclaimer {
|
| 143 |
-
margin-top: 12px; padding: 12px 14px; border-radius: 12px;
|
| 144 |
-
background: #fef2f2; color:#7f1d1d; border:1px solid #fecaca; font-weight:600;
|
| 145 |
}
|
| 146 |
-
.
|
| 147 |
-
background: #ffffff; border: 1px solid #e5e7eb; border-radius: 14px;
|
| 148 |
-
padding: 16px; box-shadow: 0 8px 18px rgba(17,24,39,0.06);
|
| 149 |
-
}
|
| 150 |
-
footer {
|
| 151 |
-
font-size: 12px; color:#6b7280; margin-top: 8px;
|
| 152 |
-
}
|
| 153 |
-
.gr-button { background-color:#1e3a8a !important; color:#fff !important; }
|
| 154 |
"""
|
| 155 |
|
| 156 |
-
with gr.Blocks(theme=theme, css=
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
""")
|
| 168 |
|
| 169 |
with gr.Row(equal_height=True):
|
| 170 |
with gr.Column(scale=1):
|
| 171 |
with gr.Group(elem_classes="card"):
|
| 172 |
-
image_input = gr.Image(
|
| 173 |
-
type="pil", label="Upload ECG Image", height=360, show_label=True
|
| 174 |
-
)
|
| 175 |
with gr.Row():
|
| 176 |
submit_btn = gr.Button("Generate Report", variant="primary")
|
| 177 |
reset_btn = gr.Button("Reset")
|
|
@@ -179,43 +166,17 @@ with gr.Blocks(theme=theme, css=custom_css, elem_id="app") as demo:
|
|
| 179 |
with gr.Column(scale=2):
|
| 180 |
with gr.Group(elem_classes="card"):
|
| 181 |
output_box = gr.Textbox(
|
| 182 |
-
label="Generated ECG Report
|
| 183 |
-
lines=
|
| 184 |
show_copy_button=True,
|
| 185 |
-
autoscroll=True,
|
| 186 |
placeholder="The model's report will appear here…",
|
| 187 |
)
|
| 188 |
-
gr.Markdown(
|
| 189 |
-
"Tip: Clear, high-resolution ECGs with visible lead labels improve P wave and ST-segment assessment."
|
| 190 |
-
)
|
| 191 |
|
| 192 |
-
gr.
|
| 193 |
-
|
| 194 |
-
Model: <code>{MODEL_ID}</code> | Device: <code>{DEVICE}</code>
|
| 195 |
-
</footer>
|
| 196 |
-
""")
|
| 197 |
-
|
| 198 |
-
submit_btn.click(
|
| 199 |
-
fn=analyze_ecg_stream,
|
| 200 |
-
inputs=image_input,
|
| 201 |
-
outputs=output_box,
|
| 202 |
-
queue=True,
|
| 203 |
-
api_name="analyze_ecg",
|
| 204 |
)
|
|
|
|
|
|
|
| 205 |
reset_btn.click(reset, outputs=[image_input, output_box])
|
| 206 |
|
| 207 |
-
def queue_with_compat(demo, max_size=32, limit=4):
|
| 208 |
-
params = inspect.signature(gr.Blocks.queue).parameters
|
| 209 |
-
if "concurrency_count" in params:
|
| 210 |
-
# Older Gradio 3.x / early 4.x
|
| 211 |
-
return demo.queue(concurrency_count=limit, max_size=max_size)
|
| 212 |
-
elif "default_concurrency_limit" in params:
|
| 213 |
-
# Newer Gradio 4.x
|
| 214 |
-
return demo.queue(default_concurrency_limit=limit, max_size=max_size)
|
| 215 |
-
else:
|
| 216 |
-
# Fallback – no knobs exposed
|
| 217 |
-
return demo.queue()
|
| 218 |
-
|
| 219 |
-
# … build your UI as before …
|
| 220 |
-
queue_with_compat(demo, max_size=32, limit=4)
|
| 221 |
demo.launch(share=False, debug=True)
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
+
# Inference (single-shot)
|
| 72 |
# -----------------------------
|
| 73 |
@spaces.GPU
|
| 74 |
+
def analyze_ecg(image: Image.Image):
|
| 75 |
if image is None:
|
| 76 |
+
return "Please upload an ECG image."
|
|
|
|
| 77 |
|
| 78 |
+
# Chat messages (preferred) -> insert image via template
|
| 79 |
messages = [
|
| 80 |
{"role": "user", "content": [
|
| 81 |
{"type": "text", "text": CLINICAL_PROMPT},
|
|
|
|
| 83 |
]}
|
| 84 |
]
|
| 85 |
|
|
|
|
| 86 |
try:
|
| 87 |
chat_text = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 88 |
+
inputs = processor(text=chat_text, images=image, return_tensors="pt")
|
| 89 |
except Exception:
|
| 90 |
+
# Fallback if image-token count/template mismatches
|
| 91 |
+
inputs = processor(text=CLINICAL_PROMPT, images=image, return_tensors="pt")
|
|
|
|
| 92 |
|
| 93 |
+
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 94 |
|
| 95 |
+
try:
|
| 96 |
+
with torch.inference_mode():
|
| 97 |
+
out = model.generate(**inputs, **GEN_KW)
|
| 98 |
+
|
| 99 |
+
# Decode only new tokens if possible
|
| 100 |
+
input_len = inputs.get("input_ids", None)
|
| 101 |
+
if input_len is not None:
|
| 102 |
+
in_len = inputs["input_ids"].shape[-1]
|
| 103 |
+
gen_ids = out[0][in_len:] if out.shape[-1] > in_len else out[0]
|
| 104 |
+
else:
|
| 105 |
+
gen_ids = out[0]
|
| 106 |
+
|
| 107 |
+
text = processor.decode(gen_ids, skip_special_tokens=True).strip()
|
| 108 |
+
return text if text else "[Empty output] Try a clearer ECG image."
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return f"[Generation Error]\n{traceback.format_exc()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
def reset():
|
| 113 |
return None, ""
|
| 114 |
|
| 115 |
# -----------------------------
|
| 116 |
+
# UI (accessible contrast)
|
| 117 |
# -----------------------------
|
| 118 |
theme = gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")
|
| 119 |
+
css = """
|
| 120 |
+
/* High-contrast output area */
|
| 121 |
+
textarea[aria-label="Generated ECG Report"] {
|
| 122 |
+
background-color: #ffffff !important;
|
| 123 |
+
color: #111827 !important; /* near-black */
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
/* Card look */
|
| 127 |
+
.card { background:#fff; border:1px solid #e5e7eb; border-radius:14px; padding:16px; }
|
| 128 |
|
| 129 |
+
/* Buttons */
|
| 130 |
+
.gr-button { background-color:#1e3a8a !important; color:#ffffff !important; }
|
| 131 |
+
|
| 132 |
+
/* Disclaimer with strong contrast */
|
| 133 |
+
.disclaimer {
|
| 134 |
+
margin-top:12px; padding:12px 14px; border-radius:12px;
|
| 135 |
+
background:#fff7ed; color:#7c2d12; border:1px solid #fdba74; font-weight:600;
|
| 136 |
}
|
| 137 |
+
|
| 138 |
+
/* Header */
|
| 139 |
.header {
|
| 140 |
display:flex; align-items:center; justify-content:space-between;
|
| 141 |
+
padding:14px 16px; border-radius:14px; background:#0f172a; color:#e5e7eb;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
}
|
| 143 |
+
.header .brand { font-weight:800; letter-spacing:.2px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
"""
|
| 145 |
|
| 146 |
+
with gr.Blocks(theme=theme, css=css) as demo:
|
| 147 |
+
gr.HTML(f"""
|
| 148 |
+
<div class="header">
|
| 149 |
+
<div class="brand">🫀 ECG Interpretation Assistant</div>
|
| 150 |
+
<div>Model: <code>{MODEL_ID}</code></div>
|
| 151 |
+
</div>
|
| 152 |
+
<div class="disclaimer">
|
| 153 |
+
⚠️ <strong>Education & Research Only:</strong> This tool is not a medical device and must not be used for diagnosis or treatment.
|
| 154 |
+
Always consult a licensed clinician for interpretation and clinical decisions.
|
| 155 |
+
</div>
|
| 156 |
+
""")
|
|
|
|
| 157 |
|
| 158 |
with gr.Row(equal_height=True):
|
| 159 |
with gr.Column(scale=1):
|
| 160 |
with gr.Group(elem_classes="card"):
|
| 161 |
+
image_input = gr.Image(type="pil", label="Upload ECG Image", height=360)
|
|
|
|
|
|
|
| 162 |
with gr.Row():
|
| 163 |
submit_btn = gr.Button("Generate Report", variant="primary")
|
| 164 |
reset_btn = gr.Button("Reset")
|
|
|
|
| 166 |
with gr.Column(scale=2):
|
| 167 |
with gr.Group(elem_classes="card"):
|
| 168 |
output_box = gr.Textbox(
|
| 169 |
+
label="Generated ECG Report",
|
| 170 |
+
lines=28,
|
| 171 |
show_copy_button=True,
|
|
|
|
| 172 |
placeholder="The model's report will appear here…",
|
| 173 |
)
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
gr.Markdown(
|
| 176 |
+
"Tip: Use a high-resolution ECG with visible lead labels to improve assessment of P waves and ST segments."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
)
|
| 178 |
+
|
| 179 |
+
submit_btn.click(analyze_ecg, inputs=image_input, outputs=output_box, queue=False)
|
| 180 |
reset_btn.click(reset, outputs=[image_input, output_box])
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
demo.launch(share=False, debug=True)
|