artificialguybr commited on
Commit
a7c2b73
·
1 Parent(s): 6b4f11e

Refactor chat history formatting in app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -18
app.py CHANGED
@@ -86,28 +86,15 @@ with gr.Blocks() as demo:
86
  else:
87
  chat_history = user(message, chat_history)
88
  chat_history, _ = chat(chat_history, system_message, max_tokens.value, temperature.value, top_p.value, 40, 1.1)
89
-
90
  formatted_chat_history = []
91
- last_role = None
92
- for entry in chat_history:
93
- role = entry["role"]
94
- content = entry["content"].strip() # Trim whitespace to check for empty content
95
- if content: # Only proceed if content is not empty
96
- if role != last_role:
97
- if role == "user":
98
- formatted_chat_history.append([content, ""])
99
- elif role == "assistant":
100
- formatted_chat_history.append(["", content])
101
- last_role = role
102
- else:
103
- if role == "user" and content:
104
- formatted_chat_history[-1][0] += "\n" + content
105
- elif role == "assistant" and content:
106
- formatted_chat_history[-1][1] += "\n" + content
107
 
108
  return formatted_chat_history, chat_history, ""
109
 
110
-
111
  submit.click(
112
  fn=update_chatbot,
113
  inputs=[message, chat_history_state],
 
86
  else:
87
  chat_history = user(message, chat_history)
88
  chat_history, _ = chat(chat_history, system_message, max_tokens.value, temperature.value, top_p.value, 40, 1.1)
89
+
90
  formatted_chat_history = []
91
+ for user_msg, assistant_msg in zip([msg["content"].strip() for msg in chat_history if msg["role"] == "user"],
92
+ [msg["content"].strip() for msg in chat_history if msg["role"] == "assistant"]):
93
+ if user_msg or assistant_msg: # Verify if either message is not empty
94
+ formatted_chat_history.append([user_msg, assistant_msg])
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  return formatted_chat_history, chat_history, ""
97
 
 
98
  submit.click(
99
  fn=update_chatbot,
100
  inputs=[message, chat_history_state],