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

Refactor chat history formatting logic in app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -91,20 +91,23 @@ with gr.Blocks() as demo:
91
  last_role = None
92
  for entry in chat_history:
93
  role = entry["role"]
94
- if role != last_role:
95
- if role == "user":
96
- formatted_chat_history.append([entry["content"], ""])
97
- elif role == "assistant":
98
- formatted_chat_history.append(["", entry["content"]])
99
- last_role = role
100
- else:
101
- if role == "user":
102
- formatted_chat_history[-1][0] += entry["content"]
103
  else:
104
- formatted_chat_history[-1][1] += entry["content"]
 
 
 
105
 
106
  return formatted_chat_history, chat_history, ""
107
 
 
108
  submit.click(
109
  fn=update_chatbot,
110
  inputs=[message, chat_history_state],
 
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],