Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import json | |
| import os | |
| from agents.programmer import ProgrammerAgent | |
| from agents.debugger import DebuggerAgent | |
| from agents.base_agent import ACPMessage | |
| # === Init Agents === | |
| aymaan = ProgrammerAgent() | |
| zaid = DebuggerAgent() | |
| chat_memory = [] | |
| # === Load example prompts from JSON === | |
| def load_prompts(): | |
| try: | |
| with open("data/prompts.json", "r", encoding="utf-8") as f: | |
| return json.load(f).get("prompts", []) | |
| except Exception as e: | |
| print(f"Error loading prompts: {e}") | |
| return [] | |
| example_prompts = load_prompts() | |
| # === Main Chat Handler === | |
| def chat(user_input): | |
| if not user_input.strip(): | |
| return chat_memory | |
| user_msg = ACPMessage(sender="User", receiver="Aymaan", performative="inform", content=user_input) | |
| # Fast handling for greetings | |
| if aymaan.is_greeting(user_input): | |
| response_aymaan = aymaan.create_message("User", "inform", "Hey there! π I'm ready to help you.") | |
| response_zaid = zaid.create_message("User", "inform", "Hi hi! π How can I assist you today?") | |
| else: | |
| response_aymaan = aymaan.receive_message(user_msg) | |
| response_zaid = zaid.receive_message(user_msg) | |
| chat_memory.append({"role": "user", "content": user_input}) | |
| chat_memory.append({"role": "aymaan", "content": response_aymaan.content}) | |
| chat_memory.append({"role": "zaid", "content": response_zaid.content}) | |
| return chat_memory | |
| # === Memory Reset === | |
| def reset_memory(): | |
| global chat_memory | |
| chat_memory = [] | |
| return chat_memory | |
| # === App UI === | |
| with gr.Blocks(css="footer {margin-top: 2em; text-align: center;}") as demo: | |
| gr.Markdown("## π€ BotTalks: Chat with 2 AI Friends!\nAsk anything and see what Aymaan and Zaid say.") | |
| chatbot = gr.Chatbot(label="Group Chat", type="messages", height=300) | |
| msg = gr.Textbox(label="You", placeholder="Ask anything...") | |
| send_btn = gr.Button("π€ Send") | |
| clear_btn = gr.Button("π§Ή Reset Memory") | |
| with gr.Row(): | |
| for prompt in example_prompts[:4]: # show first 4 prompts | |
| gr.Button(value=prompt).click(fn=chat, inputs=gr.Textbox(value=prompt, visible=False), outputs=chatbot) | |
| send_btn.click(chat, inputs=msg, outputs=chatbot) | |
| clear_btn.click(reset_memory, outputs=chatbot) | |
| gr.HTML(""" | |
| <footer> | |
| π Connect: | |
| <a href="https://www.linkedin.com/in/aymnsk" target="_blank">LinkedIn</a> | | |
| <a href="https://github.com/aymnsk" target="_blank">GitHub</a> | | |
| <a href="https://www.instagram.com/damnn_aymn/" target="_blank">Instagram</a> | | |
| <a href="https://huggingface.co/aymnsk" target="_blank">HF</a> | |
| </footer> | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch() | |