Elishaaa321 commited on
Commit
d87d8b2
·
verified ·
1 Parent(s): b7199a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -8,17 +8,14 @@ def chatbot(user_input, history):
8
  if history is None:
9
  history = []
10
 
11
- messages = []
 
12
 
13
- # FIX: Ensure proper unpacking
14
- for chat in history:
15
- if len(chat) == 2:
16
- human, bot = chat
17
- messages.append({"role": "user", "content": str(human)})
18
- messages.append({"role": "assistant", "content": str(bot)})
19
-
20
- # current message
21
- messages.append({"role": "user", "content": str(user_input)})
22
 
23
  try:
24
  response = client.chat.completions.create(
@@ -31,17 +28,24 @@ def chatbot(user_input, history):
31
  except Exception as e:
32
  bot_reply = f"Error: {str(e)}"
33
 
34
- history.append((user_input, bot_reply))
 
 
 
 
35
 
36
- return history, history
37
 
38
 
39
  with gr.Blocks() as demo:
40
  gr.Markdown("## 🤖 Simple Groq Chatbot")
41
 
42
- chatbot_ui = gr.Chatbot()
 
 
43
  msg = gr.Textbox(placeholder="Type your message...")
44
  state = gr.State([])
 
45
  clear = gr.Button("Clear")
46
 
47
  msg.submit(chatbot, [msg, state], [chatbot_ui, state])
 
8
  if history is None:
9
  history = []
10
 
11
+ # history is already in correct format now
12
+ messages = history.copy()
13
 
14
+ # add current user message
15
+ messages.append({
16
+ "role": "user",
17
+ "content": user_input
18
+ })
 
 
 
 
19
 
20
  try:
21
  response = client.chat.completions.create(
 
28
  except Exception as e:
29
  bot_reply = f"Error: {str(e)}"
30
 
31
+ # add assistant reply
32
+ messages.append({
33
+ "role": "assistant",
34
+ "content": bot_reply
35
+ })
36
 
37
+ return messages, messages
38
 
39
 
40
  with gr.Blocks() as demo:
41
  gr.Markdown("## 🤖 Simple Groq Chatbot")
42
 
43
+ # IMPORTANT CHANGE HERE
44
+ chatbot_ui = gr.Chatbot(type="messages")
45
+
46
  msg = gr.Textbox(placeholder="Type your message...")
47
  state = gr.State([])
48
+
49
  clear = gr.Button("Clear")
50
 
51
  msg.submit(chatbot, [msg, state], [chatbot_ui, state])