Fazeel Asghar commited on
Commit
c9ff9b4
·
1 Parent(s): 9ce3a35

Changes in Interface

Browse files
Files changed (1) hide show
  1. app.py +47 -12
app.py CHANGED
@@ -21,7 +21,7 @@ class ChatInput(BaseModel):
21
  session_id: str
22
  input: str
23
 
24
- def chat_logic(session_id: str, user_input: str) -> str:
25
  if session_id not in session_memory_dict:
26
  session_memory_dict[session_id] = []
27
 
@@ -48,14 +48,49 @@ def chat_logic(session_id: str, user_input: str) -> str:
48
  print(f"[Session: {session_id}] AI Response: {ai_response}")
49
  return ai_response
50
 
51
- # Gradio Interface
52
- def gradio_chat(user_input, session_id="gradio_default"):
53
- return chat_logic(session_id=session_id, user_input=user_input)
54
-
55
- gr.Interface(
56
- fn=gradio_chat,
57
- inputs=[gr.Textbox(label="Your message"), gr.Textbox(label="Session ID", value="gradio_default")],
58
- outputs=gr.Textbox(label="Response"),
59
- title="Chatbot with memory",
60
- description="Chat with Groq's LLaMA3 model. Handles sessions separately using IDs."
61
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  session_id: str
22
  input: str
23
 
24
+ def chat_logic(session_id: str, user_input: str):
25
  if session_id not in session_memory_dict:
26
  session_memory_dict[session_id] = []
27
 
 
48
  print(f"[Session: {session_id}] AI Response: {ai_response}")
49
  return ai_response
50
 
51
+ # UI logic for message display
52
+ def chat_interface(user_input, chat_history, session_id):
53
+ ai_reply = chat_logic(session_id, user_input)
54
+ chat_history.append((user_input, ai_reply))
55
+ return chat_history, ""
56
+
57
+ # Clear session memory
58
+ def clear_session(session_id):
59
+ session_memory_dict.pop(session_id, None)
60
+ return [], "gradio_default"
61
+
62
+ with gr.Blocks(css="""
63
+ .chatbot .message.user { background-color: #dcf8c6 !important; color: #000; }
64
+ .chatbot .message.bot { background-color: #ececec !important; color: #000; }
65
+ """) as demo:
66
+ gr.Markdown("### 🤖 WhatsApp-style Chatbot powered by Groq LLaMA3")
67
+
68
+ with gr.Row():
69
+ session_id = gr.Textbox(label="Session ID", value="gradio_default", interactive=True)
70
+ clear_btn = gr.Button("🔄 Clear Session")
71
+
72
+ chatbot = gr.Chatbot(label="Chat with AI", elem_classes="chatbot")
73
+ user_input = gr.Textbox(label="Type your message...", placeholder="Message...", lines=1)
74
+ send_btn = gr.Button("Send", variant="primary")
75
+
76
+ state = gr.State([])
77
+
78
+ send_btn.click(
79
+ fn=chat_interface,
80
+ inputs=[user_input, state, session_id],
81
+ outputs=[chatbot, user_input]
82
+ )
83
+
84
+ user_input.submit(
85
+ fn=chat_interface,
86
+ inputs=[user_input, state, session_id],
87
+ outputs=[chatbot, user_input]
88
+ )
89
+
90
+ clear_btn.click(
91
+ fn=clear_session,
92
+ inputs=[session_id],
93
+ outputs=[chatbot, session_id]
94
+ )
95
+
96
+ demo.launch()