alapl063 commited on
Commit
d40b8f6
·
verified ·
1 Parent(s): 9c3b287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -96,6 +96,7 @@ with gr.Blocks(css="""
96
 
97
  chatbot = gr.Chatbot(value=[("", translations["en"]["welcome"])], elem_id="chat-container")
98
 
 
99
  state = gr.State([]) # Store conversation history
100
 
101
  with gr.Row():
@@ -116,7 +117,9 @@ with gr.Blocks(css="""
116
  # Process chatbot response
117
  updated_history, _ = chat_with_bot(message, history)
118
 
119
- return gr.update(value=updated_history), "" # Ensure UI refresh triggers scroll
 
 
120
 
121
 
122
  def button_click(btn_text, history):
@@ -128,27 +131,29 @@ with gr.Blocks(css="""
128
  btn2.click(button_click, inputs=[btn2, chatbot], outputs=[chatbot, user_input])
129
  btn3.click(button_click, inputs=[btn3, chatbot], outputs=[chatbot, user_input])
130
 
131
- # Inject JavaScript for auto-scroll when a new message appears
132
  gr.HTML("""
133
  <script>
134
- function scrollToBottom() {
135
- let chat = document.querySelector('#chat-container');
136
- if (chat) {
137
- setTimeout(() => { chat.scrollTop = chat.scrollHeight; }, 100);
 
138
  }
139
  }
140
 
141
- // Observe changes to chat and scroll when new messages arrive
142
  document.addEventListener("DOMContentLoaded", function() {
143
- let observer = new MutationObserver(scrollToBottom);
144
- let chat = document.querySelector('#chat-container');
145
- if (chat) {
146
- observer.observe(chat, { childList: true, subtree: true });
147
  }
148
  });
149
  </script>
150
  """)
151
 
 
152
 
153
 
154
  demo.launch()
 
96
 
97
  chatbot = gr.Chatbot(value=[("", translations["en"]["welcome"])], elem_id="chat-container")
98
 
99
+
100
  state = gr.State([]) # Store conversation history
101
 
102
  with gr.Row():
 
117
  # Process chatbot response
118
  updated_history, _ = chat_with_bot(message, history)
119
 
120
+ # Ensure UI refresh triggers scroll
121
+ return gr.update(value=updated_history), ""
122
+
123
 
124
 
125
  def button_click(btn_text, history):
 
131
  btn2.click(button_click, inputs=[btn2, chatbot], outputs=[chatbot, user_input])
132
  btn3.click(button_click, inputs=[btn3, chatbot], outputs=[chatbot, user_input])
133
 
134
+
135
  gr.HTML("""
136
  <script>
137
+ function scrollToLatestMessage() {
138
+ let chatMessages = document.querySelectorAll('#chat-container .message');
139
+ if (chatMessages.length > 0) {
140
+ let lastMessage = chatMessages[chatMessages.length - 1];
141
+ lastMessage.scrollIntoView({ behavior: 'smooth', block: 'end' });
142
  }
143
  }
144
 
145
+ // Observe changes in chat messages and scroll automatically
146
  document.addEventListener("DOMContentLoaded", function() {
147
+ let observer = new MutationObserver(scrollToLatestMessage);
148
+ let chatContainer = document.querySelector('#chat-container');
149
+ if (chatContainer) {
150
+ observer.observe(chatContainer, { childList: true, subtree: true });
151
  }
152
  });
153
  </script>
154
  """)
155
 
156
+
157
 
158
 
159
  demo.launch()