RomZay commited on
Commit
145ec93
·
verified ·
1 Parent(s): e78e0df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -50
app.py CHANGED
@@ -101,56 +101,31 @@ def respond(message, api_key, max_tokens, top_p, temperature):
101
  else:
102
  return history, "Error: " + response.json().get("error", "Unknown error occurred.")
103
 
104
- def render_message(history):
105
- messages_html = """
106
- <div id="chatbox-container" class="chatbox">
107
- <div id="messages">"""
108
-
109
- for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
110
- if user_message:
111
- messages_html += f"<div style='display: flex; align-items: center; margin-bottom: 10px;'>"
112
- if user_pic:
113
- messages_html += f"<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
114
- messages_html += f"{user_message}</div><br>"
115
-
116
- if assistant_message:
117
- messages_html += f"<div style='display: flex; align-items: center; margin-bottom: 10px;'>"
118
- if assistant_pic:
119
- messages_html += f"<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
120
- messages_html += f"{assistant_message}</div><br>"
121
-
122
- messages_html += """
123
- </div>
124
- </div>
125
-
126
- <script>
127
- function saveScrollPosition() {
128
- var chatbox = document.getElementById('chatbox-container');
129
- return chatbox.scrollTop;
130
- }
131
-
132
- function restoreScrollPosition(scrollPosition) {
133
- var chatbox = document.getElementById('chatbox-container');
134
- chatbox.scrollTop = scrollPosition;
135
- }
136
-
137
- function scrollToBottom() {
138
- var chatbox = document.getElementById('chatbox-container');
139
- chatbox.scrollTop = chatbox.scrollHeight;
140
- }
141
-
142
- var previousScrollPosition = saveScrollPosition();
143
 
144
- setTimeout(function() {
145
- restoreScrollPosition(previousScrollPosition);
146
- scrollToBottom();
147
- }, 100);
 
148
 
149
- </script>
150
- """
151
 
152
- return messages_html
153
 
 
 
 
 
 
 
 
154
 
155
  with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid #262626; padding: 10px; background-color: #171717;}") as demo:
156
 
@@ -165,7 +140,7 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
165
  with gr.Column(visible=False) as chat_view:
166
  gr.Markdown("## P-MSQ Chat Interface")
167
 
168
- chatbot_output = gr.HTML(elem_id="chatbox")
169
 
170
  msg_input = gr.Textbox(
171
  show_label=False,
@@ -192,7 +167,9 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
192
 
193
  def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
194
  history, assistant_reply = respond(message, api_key, max_tokens, top_p, temperature)
195
- return render_message(history), history, "", message
 
 
196
 
197
  def regenerate_response(history, last_message, max_tokens, top_p, temperature):
198
  return "", []
@@ -208,8 +185,8 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
208
  return render_message(history), history
209
 
210
  msg_input.submit(user_interaction,
211
- inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],
212
- outputs=[chatbot_output, history_state, msg_input, last_message_state])
213
 
214
  send_btn.click(user_interaction,
215
  inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],
 
101
  else:
102
  return history, "Error: " + response.json().get("error", "Unknown error occurred.")
103
 
104
+ def append_message(message, previous_html):
105
+ new_message_html = ""
106
+
107
+ if message[0]:
108
+ new_message_html += f"<div style='display: flex; align-items: center; margin-bottom: 10px;'>"
109
+ if message[4]:
110
+ new_message_html += f"<img src='{message[4]}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
111
+ new_message_html += f"{message[0]}</div><br>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ if message[1]:
114
+ new_message_html += f"<div style='display: flex; align-items: center; margin-bottom: 10px;'>"
115
+ if message[5]:
116
+ new_message_html += f"<img src='{message[5]}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
117
+ new_message_html += f"{message[1]}</div><br>"
118
 
119
+ return previous_html + new_message_html
 
120
 
 
121
 
122
+ def render_message(history):
123
+ chat_html = ""
124
+
125
+ for message in history:
126
+ chat_html = append_message(message, chat_html)
127
+
128
+ return chat_html
129
 
130
  with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid #262626; padding: 10px; background-color: #171717;}") as demo:
131
 
 
140
  with gr.Column(visible=False) as chat_view:
141
  gr.Markdown("## P-MSQ Chat Interface")
142
 
143
+ chatbot_output = gr.HTML()
144
 
145
  msg_input = gr.Textbox(
146
  show_label=False,
 
167
 
168
  def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
169
  history, assistant_reply = respond(message, api_key, max_tokens, top_p, temperature)
170
+
171
+ updated_html = render_message(history)
172
+ return updated_html, history, "", message
173
 
174
  def regenerate_response(history, last_message, max_tokens, top_p, temperature):
175
  return "", []
 
185
  return render_message(history), history
186
 
187
  msg_input.submit(user_interaction,
188
+ inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],
189
+ outputs=[chatbot_output, history_state, msg_input, last_message_state])
190
 
191
  send_btn.click(user_interaction,
192
  inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],