import gradio as gr import numpy as np import matplotlib.pyplot as plt def calculate_and_plot(t1, a1, t2, a2): try: # الحسابات الأساسية k = np.log(a1 / a2) / (t2 - t1) a0 = a1 / np.exp(-k * t1) tia = a0 / k result = f""" ### ✅ Results - **A₀ (initial activity):** {a0:.2f} MBq - **k (washout rate):** {k:.5f} / hour - **TIA:** {tia:.2f} MBq·h """ # إنشاء نقاط الرسم t_points = np.linspace(0, max(t2 + 40, 200), 100) a_points = a0 * np.exp(-k * t_points) # رسم المنحنى باستخدام matplotlib fig, ax = plt.subplots(figsize=(6, 4)) ax.plot(t_points, a_points, label="Fitted Curve", color='blue') ax.scatter([t1, t2], [a1, a2], color='red', label="Input Points") ax.set_xlabel("Time (hours)") ax.set_ylabel("Activity (MBq)") ax.set_title("Mono-Exponential Washout Curve") ax.grid(True) ax.legend() plt.tight_layout() return result, fig except Exception: return "⚠️ Error: Please check your inputs.", None with gr.Blocks() as demo: gr.Markdown("# ⚛️ Lu-177 Dosimetry Tool\nEstimate Time-Integrated Activity (TIA) from Two SPECT Time Points") with gr.Row(): t1 = gr.Number(label="Time Point 1 (hours)", value=20) a1 = gr.Number(label="Activity at Time 1 (MBq)", value=12) with gr.Row(): t2 = gr.Number(label="Time Point 2 (hours)", value=60) a2 = gr.Number(label="Activity at Time 2 (MBq)", value=4) output_text = gr.Markdown() output_plot = gr.Plot() button = gr.Button("Calculate & Plot TIA") button.click(fn=calculate_and_plot, inputs=[t1, a1, t2, a2], outputs=[output_text, output_plot]) gr.Markdown(""" --- ### ℹ️ What Is This Tool? This open-source calculator estimates **Time-Integrated Activity (TIA)** for patients receiving **Lutetium-177 PSMA therapy** for metastatic prostate cancer. It uses **two post-treatment SPECT/CT time points** to fit a simplified **mono-exponential washout model**, helping clinicians perform dose calculations with minimal imaging. --- ### ⚙️ How It Works You enter two scan times (hours) and their corresponding measured activity (MBq). The tool automatically: - Fits an exponential time–activity curve - Calculates the washout rate (_k_) - Estimates initial activity (_A₀_) - Computes **TIA = A₀ / k**, which can be used in absorbed dose estimation --- ### 🧠 Scientific Basis Model used: > A(t) = A₀ · exp(–k · t) > TIA = A₀ / k This approach is ideal for organs with regular clearance kinetics (e.g., kidneys) or tumors with predictable washout, and is based on principles used in MIRD dosimetry and EANM guidelines. --- """) demo.launch()