Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 132 |
gr.HTML("""
|
| 133 |
<script>
|
| 134 |
-
function
|
| 135 |
-
let
|
| 136 |
-
if (
|
| 137 |
-
|
|
|
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
-
// Observe changes
|
| 142 |
document.addEventListener("DOMContentLoaded", function() {
|
| 143 |
-
let observer = new MutationObserver(
|
| 144 |
-
let
|
| 145 |
-
if (
|
| 146 |
-
observer.observe(
|
| 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()
|