File size: 1,840 Bytes
3aef5a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b83ec1
 
 
 
 
 
 
3aef5a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()