Upload app.py with huggingface_hub
Browse files10 unmodified lines11121314151617360 unmodified lines37837938038138238338438538638738838939039139239339439539639739839940040119 unmodified lines4214224234244254264275 unmodified lines4334344354364374384391 unmodified line4414424434444454464474 unmodified lines4524534544554564574584594604 unmodified lines46546646746846947047147247347447547647747810 unmodified lines48949049149249349449512 unmodified lines508509510511512513514515516517518489 unmodified lines100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421 unmodified line104410451046104710481049105010 unmodified linesimport torchimport gradio as grfrom _charts import VAL_B64, AUC_B64, AMD_BENCH_B64_WINDOW_LEN = 50_STEP = 3360 unmodified linesfor s, e in zip(_nb[:-1], _nb[1:])])fig = plt.figure(figsize=(18, 5.5))fig.patch.set_facecolor("#0e1015")axes = [fig.add_subplot(1, 3, 1),fig.add_subplot(1, 3, 2),fig.add_subplot(1, 3, 3, projection="3d"),]# ── Left: 7×7 network heatmap ──────────────────────────────────────────ax = axes[0]ax.set_facecolor("#161922")im = ax.imshow(net_sal, cmap="inferno", aspect="auto", interpolation="nearest")ax.set_title("FC Saliency by Brain Network", color="#bbb", fontsize=11, pad=14, fontweight="bold")ax.set_xticks(range(n_nets))ax.set_yticks(range(n_nets))ax.set_xticklabels(_nn, rotation=40, ha="right", fontsize=9, color="#ccc")ax.set_yticklabels(_nn, fontsize=9, color="#ccc")ax.tick_params(colors="#555", length=0)for sp in ax.spines.values():sp.set_color("#222")19 unmodified linesfor j in range(n_nets):txt_color = "#111" if net_sal[i, j] > 0.6 * vmax else "#666"ax.text(j, i, f"{net_sal[i, j]:.3f}", ha="center", va="center",fontsize=6.5, color=txt_color, zorder=3)if (i, j) in top5_cells:rect = plt.Rectangle((j - 0.48, i - 0.48), 0.96, 0.96,linewidth=1.8, edgecolor="#ffffff",5 unmodified lineslabel = f"#{rank+1} {_nn[i]}↔{_nn[j]}"ax.annotate(label,xy=(j, i), xytext=(n_nets - 0.3, rank * 0.85 - 0.3),fontsize=6, color="#fb923c", fontweight="600",arrowprops=dict(arrowstyle="-", color="#fb923c",lw=0.7, connectionstyle="arc3,rad=0.1"),ha="left", va="center", zorder=5)1 unmodified linecb = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)cb.ax.yaxis.set_tick_params(color="#444", labelsize=7)plt.setp(cb.ax.yaxis.get_ticklabels(), color="#555")cb.set_label("Mean |∂p(ASD)/∂FC|", color="#444", fontsize=7.5)# ── Right: network importance bar chart ────────────────────────────────ax2 = axes[1]4 unmodified linesbars = ax2.barh(range(n_nets), net_imp[order],color=[_nc[i] for i in order], alpha=0.88, edgecolor="none", height=0.65)ax2.set_yticks(range(n_nets))ax2.set_yticklabels([_nn[i] for i in order], fontsize=9.5, color="#ddd")ax2.set_xlabel("Mean gradient magnitude", color="#555", fontsize=9)ax2.set_title("Network Importance for This Prediction", color="#bbb", fontsize=11, pad=14, fontweight="bold")ax2.invert_yaxis()for sp in ["top", "right"]:ax2.spines[sp].set_visible(False)4 unmodified linesx_max = net_imp.max()for bar, val in zip(bars, net_imp[order]):ax2.text(val + x_max * 0.015, bar.get_y() + bar.get_height() / 2,f"{val:.4f}", va="center", color="#555", fontsize=7.5)# ── 3D Brain Surface — top connections ────────────────────────────────────ax3 = axes[2]ax3.set_facecolor("#0e1015")ax3.grid(False)ax3.set_axis_off()ax3.set_title("Top Connections · 3D Brain", color="#bbb", fontsize=11, pad=4, fontweight="bold")# Transparent brain ellipsoid wireframe (MNI space approx)u = np.linspace(0, 2 * np.pi, 32)10 unmodified linessize = 60 + imp_norm[k] * 260ax3.scatter([x], [y], [z], c=color, s=size, zorder=5,edgecolors="#ffffff", linewidths=0.5, alpha=0.92)ax3.text(x, y, z + 7, name, fontsize=5.5, color=color,ha="center", va="bottom", fontweight="600", zorder=6)# Draw top-5 inter-network connections as lines, thickness ∝ saliency12 unmodified linesfig.suptitle(f"Gradient Saliency · p(ASD) = {p_mean:.3f} · 20-model LOSO ensemble · CC200 → Yeo-7 networks",color="#444", fontsize=8.5, y=1.02,)plt.tight_layout()buf = io.BytesIO()plt.savefig(buf, format="png", dpi=140, bbox_inches="tight", facecolor="#0e1015")plt.close(fig)buf.seek(0)return Image.open(buf).copy()489 unmodified lines</div>"""AMD = f"""<div><!-- Benchmark chart first — most impressive thing --><img src="data:image/png;base64,{AMD_BENCH_B64}" style="width:100%;border-radius:8px;margin-bottom:20px;border:1px solid #252a35"/><!-- Two-column layout: stat grid left, pipeline right --><div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:18px"><div style="background:#161922;border:1px solid #252a35;border-radius:8px;padding:18px 20px"><div style="color:#8b95a7;font-size:0.68rem;text-transform:uppercase;letter-spacing:1.5px;margin-bottom:14px;font-weight:500">Hardware</div><div style="display:grid;grid-template-columns:1fr 1fr;gap:14px"><div><div style="font-size:1.5rem;font-weight:700;color:#fb923c;font-variant-numeric:tabular-nums">192<span style="font-size:0.75rem;color:#5e6675;font-weight:400"> GB</span></div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">HBM3 unified mem</div></div><div><div style="font-size:1.5rem;font-weight:700;color:#fb923c">bf16</div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">Full precision</div></div><div><div style="font-size:1.5rem;font-weight:700;color:#fb923c">30×</div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">Faster than CPU</div></div><div><div style="font-size:1.5rem;font-weight:700;color:#fb923c">94ms</div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">Per subject</div></div></div></div><div style="background:#161922;border:1px solid #252a35;border-radius:8px;padding:18px 20px"><div style="color:#8b95a7;font-size:0.68rem;text-transform:uppercase;letter-spacing:1.5px;margin-bottom:14px;font-weight:500">LoRA Fine-Tune</div><div style="display:grid;grid-template-columns:1fr 1fr;gap:14px"><div><div style="font-size:1.5rem;font-weight:700;color:#f4f4f5">7B</div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">Qwen2.5 params</div></div><div><div style="font-size:1.5rem;font-weight:700;color:#f4f4f5">r=16</div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">LoRA rank</div></div><div><div style="font-size:1.5rem;font-weight:700;color:#f4f4f5">2K</div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">Domain examples</div></div><div><div style="font-size:1.5rem;font-weight:700;color:#f4f4f5">3</div><div style="color:#8b95a7;font-size:0.68rem;margin-top:3px;text-transform:uppercase;letter-spacing:0.8px">Epochs</div></div></div></div></div><!-- Fine-tune spec table -->1 unmodified line<table style="width:100%;border-collapse:collapse;font-size:0.85rem"><tr><td style="padding:10px 16px;color:#8b95a7;width:150px;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">Base model</td><td style="padding:10px 16px;color:#cbd5e1">Qwen/Qwen2.5-7B-Instruct <span style="color:#5e6675">· AMD partner model · ROCm native</span></td></tr><tr style="border-top:1px solid #252a35"><td style="padding:10px 16px;color:#8b95a7;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">Method</td><td style="padding:10px 16px;color:#cbd5e1">LoRA r=16 α=32 · q, k, v, o, gate, up, down projections · bf16 — no quantization needed</td></tr><tr style="border-top:1px solid #252a35"><td style="padding:10px 16px;color:#8b95a7;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">Training task</td><td style="padding:10px 16px;color:#cbd5e1">GCN ensemble output → structured clinical referral letter with ICD-10 codes</td></tr><tr style="border-top:1px solid #252a35"><td style="padding:10px 16px;color:#8b95a7;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">Why MI300X?</td><td style="padding:10px 16px;color:#cbd5e1">192 GB unified HBM3 fits the full 7B model in bf16 without sharding — impossible on consumer GPUs. ROCm enables native PyTorch training with zero code changes.</td></tr></table></div>10 unmodified lines11121314151617360 unmodified lines37837938038138238338438538638738838939039139239339439539639739839940040140240340419 unmodified lines4244254264274284294305 unmodified lines4364374384394404414421 unmodified line4444454464474484494504 unmodified lines4554564574584594604614624634 unmodified lines46846947047147247347447547647747847948048110 unmodified lines49249349449549649749812 unmodified lines511512513514515516517518519520489 unmodified lines101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591 unmodified line1061106210631064106510661067106810 unmodified linesimport torchimport gradio as grfrom _charts import VAL_B64, AUC_B64_WINDOW_LEN = 50_STEP = 3360 unmodified linesfor s, e in zip(_nb[:-1], _nb[1:])])fig = plt.figure(figsize=(20, 18))fig.patch.set_facecolor("#0e1015")import matplotlib.gridspec as gridspecgs = gridspec.GridSpec(2, 2, figure=fig, hspace=0.38, wspace=0.32,height_ratios=[1.1, 1.0])axes = [fig.add_subplot(gs[0, 0]), # heatmapfig.add_subplot(gs[0, 1]), # bar chartfig.add_subplot(gs[1, :], projection="3d"), # 3D brain — full bottom row]# ── Left: 7×7 network heatmap ──────────────────────────────────────────ax = axes[0]ax.set_facecolor("#161922")im = ax.imshow(net_sal, cmap="inferno", aspect="auto", interpolation="nearest")ax.set_title("FC Saliency by Brain Network", color="#bbb", fontsize=14, pad=16, fontweight="bold")ax.set_xticks(range(n_nets))ax.set_yticks(range(n_nets))ax.set_xticklabels(_nn, rotation=40, ha="right", fontsize=12, color="#ccc")ax.set_yticklabels(_nn, fontsize=12, color="#ccc")ax.tick_params(colors="#555", length=0)for sp in ax.spines.values():sp.set_color("#222")19 unmodified linesfor j in range(n_nets):txt_color = "#111" if net_sal[i, j] > 0.6 * vmax else "#666"ax.text(j, i, f"{net_sal[i, j]:.3f}", ha="center", va="center",fontsize=9, color=txt_color, zorder=3)if (i, j) in top5_cells:rect = plt.Rectangle((j - 0.48, i - 0.48), 0.96, 0.96,linewidth=1.8, edgecolor="#ffffff",5 unmodified lineslabel = f"#{rank+1} {_nn[i]}↔{_nn[j]}"ax.annotate(label,xy=(j, i), xytext=(n_nets - 0.3, rank * 0.85 - 0.3),fontsize=8.5, color="#fb923c", fontweight="600",arrowprops=dict(arrowstyle="-", color="#fb923c",lw=0.7, connectionstyle="arc3,rad=0.1"),ha="left", va="center", zorder=5)1 unmodified linecb = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)cb.ax.yaxis.set_tick_params(color="#444", labelsize=7)plt.setp(cb.ax.yaxis.get_ticklabels(), color="#555")cb.set_label("Mean |∂p(ASD)/∂FC|", color="#444", fontsize=10)# ── Right: network importance bar chart ────────────────────────────────ax2 = axes[1]4 unmodified linesbars = ax2.barh(range(n_nets), net_imp[order],color=[_nc[i] for i in order], alpha=0.88, edgecolor="none", height=0.65)ax2.set_yticks(range(n_nets))ax2.set_yticklabels([_nn[i] for i in order], fontsize=12, color="#ddd")ax2.set_xlabel("Mean gradient magnitude", color="#555", fontsize=11)ax2.set_title("Network Importance for This Prediction", color="#bbb", fontsize=14, pad=16, fontweight="bold")ax2.invert_yaxis()for sp in ["top", "right"]:ax2.spines[sp].set_visible(False)4 unmodified linesx_max = net_imp.max()for bar, val in zip(bars, net_imp[order]):ax2.text(val + x_max * 0.015, bar.get_y() + bar.get_height() / 2,f"{val:.4f}", va="center", color="#555", fontsize=10)# ── 3D Brain Surface — top connections ────────────────────────────────────ax3 = axes[2]ax3.set_facecolor("#0e1015")ax3.grid(False)ax3.set_axis_off()ax3.set_title("Top Connections · 3D Brain", color="#bbb", fontsize=14, pad=8, fontweight="bold")# Transparent brain ellipsoid wireframe (MNI space approx)u = np.linspace(0, 2 * np.pi, 32)10 unmodified linessize = 60 + imp_norm[k] * 260ax3.scatter([x], [y], [z], c=color, s=size, zorder=5,edgecolors="#ffffff", linewidths=0.5, alpha=0.92)ax3.text(x, y, z + 7, name, fontsize=8, color=color,ha="center", va="bottom", fontweight="600", zorder=6)# Draw top-5 inter-network connections as lines, thickness ∝ saliency12 unmodified linesfig.suptitle(f"Gradient Saliency · p(ASD) = {p_mean:.3f} · 20-model LOSO ensemble · CC200 → Yeo-7 networks",color="#888", fontsize=12, y=1.01,)buf = io.BytesIO()plt.savefig(buf, format="png", dpi=120, bbox_inches="tight", facecolor="#0e1015")plt.close(fig)buf.seek(0)return Image.open(buf).copy()489 unmodified lines</div>"""AMD = """<div><!-- Stat grid: real benchmarked numbers --><div style="display:grid;grid-template-columns:repeat(4,1fr);gap:14px;margin-bottom:18px"><div style="background:#161922;border:1px solid #252a35;border-radius:8px;padding:18px 16px;text-align:center"><div style="font-size:1.8rem;font-weight:700;color:#fb923c;font-variant-numeric:tabular-nums">~20<span style="font-size:0.8rem;color:#5e6675;font-weight:400"> ms</span></div><div style="color:#8b95a7;font-size:0.67rem;margin-top:5px;text-transform:uppercase;letter-spacing:0.8px">End-to-end per patient</div><div style="color:#5e6675;font-size:0.64rem;margin-top:3px">preprocess + 20-model ensemble</div></div><div style="background:#161922;border:1px solid #252a35;border-radius:8px;padding:18px 16px;text-align:center"><div style="font-size:1.8rem;font-weight:700;color:#fb923c">192<span style="font-size:0.8rem;color:#5e6675;font-weight:400"> GB</span></div><div style="color:#8b95a7;font-size:0.67rem;margin-top:5px;text-transform:uppercase;letter-spacing:0.8px">HBM3 unified memory</div><div style="color:#5e6675;font-size:0.64rem;margin-top:3px">7B model fits in bf16, no sharding</div></div><div style="background:#161922;border:1px solid #252a35;border-radius:8px;padding:18px 16px;text-align:center"><div style="font-size:1.8rem;font-weight:700;color:#fb923c">60</div><div style="color:#8b95a7;font-size:0.67rem;margin-top:5px;text-transform:uppercase;letter-spacing:0.8px">Models trained on MI300X</div><div style="color:#5e6675;font-size:0.64rem;margin-top:3px">20 folds × 3 atlases · 150 epochs each</div></div><div style="background:#161922;border:1px solid #252a35;border-radius:8px;padding:18px 16px;text-align:center"><div style="font-size:1.8rem;font-weight:700;color:#fb923c">ROCm<span style="font-size:0.8rem;color:#5e6675;font-weight:400"> 7.0</span></div><div style="color:#8b95a7;font-size:0.67rem;margin-top:5px;text-transform:uppercase;letter-spacing:0.8px">Native PyTorch stack</div><div style="color:#5e6675;font-size:0.64rem;margin-top:3px">zero code changes from CUDA</div></div></div><!-- How AMD is used --><div style="background:#161922;border:1px solid #252a35;border-radius:8px;padding:18px 20px;margin-bottom:14px"><div style="color:#8b95a7;font-size:0.68rem;text-transform:uppercase;letter-spacing:1.5px;margin-bottom:14px;font-weight:500">AMD MI300X Usage in This Project</div><div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:16px"><div><div style="color:#fb923c;font-size:0.78rem;font-weight:600;margin-bottom:6px">① GCN Training</div><div style="color:#cbd5e1;font-size:0.82rem;line-height:1.55">60 site-holdout models trained via PyTorch Lightning on ROCm 7.0. K=32 modes, 150 epochs, 3 atlases (CC200, AAL, HO).</div></div><div><div style="color:#fb923c;font-size:0.78rem;font-weight:600;margin-bottom:6px">② LLM Fine-Tuning</div><div style="color:#cbd5e1;font-size:0.82rem;line-height:1.55">Qwen2.5-7B fine-tuned with LoRA (r=16, bf16) on 2K domain examples. 192GB HBM3 fits the full model without sharding.</div></div><div><div style="color:#fb923c;font-size:0.78rem;font-weight:600;margin-bottom:6px">③ Live LLM Inference</div><div style="color:#cbd5e1;font-size:0.82rem;line-height:1.55">Clinical reports generated in real-time via vLLM on the MI300X droplet. Every report you see is live AMD inference.</div></div></div></div><!-- Fine-tune spec table -->1 unmodified line<table style="width:100%;border-collapse:collapse;font-size:0.85rem"><tr><td style="padding:10px 16px;color:#8b95a7;width:150px;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">Base model</td><td style="padding:10px 16px;color:#cbd5e1">Qwen/Qwen2.5-7B-Instruct <span style="color:#5e6675">· AMD partner model · ROCm native</span></td></tr><tr style="border-top:1px solid #252a35"><td style="padding:10px 16px;color:#8b95a7;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">Method</td><td style="padding:10px 16px;color:#cbd5e1">LoRA r=16 α=32 · q, k, v, o, gate, up, down projections · bf16 — no quantization needed</td></tr><tr style="border-top:1px solid #252a35"><td style="padding:10px 16px;color:#8b95a7;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">GCN inference</td><td style="padding:10px 16px;color:#cbd5e1">~20ms end-to-end per patient · benchmarked on AMD MI300X · ROCm 7.0 · PyTorch 2.5.1</td></tr><tr style="border-top:1px solid #252a35"><td style="padding:10px 16px;color:#8b95a7;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">LLM serving</td><td style="padding:10px 16px;color:#cbd5e1">vLLM on AMD MI300X · OpenAI-compatible API · live inference for every clinical report</td></tr><tr style="border-top:1px solid #252a35"><td style="padding:10px 16px;color:#8b95a7;font-size:0.76rem;text-transform:uppercase;letter-spacing:0.5px">Why MI300X?</td><td style="padding:10px 16px;color:#cbd5e1">192 GB unified HBM3 fits the full 7B model in bf16 without sharding — impossible on consumer GPUs. ROCm enables native PyTorch training with zero code changes.</td></tr></table></div>
app.py+62-44