Spaces:
Sleeping
Sleeping
File size: 7,256 Bytes
14aaa25 caf9410 d18fef3 3cf2649 712f300 14aaa25 6e48304 d18fef3 2a26805 3cf2649 2a26805 14aaa25 eefc6a7 14aaa25 2a26805 14aaa25 eefc6a7 14aaa25 eefc6a7 14aaa25 eefc6a7 14aaa25 6fa1fee 14aaa25 2a26805 3cf2649 d18fef3 14aaa25 d18fef3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | """
Gradio CDSS Simulator for RSIA (Obstetri, Neonatologi, Ginekologi)
FINAL INTEGRATED VERSION - Now with:
- **Custom Plotly graphs** using gr.Plot for full control.
- **X-Axis shows only the first and last timestamps.**
- Tabbed plots for each vital sign, guaranteeing graph rendering.
- Historic Clinical Text, 30s countdown, CSV loader, and all scenarios.
Requirements:
pip install gradio google-generativeai pandas plotly
Set environment variable for Gemini:
export GOOGLE_API_KEY=your_key_here
Run:
python app.py
"""
from __future__ import annotations
import time
import gradio as gr
import pandas as pd
from models import Vitals, PatientState
from simulator import (
simulator_ui,
inject_scenario,
manual_edit,
tick_timer,
load_csv,
countdown_tick,
SCENARIOS,
)
from editor import editor_ui, save_rules
from validator import validator_ui, test_condition, add_rule_to_set
from diagnosis import diagnosis_ui, generate_diagnosis, check_medication_interaction, generate_medical_record_from_image
# --- Build UI ---
with gr.Blocks(
css=".gradio-container { max-width: 1200px !important; margin: auto !important; }"
) as demo:
gr.Markdown("# RSIA CDSS Simulator with Custom Plotly Graphs")
state = gr.State({})
history_df = gr.State(pd.DataFrame())
historic_text = gr.State("")
last_tick_ts = gr.State(time.time())
interpretation = gr.Textbox(label="CDSS Interpretation", lines=2, interactive=False)
with gr.Tabs():
bp_plot, hr_plot, rr_plot, temp_plot, spo2_plot = simulator_ui()
(df_mother, df_neonate, df_gyn, save_button, status_textbox) = editor_ui()
(
patient_type_validate,
sbp_validate,
dbp_validate,
hr_validate,
rr_validate,
temp_c_validate,
spo2_validate,
labs_validate,
condition_validate,
alert_validate,
test_button,
validation_result,
add_rule_button,
add_rule_status,
) = validator_ui()
(
generate_button,
diagnosis_output,
medication_output,
medication_input,
check_button,
interaction_output,
image_input,
generate_from_image_button,
medical_record_output,
) = diagnosis_ui()
with gr.Row():
with gr.Column(scale=2):
with gr.Accordion("Inject New Scenario (resets chart)", open=True):
with gr.Row():
btn_A0 = gr.Button("A0: Normal", elem_classes="small-btn")
btn_A1 = gr.Button("A1: PPH", elem_classes="small-btn")
btn_A2 = gr.Button("A2: Preeklampsia", elem_classes="small-btn")
btn_A3 = gr.Button("A3: Sepsis", elem_classes="small-btn")
with gr.Row():
btn_B1 = gr.Button("B1: Prematuritas", elem_classes="small-btn")
btn_B2 = gr.Button("B2: Asfiksia", elem_classes="small-btn")
btn_B3 = gr.Button("B3: Sepsis", elem_classes="small-btn")
with gr.Row():
btn_C1 = gr.Button("C1: Bedah Komplikasi", elem_classes="small-btn")
btn_C2 = gr.Button(
"C2: Infeksi Pasca-Bedah", elem_classes="small-btn"
)
btn_C3 = gr.Button("C3: Kanker", elem_classes="small-btn")
notes = gr.Textbox(label="Catatan Klinis", lines=2)
labs_text = gr.Textbox(label="Lab (dict or text)", value="{}")
labs_show = gr.Textbox(label="Labs (Parsed)", interactive=False)
with gr.Column(scale=1):
with gr.Group():
patient_type_radio = gr.Radio(
["Mother", "Neonate", "Gyn"], label="Patient Type", value="Mother"
)
sbp = gr.Number(label="SBP")
dbp = gr.Number(label="DBP")
hr = gr.Number(label="HR")
rr = gr.Number(label="RR")
temp_c = gr.Number(label="Temp (°C)")
spo2 = gr.Number(label="SpO₂ (%)")
apply_manual = gr.Button("Apply Manual Edits", variant="secondary")
with gr.Row():
csv_file = gr.File(label="Load CSV to Graph")
df_view = gr.Dataframe(label="History Table", wrap=True, interactive=False)
cdss_toggle = gr.Checkbox(value=False, label="With CDSS (Gemini)")
scenario_lbl = gr.Textbox(label="Active Scenario", interactive=False)
countdown_lbl = gr.Label()
historic_box = gr.Textbox(label="Historic Text", lines=12, interactive=False)
# --- Event Handlers ---
def update_medication_input(patient_type):
if patient_type == "Mother":
return gr.update(value="Aspirin, Ibuprofen")
elif patient_type == "Gyn":
return gr.update(value="Clopidogrel, Omeprazole")
elif patient_type == "Neonate":
return gr.update(value="Ceftriaxone, Calcium")
patient_type_radio.change(update_medication_input, inputs=patient_type_radio, outputs=medication_input)
ui_outputs = [
state,
scenario_lbl,
patient_type_radio,
notes,
sbp,
dbp,
hr,
rr,
temp_c,
spo2,
labs_text,
labs_show,
interpretation,
history_df,
df_view,
historic_box,
last_tick_ts,
bp_plot,
hr_plot,
rr_plot,
temp_plot,
spo2_plot,
]
manual_inputs = [
sbp,
dbp,
hr,
rr,
temp_c,
spo2,
notes,
labs_text,
cdss_toggle,
patient_type_radio,
state,
history_df,
historic_text,
]
apply_manual.click(manual_edit, manual_inputs, ui_outputs)
for tag, btn in zip(
SCENARIOS.keys(),
[
btn_A0,
btn_A1,
btn_A2,
btn_A3,
btn_B1,
btn_B2,
btn_B3,
btn_C1,
btn_C2,
btn_C3,
],
):
btn.click(
inject_scenario,
[gr.State(tag), cdss_toggle, history_df, historic_text],
ui_outputs,
)
csv_outputs = [history_df, df_view, bp_plot, hr_plot, rr_plot, temp_plot, spo2_plot]
csv_file.change(load_csv, [csv_file, history_df], csv_outputs)
timer_inputs = [cdss_toggle, state, history_df, historic_text]
gr.Timer(30.0).tick(tick_timer, timer_inputs, ui_outputs)
gr.Timer(1.0).tick(countdown_tick, [last_tick_ts], [countdown_lbl])
generate_button.click(generate_diagnosis, inputs=state, outputs=[diagnosis_output, medication_output])
check_button.click(check_medication_interaction, inputs=[patient_type_radio, medication_input], outputs=interaction_output)
generate_from_image_button.click(
generate_medical_record_from_image,
inputs=[state, image_input],
outputs=medical_record_output
)
demo.load(inject_scenario, [gr.State("A0"), cdss_toggle, history_df, historic_text], ui_outputs)
if __name__ == "__main__":
demo.launch() |