Spaces:
Sleeping
Sleeping
| 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] | |
| ) | |
| def append(self, message: str): | |
| print(message) | |
| if not self._is_shutdown: | |
| # Add message + force UI update | |
| self._log += f"\n {message}" | |
| self._log_output = gr.TextArea.update( | |
| value=self._log, | |
| # Optional: Ensure visual consistency | |
| interactive=False, | |
| autoscroll=True | |
| ) | |
| 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() | |