Spaces:
Paused
Paused
send button disabling
Browse files
app.py
CHANGED
|
@@ -13,7 +13,8 @@ import uuid
|
|
| 13 |
from datetime import datetime, timezone
|
| 14 |
from pymongo import MongoClient
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
MONGODB_URI = os.getenv("MONGODB_URI")
|
| 18 |
MONGODB_DB = os.getenv("MONGODB_DB", "chatbot_db")
|
| 19 |
MONGODB_COLLECTION = os.getenv("MONGODB_COLLECTION", "chat_logs")
|
|
@@ -93,18 +94,20 @@ def initModel():
|
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
-
def start_chat(initial_issue, chat_history, context, support_type, session_id, request: gr.Request):
|
|
|
|
| 97 |
session_id = get_session_id(session_id, request)
|
| 98 |
initial_issue = (initial_issue or "").strip()
|
| 99 |
if not initial_issue or not support_type:
|
| 100 |
return (
|
| 101 |
-
chat_history, context, chat_history, session_id,
|
| 102 |
gr.update(), # initial textbox
|
| 103 |
gr.update(), # radio
|
| 104 |
-
gr.update(interactive=False),
|
| 105 |
-
gr.update(interactive=True)
|
|
|
|
| 106 |
)
|
| 107 |
-
|
| 108 |
opening_response = (
|
| 109 |
f"Oh, you need help with this situation: {initial_issue}. "
|
| 110 |
f"Could you say more?"
|
|
@@ -121,11 +124,12 @@ def start_chat(initial_issue, chat_history, context, support_type, session_id, r
|
|
| 121 |
chat_history,
|
| 122 |
context,
|
| 123 |
chat_history,
|
| 124 |
-
session_id,
|
| 125 |
gr.update(value=initial_issue, interactive=False), # lock initial textbox
|
| 126 |
gr.update(value=support_type, interactive=False), # lock radio
|
| 127 |
gr.update(value="", interactive=True), # enable followup textbox
|
| 128 |
-
gr.update(interactive=False) # disable start button
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
@spaces.GPU()
|
|
@@ -222,11 +226,13 @@ def save_chat_log(session_id, support_type, chat_history, context):
|
|
| 222 |
)
|
| 223 |
|
| 224 |
|
| 225 |
-
def chat_fn(user_input, chat_history, context, support_type, session_id, request: gr.Request):
|
| 226 |
session_id = get_session_id(session_id, request)
|
| 227 |
user_input = (user_input or "").strip()
|
|
|
|
|
|
|
| 228 |
if not user_input:
|
| 229 |
-
return "", chat_history, context, chat_history, session_id
|
| 230 |
gr.Radio(interactive=False, value=support_type)
|
| 231 |
# retrieved_context = retrieve_context(user_input, top_k=3)
|
| 232 |
support_instruction = MODE_INSTRUCTIONS.get(
|
|
@@ -244,7 +250,7 @@ def chat_fn(user_input, chat_history, context, support_type, session_id, request
|
|
| 244 |
context += f"\nUser: {user_input}\nAI: {result}"
|
| 245 |
save_chat_log(session_id, support_type, chat_history, context)
|
| 246 |
|
| 247 |
-
return "", chat_history, context, chat_history, session_id
|
| 248 |
|
| 249 |
|
| 250 |
custom_css = """
|
|
@@ -280,7 +286,18 @@ button[aria-label="Copy all"] {
|
|
| 280 |
|
| 281 |
|
| 282 |
with gr.Blocks() as demo:
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
# gr.Markdown("## Support Chatbot")
|
| 285 |
gr.Markdown("Describe a stressful situation in the past.")
|
| 286 |
initial_issue = gr.Textbox(
|
|
@@ -309,6 +326,11 @@ with gr.Blocks() as demo:
|
|
| 309 |
def toggle_start_button(initial_issue, support_type):
|
| 310 |
ready = bool((initial_issue or "").strip()) and bool(support_type)
|
| 311 |
return gr.update(interactive=ready)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
# def disable_support_type(text, locked, current_value):
|
| 314 |
# if locked:
|
|
@@ -333,41 +355,43 @@ with gr.Blocks() as demo:
|
|
| 333 |
queue=False
|
| 334 |
)
|
| 335 |
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
with gr.Row():
|
| 343 |
-
send_btn = gr.Button("Send")
|
| 344 |
|
| 345 |
start_btn.click(
|
| 346 |
start_chat,
|
| 347 |
-
inputs=[initial_issue, history_state, context_state, support_type, session_state],
|
| 348 |
outputs=[
|
| 349 |
chatbot,
|
| 350 |
context_state,
|
| 351 |
history_state,
|
| 352 |
session_state,
|
|
|
|
| 353 |
initial_issue,
|
| 354 |
support_type,
|
| 355 |
user_input,
|
| 356 |
-
start_btn
|
|
|
|
| 357 |
]
|
| 358 |
)
|
| 359 |
send_btn.click(
|
| 360 |
chat_fn,
|
| 361 |
-
inputs=[user_input, history_state, context_state, support_type, session_state],
|
| 362 |
-
outputs=[user_input, chatbot, context_state, history_state, session_state]
|
| 363 |
)
|
| 364 |
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
|
| 371 |
-
demo.launch(css=custom_css
|
| 372 |
|
| 373 |
|
|
|
|
| 13 |
from datetime import datetime, timezone
|
| 14 |
from pymongo import MongoClient
|
| 15 |
|
| 16 |
+
import time
|
| 17 |
+
SESSION_LIMIT_SECONDS = 300
|
| 18 |
MONGODB_URI = os.getenv("MONGODB_URI")
|
| 19 |
MONGODB_DB = os.getenv("MONGODB_DB", "chatbot_db")
|
| 20 |
MONGODB_COLLECTION = os.getenv("MONGODB_COLLECTION", "chat_logs")
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
+
def start_chat(initial_issue, chat_history, context, support_type, session_id, start_time, request: gr.Request):
|
| 98 |
+
|
| 99 |
session_id = get_session_id(session_id, request)
|
| 100 |
initial_issue = (initial_issue or "").strip()
|
| 101 |
if not initial_issue or not support_type:
|
| 102 |
return (
|
| 103 |
+
chat_history, context, chat_history, session_id, start_time,
|
| 104 |
gr.update(), # initial textbox
|
| 105 |
gr.update(), # radio
|
| 106 |
+
gr.update(value="", interactive=False), # user_input
|
| 107 |
+
gr.update(interactive=True), # start_btn
|
| 108 |
+
gr.update(interactive=False) # send_btn
|
| 109 |
)
|
| 110 |
+
start_time = time.time()
|
| 111 |
opening_response = (
|
| 112 |
f"Oh, you need help with this situation: {initial_issue}. "
|
| 113 |
f"Could you say more?"
|
|
|
|
| 124 |
chat_history,
|
| 125 |
context,
|
| 126 |
chat_history,
|
| 127 |
+
session_id, start_time,
|
| 128 |
gr.update(value=initial_issue, interactive=False), # lock initial textbox
|
| 129 |
gr.update(value=support_type, interactive=False), # lock radio
|
| 130 |
gr.update(value="", interactive=True), # enable followup textbox
|
| 131 |
+
gr.update(interactive=False), # disable start button
|
| 132 |
+
gr.update(interactive=False) # send still disabled until typing
|
| 133 |
)
|
| 134 |
|
| 135 |
@spaces.GPU()
|
|
|
|
| 226 |
)
|
| 227 |
|
| 228 |
|
| 229 |
+
def chat_fn(user_input, chat_history, context, support_type, session_id, start_time, request: gr.Request):
|
| 230 |
session_id = get_session_id(session_id, request)
|
| 231 |
user_input = (user_input or "").strip()
|
| 232 |
+
if start_time is not None and (time.time() - start_time) >= SESSION_LIMIT_SECONDS:
|
| 233 |
+
return "", chat_history, context, chat_history, session_id, start_time, gr.update(interactive=False)
|
| 234 |
if not user_input:
|
| 235 |
+
return "", chat_history, context, chat_history, session_id,start_time, gr.update(interactive=False)
|
| 236 |
gr.Radio(interactive=False, value=support_type)
|
| 237 |
# retrieved_context = retrieve_context(user_input, top_k=3)
|
| 238 |
support_instruction = MODE_INSTRUCTIONS.get(
|
|
|
|
| 250 |
context += f"\nUser: {user_input}\nAI: {result}"
|
| 251 |
save_chat_log(session_id, support_type, chat_history, context)
|
| 252 |
|
| 253 |
+
return "", chat_history, context, chat_history, session_id, start_time, gr.update(interactive=False)
|
| 254 |
|
| 255 |
|
| 256 |
custom_css = """
|
|
|
|
| 286 |
|
| 287 |
|
| 288 |
with gr.Blocks() as demo:
|
| 289 |
+
start_time_state = gr.State(None)
|
| 290 |
+
timer = gr.Timer(1.0)
|
| 291 |
+
def disable_after_timeout(start_time):
|
| 292 |
+
if start_time is None:
|
| 293 |
+
return gr.update()
|
| 294 |
+
expired = (time.time() - start_time) >= SESSION_LIMIT_SECONDS
|
| 295 |
+
return gr.update(interactive=not expired)
|
| 296 |
+
timer.tick(
|
| 297 |
+
disable_after_timeout,
|
| 298 |
+
inputs=[start_time_state],
|
| 299 |
+
outputs=[send_btn]
|
| 300 |
+
)
|
| 301 |
# gr.Markdown("## Support Chatbot")
|
| 302 |
gr.Markdown("Describe a stressful situation in the past.")
|
| 303 |
initial_issue = gr.Textbox(
|
|
|
|
| 326 |
def toggle_start_button(initial_issue, support_type):
|
| 327 |
ready = bool((initial_issue or "").strip()) and bool(support_type)
|
| 328 |
return gr.update(interactive=ready)
|
| 329 |
+
def toggle_send_button(user_input):
|
| 330 |
+
ready = bool((user_input or "").strip())
|
| 331 |
+
return gr.update(interactive=ready)
|
| 332 |
+
|
| 333 |
+
|
| 334 |
|
| 335 |
# def disable_support_type(text, locked, current_value):
|
| 336 |
# if locked:
|
|
|
|
| 355 |
queue=False
|
| 356 |
)
|
| 357 |
|
| 358 |
+
user_input.input(
|
| 359 |
+
toggle_send_button,
|
| 360 |
+
inputs=[user_input],
|
| 361 |
+
outputs=[send_btn],
|
| 362 |
+
queue=False
|
| 363 |
+
)
|
| 364 |
with gr.Row():
|
| 365 |
+
send_btn = gr.Button("Send", interactive=False)
|
| 366 |
|
| 367 |
start_btn.click(
|
| 368 |
start_chat,
|
| 369 |
+
inputs=[initial_issue, history_state, context_state, support_type, session_state, start_time_state],
|
| 370 |
outputs=[
|
| 371 |
chatbot,
|
| 372 |
context_state,
|
| 373 |
history_state,
|
| 374 |
session_state,
|
| 375 |
+
start_time_state,
|
| 376 |
initial_issue,
|
| 377 |
support_type,
|
| 378 |
user_input,
|
| 379 |
+
start_btn,
|
| 380 |
+
send_btn
|
| 381 |
]
|
| 382 |
)
|
| 383 |
send_btn.click(
|
| 384 |
chat_fn,
|
| 385 |
+
inputs=[user_input, history_state, context_state, support_type, session_state, start_time_state],
|
| 386 |
+
outputs=[user_input, chatbot, context_state, history_state, session_state, start_time_state, send_btn]
|
| 387 |
)
|
| 388 |
|
| 389 |
+
user_input.submit(
|
| 390 |
+
chat_fn,
|
| 391 |
+
inputs=[user_input, history_state, context_state, support_type, session_state, start_time_state],
|
| 392 |
+
outputs=[user_input, chatbot, context_state, history_state, session_state, start_time_state, send_btn]
|
| 393 |
+
)
|
| 394 |
|
| 395 |
+
demo.launch(css=custom_css)
|
| 396 |
|
| 397 |
|