aymnsk commited on
Commit
e9337b4
Β·
verified Β·
1 Parent(s): 1221e37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -41
app.py CHANGED
@@ -1,76 +1,75 @@
1
  import gradio as gr
2
- import json
3
- import os
4
  from agents.programmer import ProgrammerAgent
5
  from agents.debugger import DebuggerAgent
6
  from agents.base_agent import ACPMessage
 
 
7
 
8
- # === Init Agents ===
9
  aymaan = ProgrammerAgent()
10
  zaid = DebuggerAgent()
 
 
11
  chat_memory = []
12
 
13
- # === Load example prompts from JSON ===
14
- def load_prompts():
15
- try:
16
- with open("data/prompts.json", "r", encoding="utf-8") as f:
17
- return json.load(f).get("prompts", [])
18
- except Exception as e:
19
- print(f"Error loading prompts: {e}")
20
- return []
21
 
22
- example_prompts = load_prompts()
 
 
 
 
 
23
 
24
- # === Main Chat Handler ===
25
  def chat(user_input):
26
  if not user_input.strip():
27
  return chat_memory
28
 
29
- user_msg = ACPMessage(sender="User", receiver="Aymaan", performative="inform", content=user_input)
30
-
31
- # Fast handling for greetings
32
- if aymaan.is_greeting(user_input):
33
- response_aymaan = aymaan.create_message("User", "inform", "Hey there! 😊 I'm ready to help you.")
34
- response_zaid = zaid.create_message("User", "inform", "Hi hi! πŸ‘‹ How can I assist you today?")
35
  else:
36
- response_aymaan = aymaan.receive_message(user_msg)
37
- response_zaid = zaid.receive_message(user_msg)
 
 
 
38
 
39
  chat_memory.append({"role": "user", "content": user_input})
40
- chat_memory.append({"role": "aymaan", "content": response_aymaan.content})
41
- chat_memory.append({"role": "zaid", "content": response_zaid.content})
42
 
43
- return chat_memory
 
 
 
44
 
45
- # === Memory Reset ===
46
  def reset_memory():
47
  global chat_memory
48
  chat_memory = []
49
- return chat_memory
50
 
51
- # === App UI ===
52
  with gr.Blocks(css="footer {margin-top: 2em; text-align: center;}") as demo:
53
- gr.Markdown("## πŸ€– BotTalks: Chat with 2 AI Friends!\nAsk anything and see what Aymaan and Zaid say.")
54
-
55
- chatbot = gr.Chatbot(label="Group Chat", type="messages", height=300)
56
- msg = gr.Textbox(label="You", placeholder="Ask anything...")
57
- send_btn = gr.Button("πŸ“€ Send")
58
- clear_btn = gr.Button("🧹 Reset Memory")
59
 
60
- with gr.Row():
61
- for prompt in example_prompts[:4]: # show first 4 prompts
62
- gr.Button(value=prompt).click(fn=chat, inputs=gr.Textbox(value=prompt, visible=False), outputs=chatbot)
 
63
 
64
  send_btn.click(chat, inputs=msg, outputs=chatbot)
65
  clear_btn.click(reset_memory, outputs=chatbot)
66
 
67
  gr.HTML("""
68
  <footer>
69
- πŸ”— Connect:
70
- <a href="https://www.linkedin.com/in/aymnsk" target="_blank">LinkedIn</a> |
71
- <a href="https://github.com/aymnsk" target="_blank">GitHub</a> |
72
- <a href="https://www.instagram.com/damnn_aymn/" target="_blank">Instagram</a> |
73
- <a href="https://huggingface.co/aymnsk" target="_blank">HF</a>
74
  </footer>
75
  """)
76
 
 
1
  import gradio as gr
 
 
2
  from agents.programmer import ProgrammerAgent
3
  from agents.debugger import DebuggerAgent
4
  from agents.base_agent import ACPMessage
5
+ import json
6
+ import os
7
 
8
+ # Initialize agents
9
  aymaan = ProgrammerAgent()
10
  zaid = DebuggerAgent()
11
+
12
+ # In-memory chat
13
  chat_memory = []
14
 
15
+ # Load greeting shortcut mappings (optional JSON file)
16
+ GREETING_RESPONSES = {
17
+ "hi": "Hey there! πŸ‘‹",
18
+ "hello": "Hello! How can I help?",
19
+ "how are you": "Doing great! Thanks for asking 😊",
20
+ }
 
 
21
 
22
+ def fast_greeting_handler(msg):
23
+ lowered = msg.lower()
24
+ for greeting in GREETING_RESPONSES:
25
+ if greeting in lowered:
26
+ return True, GREETING_RESPONSES[greeting]
27
+ return False, ""
28
 
 
29
  def chat(user_input):
30
  if not user_input.strip():
31
  return chat_memory
32
 
33
+ is_greeting, shortcut = fast_greeting_handler(user_input)
34
+
35
+ if is_greeting:
36
+ response_aymaan = shortcut + " (Aymaan)"
37
+ response_zaid = shortcut + " (Zaid)"
 
38
  else:
39
+ user_msg = ACPMessage(sender="User", receiver="Aymaan", performative="inform", content=user_input)
40
+ response_aymaan = aymaan.receive_message(user_msg).content
41
+
42
+ user_msg = ACPMessage(sender="User", receiver="Zaid", performative="inform", content=user_input)
43
+ response_zaid = zaid.receive_message(user_msg).content
44
 
45
  chat_memory.append({"role": "user", "content": user_input})
46
+ chat_memory.append({"role": "Aymaan", "content": response_aymaan})
47
+ chat_memory.append({"role": "Zaid", "content": response_zaid})
48
 
49
+ formatted = []
50
+ for item in chat_memory:
51
+ formatted.append((item["role"], item["content"]))
52
+ return formatted
53
 
 
54
  def reset_memory():
55
  global chat_memory
56
  chat_memory = []
57
+ return []
58
 
 
59
  with gr.Blocks(css="footer {margin-top: 2em; text-align: center;}") as demo:
60
+ gr.Markdown("## πŸ€– BotTalks (Groq Edition) β€” Chat with Aymaan & Zaid")
 
 
 
 
 
61
 
62
+ chatbot = gr.Chatbot(label="Group Chat", height=300)
63
+ msg = gr.Textbox(label="Type your message...")
64
+ send_btn = gr.Button("Send")
65
+ clear_btn = gr.Button("🧹 Clear Chat")
66
 
67
  send_btn.click(chat, inputs=msg, outputs=chatbot)
68
  clear_btn.click(reset_memory, outputs=chatbot)
69
 
70
  gr.HTML("""
71
  <footer>
72
+ πŸ‘¨β€πŸ’» Made by <a href="https://github.com/aymnsk" target="_blank">aymnsk</a> β€’ Powered by <a href="https://groq.com/" target="_blank">Groq API</a>
 
 
 
 
73
  </footer>
74
  """)
75