Spaces:
Sleeping
Sleeping
Update ui/contentagentui.py
Browse files- ui/contentagentui.py +102 -29
ui/contentagentui.py
CHANGED
|
@@ -1,16 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
from status_check import is_endpoint_healthy
|
| 5 |
-
from endpoint_utils import wake_endpoint
|
| 6 |
|
| 7 |
|
| 8 |
class ContentAgentUI:
|
| 9 |
"""
|
| 10 |
Gradio UI that:
|
| 11 |
- shows a minimal control panel first (status + Start button),
|
| 12 |
-
-
|
| 13 |
-
-
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
def __init__(self, endpoint_uri: str, is_healthy: bool, health_message: str, agent_initializer):
|
|
@@ -19,15 +20,15 @@ class ContentAgentUI:
|
|
| 19 |
self.health_message = health_message or ""
|
| 20 |
self.agent_initializer = agent_initializer # callable: (uri) -> CodeAgent
|
| 21 |
|
| 22 |
-
# set
|
| 23 |
self.app: gr.Blocks | None = None
|
| 24 |
self.status_box = None
|
| 25 |
self.control_panel = None
|
| 26 |
self.main_panel = None
|
| 27 |
self.prompt = None
|
| 28 |
self.reply = None
|
| 29 |
-
self.agent_state = None
|
| 30 |
-
|
| 31 |
|
| 32 |
# ---------- helpers ----------
|
| 33 |
|
|
@@ -42,14 +43,22 @@ class ContentAgentUI:
|
|
| 42 |
return None
|
| 43 |
|
| 44 |
def _initial_status_text(self) -> str:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# ---------- agent call ----------
|
| 55 |
|
|
@@ -69,10 +78,13 @@ class ContentAgentUI:
|
|
| 69 |
return self.app
|
| 70 |
|
| 71 |
css = self._read_css()
|
|
|
|
|
|
|
| 72 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 73 |
gr.Markdown("# Content Agent")
|
| 74 |
|
| 75 |
-
# Control panel (shown first)
|
| 76 |
with gr.Group(visible=True) as self.control_panel:
|
| 77 |
self.status_box = gr.Textbox(
|
| 78 |
label="Status",
|
|
@@ -84,6 +96,17 @@ class ContentAgentUI:
|
|
| 84 |
|
| 85 |
# Main panel (hidden until agent is initialized)
|
| 86 |
with gr.Group(visible=False) as self.main_panel:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
self.agent_state = gr.State(None)
|
| 88 |
self.prompt = gr.Textbox(label="Your Input", placeholder="Enter something here…")
|
| 89 |
self.reply = gr.Textbox(label="Content feedback", interactive=False, lines=12)
|
|
@@ -92,39 +115,89 @@ class ContentAgentUI:
|
|
| 92 |
submit_btn.click(self._call_agent, inputs=[self.prompt, self.agent_state], outputs=self.reply)
|
| 93 |
self.prompt.submit(self._call_agent, inputs=[self.prompt, self.agent_state], outputs=self.reply)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def on_start():
|
| 98 |
-
lines = []
|
| 99 |
-
|
|
|
|
| 100 |
lines.append(s)
|
| 101 |
return ("\n".join(lines), gr.update(), gr.update(), None)
|
| 102 |
-
|
|
|
|
| 103 |
yield push("Waking endpoint… (this can take several minutes for cold starts)")
|
| 104 |
-
ok, err = wake_endpoint(self.endpoint_uri, max_wait=600, poll_every=5.0, log=
|
| 105 |
-
yield
|
| 106 |
-
|
| 107 |
if not ok:
|
| 108 |
yield push(f"[Server message] {err or 'wake failed'}")
|
| 109 |
return
|
| 110 |
-
|
|
|
|
| 111 |
yield push("Endpoint awake ✅. Checking health…")
|
| 112 |
healthy, msg = is_endpoint_healthy(self.endpoint_uri)
|
| 113 |
if not healthy:
|
| 114 |
yield push(f"[Server message] {msg}")
|
| 115 |
return
|
| 116 |
-
|
| 117 |
yield push("Initializing agent…")
|
| 118 |
try:
|
| 119 |
agent = self.agent_initializer(self.endpoint_uri)
|
| 120 |
except Exception as e:
|
| 121 |
yield push(f"Agent init failed: {e}")
|
| 122 |
return
|
| 123 |
-
|
| 124 |
yield ("Agent initialized ✅", gr.update(visible=False), gr.update(visible=True), agent)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
self.app = demo
|
| 130 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
from status_check import is_endpoint_healthy
|
| 5 |
+
from endpoint_utils import wake_endpoint
|
| 6 |
|
| 7 |
|
| 8 |
class ContentAgentUI:
|
| 9 |
"""
|
| 10 |
Gradio UI that:
|
| 11 |
- shows a minimal control panel first (status + Start button),
|
| 12 |
+
- auto-initializes the agent on load if the endpoint is already healthy,
|
| 13 |
+
- otherwise lets the user 'Start Agent' (wake -> health -> init),
|
| 14 |
+
- reveals the main chat panel (with header, guidance, examples, footer) after init.
|
| 15 |
"""
|
| 16 |
|
| 17 |
def __init__(self, endpoint_uri: str, is_healthy: bool, health_message: str, agent_initializer):
|
|
|
|
| 20 |
self.health_message = health_message or ""
|
| 21 |
self.agent_initializer = agent_initializer # callable: (uri) -> CodeAgent
|
| 22 |
|
| 23 |
+
# set in build()
|
| 24 |
self.app: gr.Blocks | None = None
|
| 25 |
self.status_box = None
|
| 26 |
self.control_panel = None
|
| 27 |
self.main_panel = None
|
| 28 |
self.prompt = None
|
| 29 |
self.reply = None
|
| 30 |
+
self.agent_state = None
|
| 31 |
+
self.examples_radio = None
|
| 32 |
|
| 33 |
# ---------- helpers ----------
|
| 34 |
|
|
|
|
| 43 |
return None
|
| 44 |
|
| 45 |
def _initial_status_text(self) -> str:
|
| 46 |
+
# neutral; on_load will set real status and maybe auto-init
|
| 47 |
+
return "Checking endpoint status…"
|
| 48 |
+
|
| 49 |
+
def _load_examples(self) -> list[str]:
|
| 50 |
+
ex_dir = os.path.join(os.path.dirname(__file__), "examples")
|
| 51 |
+
out: list[str] = []
|
| 52 |
+
if os.path.isdir(ex_dir):
|
| 53 |
+
for name in sorted(os.listdir(ex_dir)):
|
| 54 |
+
if name.lower().endswith(".txt"):
|
| 55 |
+
p = os.path.join(ex_dir, name)
|
| 56 |
+
try:
|
| 57 |
+
with open(p, "r", encoding="utf-8", errors="ignore") as f:
|
| 58 |
+
out.append(f.read())
|
| 59 |
+
except Exception:
|
| 60 |
+
pass
|
| 61 |
+
return out
|
| 62 |
|
| 63 |
# ---------- agent call ----------
|
| 64 |
|
|
|
|
| 78 |
return self.app
|
| 79 |
|
| 80 |
css = self._read_css()
|
| 81 |
+
examples = self._load_examples()
|
| 82 |
+
|
| 83 |
with gr.Blocks(css=css) as demo:
|
| 84 |
+
# global header (always visible)
|
| 85 |
gr.Markdown("# Content Agent")
|
| 86 |
|
| 87 |
+
# Control panel (shown first; may auto-hide on load)
|
| 88 |
with gr.Group(visible=True) as self.control_panel:
|
| 89 |
self.status_box = gr.Textbox(
|
| 90 |
label="Status",
|
|
|
|
| 96 |
|
| 97 |
# Main panel (hidden until agent is initialized)
|
| 98 |
with gr.Group(visible=False) as self.main_panel:
|
| 99 |
+
# Guidance / about
|
| 100 |
+
gr.Markdown(
|
| 101 |
+
"### What this does\n"
|
| 102 |
+
"Enter text below; the agent will classify tone (polite/neutral/impolite) and can use tools.\n\n"
|
| 103 |
+
"**Tech:**\n"
|
| 104 |
+
"- `deepseek-ai/DeepSeek-R1-Distill-Qwen-32B` (endpoint)\n"
|
| 105 |
+
"- Intel Polite Guard tool\n"
|
| 106 |
+
"- Runs via your Inference Endpoint\n"
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Chat controls
|
| 110 |
self.agent_state = gr.State(None)
|
| 111 |
self.prompt = gr.Textbox(label="Your Input", placeholder="Enter something here…")
|
| 112 |
self.reply = gr.Textbox(label="Content feedback", interactive=False, lines=12)
|
|
|
|
| 115 |
submit_btn.click(self._call_agent, inputs=[self.prompt, self.agent_state], outputs=self.reply)
|
| 116 |
self.prompt.submit(self._call_agent, inputs=[self.prompt, self.agent_state], outputs=self.reply)
|
| 117 |
|
| 118 |
+
# Examples (optional)
|
| 119 |
+
gr.Markdown("### Try one of these examples")
|
| 120 |
+
if examples:
|
| 121 |
+
self.examples_radio = gr.Radio(choices=examples, label="Examples")
|
| 122 |
+
# fill the prompt when an example is picked
|
| 123 |
+
self.examples_radio.change(lambda ex: ex, inputs=self.examples_radio, outputs=self.prompt)
|
| 124 |
+
else:
|
| 125 |
+
gr.Markdown("*No examples found.*")
|
| 126 |
+
|
| 127 |
+
# Footer
|
| 128 |
+
gr.Markdown("<div id='footer'>Thanks for trying it out!</div>")
|
| 129 |
+
|
| 130 |
+
# --- AUTO INIT ON LOAD IF HEALTHY ---
|
| 131 |
+
def on_load():
|
| 132 |
+
healthy, msg = is_endpoint_healthy(self.endpoint_uri)
|
| 133 |
+
if healthy:
|
| 134 |
+
try:
|
| 135 |
+
agent = self.agent_initializer(self.endpoint_uri)
|
| 136 |
+
return (
|
| 137 |
+
f"Endpoint healthy ✅ — {msg}. Agent initialized.",
|
| 138 |
+
gr.update(visible=False), # hide control panel
|
| 139 |
+
gr.update(visible=True), # show main panel
|
| 140 |
+
agent,
|
| 141 |
+
)
|
| 142 |
+
except Exception as e:
|
| 143 |
+
return (
|
| 144 |
+
f"Agent init failed: {e}",
|
| 145 |
+
gr.update(visible=True),
|
| 146 |
+
gr.update(visible=False),
|
| 147 |
+
None,
|
| 148 |
+
)
|
| 149 |
+
# not healthy → keep Start button path
|
| 150 |
+
return (
|
| 151 |
+
f"Endpoint not healthy: {msg}\nClick 'Start Agent' to wake and initialize.",
|
| 152 |
+
gr.update(visible=True),
|
| 153 |
+
gr.update(visible=False),
|
| 154 |
+
None,
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
demo.load(
|
| 158 |
+
on_load,
|
| 159 |
+
inputs=None,
|
| 160 |
+
outputs=[self.status_box, self.control_panel, self.main_panel, self.agent_state],
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# --- MANUAL START (wake → health → init) ---
|
| 164 |
def on_start():
|
| 165 |
+
lines: list[str] = []
|
| 166 |
+
|
| 167 |
+
def push(s: str):
|
| 168 |
lines.append(s)
|
| 169 |
return ("\n".join(lines), gr.update(), gr.update(), None)
|
| 170 |
+
|
| 171 |
+
# Wake with progress
|
| 172 |
yield push("Waking endpoint… (this can take several minutes for cold starts)")
|
| 173 |
+
ok, err = wake_endpoint(self.endpoint_uri, max_wait=600, poll_every=5.0, log=lines.append)
|
| 174 |
+
yield ("\n".join(lines), gr.update(), gr.update(), None) # flush all logs
|
| 175 |
+
|
| 176 |
if not ok:
|
| 177 |
yield push(f"[Server message] {err or 'wake failed'}")
|
| 178 |
return
|
| 179 |
+
|
| 180 |
+
# Health → init
|
| 181 |
yield push("Endpoint awake ✅. Checking health…")
|
| 182 |
healthy, msg = is_endpoint_healthy(self.endpoint_uri)
|
| 183 |
if not healthy:
|
| 184 |
yield push(f"[Server message] {msg}")
|
| 185 |
return
|
| 186 |
+
|
| 187 |
yield push("Initializing agent…")
|
| 188 |
try:
|
| 189 |
agent = self.agent_initializer(self.endpoint_uri)
|
| 190 |
except Exception as e:
|
| 191 |
yield push(f"Agent init failed: {e}")
|
| 192 |
return
|
| 193 |
+
|
| 194 |
yield ("Agent initialized ✅", gr.update(visible=False), gr.update(visible=True), agent)
|
| 195 |
|
| 196 |
+
start_btn.click(
|
| 197 |
+
on_start,
|
| 198 |
+
inputs=None,
|
| 199 |
+
outputs=[self.status_box, self.control_panel, self.main_panel, self.agent_state],
|
| 200 |
+
)
|
| 201 |
|
| 202 |
self.app = demo
|
| 203 |
|