ndkilla commited on
Commit
5849c6c
·
verified ·
1 Parent(s): e38b21b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -1,7 +1,26 @@
1
  import gradio as gr
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  def echo(message, history=None):
4
  history = history or []
 
 
5
 
6
  message_lower = message.lower()
7
  if 'dahlia' in message_lower:
@@ -13,13 +32,9 @@ def echo(message, history=None):
13
  else:
14
  response = f'{message} ... Always.'
15
 
16
- # Append user message
17
  history.append({"role": "user", "content": message})
18
- # Append assistant response
19
  history.append({"role": "assistant", "content": response})
20
-
21
- # Return updated history as both output and chat history state
22
- return history, history
23
 
24
  chat = gr.ChatInterface(
25
  fn=echo,
 
1
  import gradio as gr
2
 
3
+ def convert_tuples_to_message_dicts(messages):
4
+ """
5
+ Converts a list of tuples [('user', 'hi'), ...]
6
+ to OpenAI style messages [{'role': 'user', 'content': 'hi'}, ...].
7
+ Leaves existing dicts unchanged.
8
+ """
9
+ converted = []
10
+ for msg in messages:
11
+ if isinstance(msg, tuple) and len(msg) == 2:
12
+ role, content = msg
13
+ converted.append({"role": role, "content": content})
14
+ elif isinstance(msg, dict) and "role" in msg and "content" in msg:
15
+ converted.append(msg)
16
+ else:
17
+ raise ValueError(f"Invalid message format: {msg}")
18
+ return converted
19
+
20
  def echo(message, history=None):
21
  history = history or []
22
+ # Important: Clean up incoming history, just in case
23
+ history = convert_tuples_to_message_dicts(history)
24
 
25
  message_lower = message.lower()
26
  if 'dahlia' in message_lower:
 
32
  else:
33
  response = f'{message} ... Always.'
34
 
 
35
  history.append({"role": "user", "content": message})
 
36
  history.append({"role": "assistant", "content": response})
37
+ return history
 
 
38
 
39
  chat = gr.ChatInterface(
40
  fn=echo,