Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from agents.programmer import ProgrammerAgent
|
| 5 |
+
from agents.debugger import DebuggerAgent
|
| 6 |
+
from agents.base_agent import ACPMessage
|
| 7 |
+
|
| 8 |
+
# Initialize agents
|
| 9 |
+
codebot = ProgrammerAgent()
|
| 10 |
+
bugbot = DebuggerAgent()
|
| 11 |
+
|
| 12 |
+
# Chat history
|
| 13 |
+
conversation = []
|
| 14 |
+
|
| 15 |
+
def start_conversation():
|
| 16 |
+
global conversation
|
| 17 |
+
conversation = [] # reset
|
| 18 |
+
|
| 19 |
+
# Initial message from BugBot
|
| 20 |
+
msg = bugbot.create_message(
|
| 21 |
+
receiver=codebot.name,
|
| 22 |
+
performative="request",
|
| 23 |
+
content="Can you write a Python function to reverse a list?"
|
| 24 |
+
)
|
| 25 |
+
conversation.append(str(msg))
|
| 26 |
+
|
| 27 |
+
# CodeBot responds
|
| 28 |
+
reply = codebot.receive_message(msg)
|
| 29 |
+
conversation.append(str(reply))
|
| 30 |
+
|
| 31 |
+
return "\n\n".join(conversation)
|
| 32 |
+
|
| 33 |
+
def continue_conversation():
|
| 34 |
+
if len(conversation) < 2:
|
| 35 |
+
return "Please start the conversation first."
|
| 36 |
+
|
| 37 |
+
# Last reply came from CodeBot β BugBot now responds
|
| 38 |
+
last_msg = conversation[-1]
|
| 39 |
+
parsed = parse_acp_string(last_msg)
|
| 40 |
+
reply = bugbot.receive_message(parsed)
|
| 41 |
+
conversation.append(str(reply))
|
| 42 |
+
|
| 43 |
+
# CodeBot replies again
|
| 44 |
+
reply2 = codebot.receive_message(reply)
|
| 45 |
+
conversation.append(str(reply2))
|
| 46 |
+
|
| 47 |
+
return "\n\n".join(conversation)
|
| 48 |
+
|
| 49 |
+
def parse_acp_string(message_str: str) -> ACPMessage:
|
| 50 |
+
"""
|
| 51 |
+
Convert "[PERFORMATIVE] sender β receiver: content"
|
| 52 |
+
back to an ACPMessage object
|
| 53 |
+
"""
|
| 54 |
+
try:
|
| 55 |
+
parts = message_str.split("] ", 1)
|
| 56 |
+
performative = parts[0][1:].lower()
|
| 57 |
+
meta, content = parts[1].split(": ", 1)
|
| 58 |
+
sender, receiver = [s.strip() for s in meta.split("β")]
|
| 59 |
+
return ACPMessage(sender, receiver, performative, content.strip())
|
| 60 |
+
except Exception:
|
| 61 |
+
return ACPMessage("Unknown", "Unknown", "inform", "Failed to parse message.")
|
| 62 |
+
|
| 63 |
+
# Gradio interface
|
| 64 |
+
with gr.Blocks(title="BotTalks") as demo:
|
| 65 |
+
gr.Markdown("# π€ BotTalks: Programmer vs Debugger\nAgent Communication Protocol Chat")
|
| 66 |
+
|
| 67 |
+
chatbox = gr.Textbox(label="Conversation", lines=20)
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
start_btn = gr.Button("Start Conversation π")
|
| 71 |
+
next_btn = gr.Button("Next Turn π")
|
| 72 |
+
|
| 73 |
+
start_btn.click(start_conversation, outputs=chatbox)
|
| 74 |
+
next_btn.click(continue_conversation, outputs=chatbox)
|
| 75 |
+
|
| 76 |
+
# Run app
|
| 77 |
+
demo.launch()
|