AbuSaleh28 commited on
Commit
7c47949
·
verified ·
1 Parent(s): 20d2ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -22,26 +22,28 @@ custom_css = """
22
  @spaces.GPU
23
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
24
  """
25
- Handles streaming responses using the modern list-of-dicts (messages) history format.
 
26
  """
27
  if not message.strip():
28
  yield history
29
  return
30
 
31
- # 1. Initialize message list with system prompt for the Groq API payload
32
  api_messages = [{"role": "system", "content": system_prompt}]
33
 
34
- # 2. Append existing conversation history safely
35
- for turn in history:
36
- # turn is a dictionary: {"role": "user"|"assistant", "content": "..."}
37
- api_messages.append({"role": turn["role"], "content": turn["content"]})
 
 
38
 
39
  # 3. Append the newest user message to the API list
40
  api_messages.append({"role": "user", "content": message})
41
 
42
- # 4. Update the Gradio UI history using the dictionary format
43
- history.append({"role": "user", "content": message})
44
- history.append({"role": "assistant", "content": ""})
45
  yield history
46
 
47
  # 5. Stream from Groq
@@ -58,12 +60,12 @@ def chat_stream(message, history, model, system_prompt, temperature, max_tokens)
58
  for chunk in stream:
59
  if chunk.choices[0].delta.content:
60
  partial_response += chunk.choices[0].delta.content
61
- # Update the very last item in history (the assistant's content dictionary)
62
- history[-1]["content"] = partial_response
63
  yield history
64
 
65
  except Exception as e:
66
- history[-1]["content"] = f"⚠️ Error connecting to Groq: {str(e)}"
67
  yield history
68
 
69
  # Build the layout manually
@@ -107,8 +109,8 @@ with gr.Blocks() as demo:
107
 
108
  # --- RIGHT COLUMN: CHAT INTERFACE ---
109
  with gr.Column(scale=3):
110
- # Explicitly state we are using the modern messages type for the component layout
111
- chatbot = gr.Chatbot(elem_classes="chat-window", type="messages")
112
 
113
  msg_input = gr.Textbox(
114
  placeholder="Type your message here and press Enter...",
 
22
  @spaces.GPU
23
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
24
  """
25
+ Handles streaming responses using the standard tuple-based chat history format.
26
+ history layout: [[user_msg1, bot_msg1], [user_msg2, bot_msg2], ...]
27
  """
28
  if not message.strip():
29
  yield history
30
  return
31
 
32
+ # 1. Initialize message list with system prompt for Groq API
33
  api_messages = [{"role": "system", "content": system_prompt}]
34
 
35
+ # 2. Append existing conversation history seamlessly
36
+ for user_msg, bot_msg in history:
37
+ if user_msg:
38
+ api_messages.append({"role": "user", "content": user_msg})
39
+ if bot_msg:
40
+ api_messages.append({"role": "assistant", "content": bot_msg})
41
 
42
  # 3. Append the newest user message to the API list
43
  api_messages.append({"role": "user", "content": message})
44
 
45
+ # 4. Update the Gradio UI history with an empty slot for the bot's upcoming message
46
+ history.append([message, ""])
 
47
  yield history
48
 
49
  # 5. Stream from Groq
 
60
  for chunk in stream:
61
  if chunk.choices[0].delta.content:
62
  partial_response += chunk.choices[0].delta.content
63
+ # Update the very last bot message slot in history
64
+ history[-1][1] = partial_response
65
  yield history
66
 
67
  except Exception as e:
68
+ history[-1][1] = f"⚠️ Error connecting to Groq: {str(e)}"
69
  yield history
70
 
71
  # Build the layout manually
 
109
 
110
  # --- RIGHT COLUMN: CHAT INTERFACE ---
111
  with gr.Column(scale=3):
112
+ # Removed the `type="messages"` keyword argument entirely to prevent the TypeError
113
+ chatbot = gr.Chatbot(elem_classes="chat-window")
114
 
115
  msg_input = gr.Textbox(
116
  placeholder="Type your message here and press Enter...",