Eyadddddddd commited on
Commit
b67ccb2
·
verified ·
1 Parent(s): efa29f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -58,9 +58,11 @@ def chat_fn(message, history, mode, model, image, include_ascii):
58
  system_prompt = MODE_PROMPTS.get(mode, MODE_PROMPTS["Normal Chat"])
59
  messages = [{"role": "system", "content": system_prompt}]
60
 
61
- # 2. Append history (Gradio 4.x 'messages' format is already compatible with Groq)
62
- # History comes in as: [{'role': 'user', 'content': '...'}, {'role': 'assistant', 'content': '...'}]
63
- messages.extend(history)
 
 
64
 
65
  # 3. Process current input
66
  user_input_text = str(message).strip()
@@ -88,8 +90,8 @@ with gr.Blocks() as demo:
88
  mode_dd = gr.Dropdown(choices=list(MODE_PROMPTS.keys()), value="Normal Chat", label="Mode")
89
  model_dd = gr.Dropdown(choices=["llama-3.1-8b-instant", "llama-3.1-70b-versatile"], value=DEFAULT_MODEL, label="Model")
90
 
91
- # FIX 1: Explicitly set type="messages"
92
- chatbot = gr.Chatbot(height=450, type="messages")
93
 
94
  with gr.Row():
95
  user_input = gr.Textbox(placeholder="Ask anything...", show_label=False, scale=4)
@@ -102,9 +104,8 @@ with gr.Blocks() as demo:
102
  # 1. Get the bot's response
103
  bot_message = chat_fn(msg, chat_history, mode, model, img, ascii_on)
104
 
105
- # FIX 2: Append dictionaries, not lists
106
- chat_history.append({"role": "user", "content": msg})
107
- chat_history.append({"role": "assistant", "content": bot_message})
108
 
109
  # 3. Return history and clear the input box
110
  return chat_history, ""
 
58
  system_prompt = MODE_PROMPTS.get(mode, MODE_PROMPTS["Normal Chat"])
59
  messages = [{"role": "system", "content": system_prompt}]
60
 
61
+ # 2. Convert Gradio's history (list of lists) to Groq's format (list of dicts)
62
+ if history:
63
+ for user_msg, bot_msg in history:
64
+ if user_msg: messages.append({"role": "user", "content": str(user_msg)})
65
+ if bot_msg: messages.append({"role": "assistant", "content": str(bot_msg)})
66
 
67
  # 3. Process current input
68
  user_input_text = str(message).strip()
 
90
  mode_dd = gr.Dropdown(choices=list(MODE_PROMPTS.keys()), value="Normal Chat", label="Mode")
91
  model_dd = gr.Dropdown(choices=["llama-3.1-8b-instant", "llama-3.1-70b-versatile"], value=DEFAULT_MODEL, label="Model")
92
 
93
+ # Force classic tuple format for history
94
+ chatbot = gr.Chatbot(height=450, type="tuple")
95
 
96
  with gr.Row():
97
  user_input = gr.Textbox(placeholder="Ask anything...", show_label=False, scale=4)
 
104
  # 1. Get the bot's response
105
  bot_message = chat_fn(msg, chat_history, mode, model, img, ascii_on)
106
 
107
+ # 2. Append the new interaction as a TUPLE (user, bot)
108
+ chat_history.append((msg, bot_message))
 
109
 
110
  # 3. Return history and clear the input box
111
  return chat_history, ""