Spaces:
Paused
Paused
Update app.py via AI Editor
Browse files
app.py
CHANGED
@@ -236,7 +236,6 @@ def left_navbar_static():
|
|
236 |
], style={"padding": "1rem", "backgroundColor": "#f8f9fa", "height": "100vh", "overflowY": "auto"})
|
237 |
|
238 |
def chat_box_card():
|
239 |
-
# Use a scrollable vertical container for chat history
|
240 |
return dbc.Card(
|
241 |
dbc.CardBody([
|
242 |
html.Div(
|
@@ -330,6 +329,21 @@ app.clientside_callback(
|
|
330 |
State('user-input', 'value')
|
331 |
)
|
332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
def _is_supported_doc(filename):
|
334 |
ext = os.path.splitext(filename)[1].lower()
|
335 |
return ext in [".txt", ".pdf", ".md", ".docx", ".xlsx"]
|
@@ -399,6 +413,7 @@ def assign_session_id(_):
|
|
399 |
Output("stream-interval", "disabled"),
|
400 |
Output("stream-interval", "n_intervals"),
|
401 |
Output("user-input", "value"),
|
|
|
402 |
Input("session-id", "data"),
|
403 |
Input("send-btn", "n_clicks"),
|
404 |
Input("file-upload", "contents"),
|
@@ -406,9 +421,10 @@ def assign_session_id(_):
|
|
406 |
Input('hidden-send', 'n_clicks'),
|
407 |
State("file-upload", "filename"),
|
408 |
State("user-input", "value"),
|
|
|
409 |
prevent_initial_call=False
|
410 |
)
|
411 |
-
def main_callback(session_id, send_clicks, file_contents, stream_n, hidden_send_clicks, file_names, user_input):
|
412 |
trigger = callback_context.triggered[0]['prop_id'].split('.')[0] if callback_context.triggered else ""
|
413 |
session_id = session_id or get_session_id()
|
414 |
session_lock = get_session_lock(session_id)
|
@@ -551,7 +567,7 @@ def main_callback(session_id, send_clicks, file_contents, stream_n, hidden_send_
|
|
551 |
chat_cards = []
|
552 |
for msg in chat_history:
|
553 |
chat_cards.append(chat_message_card(msg['content'], is_user=(msg['role'] == "user")))
|
554 |
-
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, no_update
|
555 |
|
556 |
send_triggered = False
|
557 |
if trigger == "send-btn" or trigger == "hidden-send":
|
@@ -650,7 +666,8 @@ def main_callback(session_id, send_clicks, file_contents, stream_n, hidden_send_
|
|
650 |
"",
|
651 |
False,
|
652 |
stream_n+1,
|
653 |
-
no_update
|
|
|
654 |
)
|
655 |
else:
|
656 |
chat_cards = []
|
@@ -663,7 +680,8 @@ def main_callback(session_id, send_clicks, file_contents, stream_n, hidden_send_
|
|
663 |
"",
|
664 |
True,
|
665 |
0,
|
666 |
-
no_update
|
|
|
667 |
)
|
668 |
|
669 |
chat_history = state.get("messages", [])
|
@@ -673,11 +691,11 @@ def main_callback(session_id, send_clicks, file_contents, stream_n, hidden_send_
|
|
673 |
for msg in chat_history:
|
674 |
chat_cards.append(chat_message_card(msg['content'], is_user=(msg['role'] == "user")))
|
675 |
if trigger == "send-btn" or trigger == "hidden-send":
|
676 |
-
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, ""
|
677 |
elif trigger == "file-upload":
|
678 |
-
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, no_update
|
679 |
else:
|
680 |
-
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, no_update
|
681 |
|
682 |
@app_flask.after_request
|
683 |
def set_session_cookie(resp):
|
|
|
236 |
], style={"padding": "1rem", "backgroundColor": "#f8f9fa", "height": "100vh", "overflowY": "auto"})
|
237 |
|
238 |
def chat_box_card():
|
|
|
239 |
return dbc.Card(
|
240 |
dbc.CardBody([
|
241 |
html.Div(
|
|
|
329 |
State('user-input', 'value')
|
330 |
)
|
331 |
|
332 |
+
# Clientside callback to scroll chat window to bottom when scroll-bottom is incremented
|
333 |
+
app.clientside_callback(
|
334 |
+
"""
|
335 |
+
function(scrollIndex) {
|
336 |
+
var chatContainer = document.getElementById('chat-window-container');
|
337 |
+
if (chatContainer) {
|
338 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
339 |
+
}
|
340 |
+
return null;
|
341 |
+
}
|
342 |
+
""",
|
343 |
+
Output('clear-input', 'data'), # dummy output
|
344 |
+
Input('scroll-bottom', 'data')
|
345 |
+
)
|
346 |
+
|
347 |
def _is_supported_doc(filename):
|
348 |
ext = os.path.splitext(filename)[1].lower()
|
349 |
return ext in [".txt", ".pdf", ".md", ".docx", ".xlsx"]
|
|
|
413 |
Output("stream-interval", "disabled"),
|
414 |
Output("stream-interval", "n_intervals"),
|
415 |
Output("user-input", "value"),
|
416 |
+
Output("scroll-bottom", "data"),
|
417 |
Input("session-id", "data"),
|
418 |
Input("send-btn", "n_clicks"),
|
419 |
Input("file-upload", "contents"),
|
|
|
421 |
Input('hidden-send', 'n_clicks'),
|
422 |
State("file-upload", "filename"),
|
423 |
State("user-input", "value"),
|
424 |
+
State("scroll-bottom", "data"),
|
425 |
prevent_initial_call=False
|
426 |
)
|
427 |
+
def main_callback(session_id, send_clicks, file_contents, stream_n, hidden_send_clicks, file_names, user_input, scroll_bottom):
|
428 |
trigger = callback_context.triggered[0]['prop_id'].split('.')[0] if callback_context.triggered else ""
|
429 |
session_id = session_id or get_session_id()
|
430 |
session_lock = get_session_lock(session_id)
|
|
|
567 |
chat_cards = []
|
568 |
for msg in chat_history:
|
569 |
chat_cards.append(chat_message_card(msg['content'], is_user=(msg['role'] == "user")))
|
570 |
+
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, no_update, scroll_bottom+1
|
571 |
|
572 |
send_triggered = False
|
573 |
if trigger == "send-btn" or trigger == "hidden-send":
|
|
|
666 |
"",
|
667 |
False,
|
668 |
stream_n+1,
|
669 |
+
no_update,
|
670 |
+
scroll_bottom+1
|
671 |
)
|
672 |
else:
|
673 |
chat_cards = []
|
|
|
680 |
"",
|
681 |
True,
|
682 |
0,
|
683 |
+
no_update,
|
684 |
+
scroll_bottom+1
|
685 |
)
|
686 |
|
687 |
chat_history = state.get("messages", [])
|
|
|
691 |
for msg in chat_history:
|
692 |
chat_cards.append(chat_message_card(msg['content'], is_user=(msg['role'] == "user")))
|
693 |
if trigger == "send-btn" or trigger == "hidden-send":
|
694 |
+
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, "", scroll_bottom+1
|
695 |
elif trigger == "file-upload":
|
696 |
+
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, no_update, scroll_bottom+1
|
697 |
else:
|
698 |
+
return upload_cards, chat_cards, error, (not state.get("streaming", False)), 0, no_update, scroll_bottom
|
699 |
|
700 |
@app_flask.after_request
|
701 |
def set_session_cookie(resp):
|