Spaces:
Runtime error
Runtime error
Charles Azam commited on
Commit ·
60a42e1
1
Parent(s): e40a46c
works with theards
Browse files
src/deepengineer/backend/gradio_entry_point.py
CHANGED
|
@@ -1,43 +1,86 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import threading
|
| 4 |
-
import time
|
| 5 |
-
from smolagents import Tool
|
| 6 |
-
from deepengineer.deepsearch.main_agent import main_search
|
| 7 |
-
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class SearchTool:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
time.sleep(1)
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
class DrawTool:
|
| 17 |
-
def forward(self,
|
|
|
|
| 18 |
time.sleep(1)
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
final_answer = search_tool.forward(user_input) + draw_tool.forward(user_input)
|
| 27 |
-
return final_answer
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
agent_output = gr.Textbox(label="Agent Response")
|
| 35 |
-
log_output
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
fn=run_agent,
|
| 41 |
inputs=[user_input],
|
| 42 |
outputs=[agent_output, log_output],
|
| 43 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import threading, time, queue, logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# ---------- 1. A thread‑safe queue for log messages ----------
|
| 5 |
+
log_queue = queue.Queue()
|
| 6 |
+
|
| 7 |
+
def push_log(msg: str):
|
| 8 |
+
"""Helper so tools can push log lines."""
|
| 9 |
+
log_queue.put(msg)
|
| 10 |
+
|
| 11 |
+
# ---------- 2. Tools that report progress ----------
|
| 12 |
class SearchTool:
|
| 13 |
+
def forward(self, query: str) -> str:
|
| 14 |
+
push_log(f"🔍 SearchTool → {query}")
|
| 15 |
+
time.sleep(1) # expensive work…
|
| 16 |
+
out = f"Found {query}"
|
| 17 |
+
push_log(f"✅ SearchTool done")
|
| 18 |
+
return out
|
| 19 |
|
| 20 |
class DrawTool:
|
| 21 |
+
def forward(self, prompt: str) -> str:
|
| 22 |
+
push_log(f"🎨 DrawTool → {prompt}")
|
| 23 |
time.sleep(1)
|
| 24 |
+
out = f"Drawing {prompt}"
|
| 25 |
+
push_log(f"✅ DrawTool done")
|
| 26 |
+
return out
|
| 27 |
|
| 28 |
+
# ---------- 3. Your unchanged agent -------------------------
|
| 29 |
+
def agent(user_input: str) -> str:
|
| 30 |
+
st, dt = SearchTool(), DrawTool()
|
| 31 |
+
return st.forward(user_input) + dt.forward(user_input)
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# ---------- 4. Wrapper that streams -------------------------
|
| 34 |
+
def run_agent_stream(user_input: str):
|
| 35 |
+
"""
|
| 36 |
+
Generator wired to Gradio:
|
| 37 |
+
– starts the agent in a background thread
|
| 38 |
+
– while the agent runs, flushes anything that tools
|
| 39 |
+
have pushed into `log_queue`
|
| 40 |
+
– finally yields the agent’s answer
|
| 41 |
+
Yields tuples: (agent_output, log_output)
|
| 42 |
+
"""
|
| 43 |
+
# empty queue before each run
|
| 44 |
+
while not log_queue.empty():
|
| 45 |
+
log_queue.get_nowait()
|
| 46 |
+
|
| 47 |
+
answer_container = {"text": None}
|
| 48 |
+
done = threading.Event()
|
| 49 |
+
|
| 50 |
+
def _worker():
|
| 51 |
+
answer_container["text"] = agent(user_input)
|
| 52 |
+
done.set()
|
| 53 |
|
| 54 |
+
threading.Thread(target=_worker, daemon=True).start()
|
| 55 |
+
|
| 56 |
+
# stream logs until the agent finishes
|
| 57 |
+
log_buffer = ""
|
| 58 |
+
while not done.is_set() or not log_queue.empty():
|
| 59 |
+
while not log_queue.empty():
|
| 60 |
+
log_line = log_queue.get()
|
| 61 |
+
log_buffer += log_line + "\n"
|
| 62 |
+
# keep agent_output None until we have the final answer
|
| 63 |
+
yield (None, log_buffer.rstrip())
|
| 64 |
+
time.sleep(0.1)
|
| 65 |
+
|
| 66 |
+
# one last flush in case something arrived after last poll
|
| 67 |
+
while not log_queue.empty():
|
| 68 |
+
log_line = log_queue.get()
|
| 69 |
+
log_buffer += log_line + "\n"
|
| 70 |
+
|
| 71 |
+
# final yield: agent_output filled, log_output frozen
|
| 72 |
+
yield (answer_container["text"], log_buffer.rstrip())
|
| 73 |
+
|
| 74 |
+
# ---------- 5. Gradio UI ------------------------------------
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("## Agent Interface with Real‑Time Tool Logging")
|
| 77 |
+
user_input = gr.Textbox(label="User Message")
|
| 78 |
agent_output = gr.Textbox(label="Agent Response")
|
| 79 |
+
log_output = gr.Textbox(label="Tool Invocation Log", interactive=False)
|
| 80 |
|
| 81 |
+
send = gr.Button("Send")
|
| 82 |
+
send.click(
|
| 83 |
+
fn=run_agent_stream,
|
|
|
|
| 84 |
inputs=[user_input],
|
| 85 |
outputs=[agent_output, log_output],
|
| 86 |
)
|