Spaces:
Runtime error
Runtime error
import gradio as gr | |
class StatusUI: | |
""" | |
A Gradio-based status logger with gr.update(). | |
Use `append(msg)` to update the log, | |
and `shutdown()` to close out the session. | |
""" | |
def __init__(self, title="🖥️ Status Monitor"): | |
self._log = "" | |
self._is_shutdown = False | |
with gr.Blocks() as self._ui: | |
gr.Markdown(f"## {title}") | |
self._log_output = gr.TextArea( | |
label="Log Output", value="(log starts here)", lines=12, interactive=False | |
) | |
self._state = gr.State(self._log) | |
self._shutdown_btn = gr.Button("Shutdown") | |
self._shutdown_btn.click( | |
fn=self._on_shutdown, | |
inputs=self._state, | |
outputs=[self._log_output, self._state] | |
) | |
# Auto-update every second | |
self._ui.load( | |
self._stream_logs, | |
outputs=self._log_output, | |
every=1 | |
) | |
def append(self, message: str): | |
"""Append a message to the UI log.""" | |
self._log += f"\n{message}" | |
self._log_output.update(value=self._log) | |
def shutdown(self): | |
"""Trigger the shutdown sequence manually.""" | |
self._on_shutdown(self._log) | |
def launch(self, **kwargs): | |
"""Launch the UI.""" | |
self._ui.launch(**kwargs) | |
def _on_shutdown(self, current_log): | |
"""Internal shutdown logic.""" | |
if not self._is_shutdown: | |
self._is_shutdown = True | |
current_log += "\n🛑 Shutdown requested." | |
return gr.update(value=current_log), current_log | |
# Usage | |
#ui = StatusUI("🚦 Endpoint Logger") | |
#ui.launch() | |
# Elsewhere in your code: | |
#ui.append("Checking endpoint…") | |
#ui.append("✅ Model responded OK.") | |
#ui.shutdown() | |