Spaces:
Sleeping
Sleeping
Update ui/contentagentui.py
Browse files- ui/contentagentui.py +119 -102
ui/contentagentui.py
CHANGED
|
@@ -1,117 +1,134 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class ContentAgentUI:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def __init__(self, endpoint_uri: str, is_healthy: bool, health_message: str, agent_initializer):
|
| 9 |
self.endpoint_uri = endpoint_uri
|
| 10 |
-
self.is_healthy = is_healthy
|
| 11 |
-
self.health_message = health_message
|
| 12 |
-
self.agent_initializer = agent_initializer
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
css_path = os.path.join(os.getcwd(), "ui", "styles.css")
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
-
if
|
| 36 |
-
return
|
| 37 |
-
|
| 38 |
-
return "Agent not loaded."
|
| 39 |
except Exception as e:
|
| 40 |
-
return f"Error: {
|
| 41 |
-
|
| 42 |
-
def create_header(self):
|
| 43 |
-
with self.ca_gui:
|
| 44 |
-
gr.Markdown("# Content Agent")
|
| 45 |
-
|
| 46 |
-
def create_user_guidance(self):
|
| 47 |
-
with self.ca_gui:
|
| 48 |
-
gr.Markdown("""
|
| 49 |
-
Please enter text below to get started. The AI Agent will try to determine whether the language is polite and uses the following classification:
|
| 50 |
-
|
| 51 |
-
- `polite`
|
| 52 |
-
- `somewhat polite`
|
| 53 |
-
- `neutral`
|
| 54 |
-
- `impolite`
|
| 55 |
-
|
| 56 |
-
Technology:
|
| 57 |
|
| 58 |
-
|
| 59 |
-
- Agent uses Intel's Polite Guard NLP library tool
|
| 60 |
-
- Compute on GCP · Nvidia L4 · 4x GPUs · 96 GB
|
| 61 |
-
""")
|
| 62 |
|
| 63 |
-
def
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
with gr.Column():
|
| 67 |
-
self.user_input = gr.Textbox(label="Your Input", placeholder="Enter something here...")
|
| 68 |
-
self.submit_button = gr.Button("Submit")
|
| 69 |
-
self.output = gr.Textbox(label="Content feedback", interactive=False, lines=10, max_lines=20)
|
| 70 |
-
|
| 71 |
-
# Use bound method with `self`
|
| 72 |
-
self.submit_button.click(self.call_agent, inputs=self.user_input, outputs=self.output)
|
| 73 |
-
self.user_input.submit(self.call_agent, inputs=self.user_input, outputs=self.output)
|
| 74 |
-
|
| 75 |
-
def get_example(self):
|
| 76 |
-
example_root = os.path.join(os.path.dirname(__file__), "examples")
|
| 77 |
-
examples = []
|
| 78 |
-
|
| 79 |
-
if os.path.exists(example_root):
|
| 80 |
-
example_files = [os.path.join(example_root, f) for f in os.listdir(example_root) if f.endswith(".txt")]
|
| 81 |
-
|
| 82 |
-
for file_path in example_files:
|
| 83 |
-
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 84 |
-
examples.append(f.read())
|
| 85 |
-
|
| 86 |
-
return examples
|
| 87 |
-
|
| 88 |
-
def create_examples(self):
|
| 89 |
-
examples = self.get_example()
|
| 90 |
-
|
| 91 |
-
with self.ca_gui:
|
| 92 |
-
if examples:
|
| 93 |
-
example_radio = gr.Radio(choices=examples, label="Try one of these examples:")
|
| 94 |
-
example_radio.change(fn=lambda ex: ex, inputs=example_radio, outputs=self.user_input)
|
| 95 |
-
else:
|
| 96 |
-
gr.Markdown("*No examples found.*")
|
| 97 |
-
|
| 98 |
-
def create_footer(self):
|
| 99 |
-
with self.ca_gui:
|
| 100 |
-
gr.Markdown("<div id='footer'>Thanks for trying it out!</div>")
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
def launch(self):
|
| 105 |
-
# build gradio Blocks:
|
| 106 |
-
# - status textbox prefilled with health_message
|
| 107 |
-
# - “Wake Endpoint” button → calls wake logic, updates status
|
| 108 |
-
# - “Initialize Agent” button → if healthy, self.agent = agent_initializer(endpoint_uri); reveal main panel
|
| 109 |
-
...
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def pass_through_agent(self, agent):
|
| 113 |
-
# Assign the agent for future use in `call_agent`
|
| 114 |
-
self.agent = agent
|
| 115 |
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
+
from status_check import is_endpoint_healthy # your tiny checker
|
| 5 |
+
from endpoint_utils import wake_endpoint # your wake+poll helper
|
| 6 |
|
| 7 |
|
| 8 |
class ContentAgentUI:
|
| 9 |
+
"""
|
| 10 |
+
Gradio UI that:
|
| 11 |
+
- shows a minimal control panel first (status + Start button),
|
| 12 |
+
- wakes & inits the agent when requested,
|
| 13 |
+
- reveals the main chat panel only after the agent is ready.
|
| 14 |
+
"""
|
| 15 |
|
| 16 |
def __init__(self, endpoint_uri: str, is_healthy: bool, health_message: str, agent_initializer):
|
| 17 |
self.endpoint_uri = endpoint_uri
|
| 18 |
+
self.is_healthy = bool(is_healthy)
|
| 19 |
+
self.health_message = health_message or ""
|
| 20 |
+
self.agent_initializer = agent_initializer # callable: (uri) -> CodeAgent
|
| 21 |
+
|
| 22 |
+
# set at build()
|
| 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 # where we store the agent object
|
| 30 |
+
# NOTE: we don't keep a separate self.agent; we read from agent_state in callbacks
|
| 31 |
+
|
| 32 |
+
# ---------- helpers ----------
|
| 33 |
+
|
| 34 |
+
def _read_css(self) -> str | None:
|
| 35 |
css_path = os.path.join(os.getcwd(), "ui", "styles.css")
|
| 36 |
+
if os.path.exists(css_path):
|
| 37 |
+
try:
|
| 38 |
+
with open(css_path, "r", encoding="utf-8") as f:
|
| 39 |
+
return f.read()
|
| 40 |
+
except Exception:
|
| 41 |
+
return None
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
def _initial_status_text(self) -> str:
|
| 45 |
+
if not self.endpoint_uri:
|
| 46 |
+
return "No endpoint URI configured."
|
| 47 |
+
if self.is_healthy:
|
| 48 |
+
return f"Endpoint healthy ✅ — {self.health_message or 'OK'}.\nClick 'Start Agent' to initialize."
|
| 49 |
+
return (
|
| 50 |
+
f"Endpoint not healthy: {self.health_message or 'unknown'}.\n"
|
| 51 |
+
"Click 'Start Agent' to wake the endpoint and initialize."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# ---------- agent call ----------
|
| 55 |
+
|
| 56 |
+
@staticmethod
|
| 57 |
+
def _call_agent(text: str, agent) -> str:
|
| 58 |
try:
|
| 59 |
+
if agent is None:
|
| 60 |
+
return "Agent not initialized yet. Click 'Start Agent'."
|
| 61 |
+
return str(agent.run(text)) # smolagents.CodeAgent API
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
+
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# ---------- UI build ----------
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
def build(self) -> gr.Blocks:
|
| 68 |
+
if self.app is not None:
|
| 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",
|
| 79 |
+
value=self._initial_status_text(),
|
| 80 |
+
lines=8,
|
| 81 |
+
interactive=False,
|
| 82 |
+
)
|
| 83 |
+
start_btn = gr.Button("Start Agent (wake if needed)")
|
| 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)
|
| 90 |
+
submit_btn = gr.Button("Submit")
|
| 91 |
+
|
| 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 |
+
# Event: Start Agent (wake → health-check → init → reveal main panel)
|
| 96 |
+
def on_start():
|
| 97 |
+
# 1) show we're waking (stream status)
|
| 98 |
+
yield ("Waking endpoint…", gr.update(), gr.update(), None)
|
| 99 |
+
|
| 100 |
+
ok, err = wake_endpoint(self.endpoint_uri, max_wait=180, poll_every=5.0)
|
| 101 |
+
if not ok:
|
| 102 |
+
# wake failed → keep control panel visible
|
| 103 |
+
yield (f"Wake failed: {err or 'unknown error'}", gr.update(), gr.update(), None)
|
| 104 |
+
return
|
| 105 |
+
|
| 106 |
+
# 2) check health
|
| 107 |
+
yield ("Endpoint awake ✅. Checking health…", gr.update(), gr.update(), None)
|
| 108 |
+
healthy, msg = is_endpoint_healthy(self.endpoint_uri)
|
| 109 |
+
if not healthy:
|
| 110 |
+
yield (f"Endpoint not healthy: {msg}", gr.update(), gr.update(), None)
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
# 3) initialize agent
|
| 114 |
+
yield ("Initializing agent…", gr.update(), gr.update(), None)
|
| 115 |
+
try:
|
| 116 |
+
agent = self.agent_initializer(self.endpoint_uri)
|
| 117 |
+
except Exception as e:
|
| 118 |
+
yield (f"Agent init failed: {e}", gr.update(), gr.update(), None)
|
| 119 |
+
return
|
| 120 |
+
|
| 121 |
+
# 4) success → hide control panel, show main
|
| 122 |
+
yield ("Agent initialized ✅", gr.update(visible=False), gr.update(visible=True), agent)
|
| 123 |
+
|
| 124 |
+
# wire the button: outputs are (status_text, control_panel_update, main_panel_update, agent_state)
|
| 125 |
+
start_btn.click(on_start, inputs=None, outputs=[self.status_box, self.control_panel, self.main_panel, self.agent_state])
|
| 126 |
+
|
| 127 |
+
self.app = demo
|
| 128 |
+
|
| 129 |
+
return self.app
|
| 130 |
+
|
| 131 |
+
# ---------- public API ----------
|
| 132 |
+
|
| 133 |
+
def launch(self, **kwargs):
|
| 134 |
+
return self.build().launch(**kwargs)
|