Jeremy Live
commited on
Commit
路
713a6e6
1
Parent(s):
4eccef6
v8
Browse files
app.py
CHANGED
@@ -382,10 +382,11 @@ def create_ui():
|
|
382 |
El asistente est谩 listo para responder tus preguntas sobre la base de datos.
|
383 |
""")
|
384 |
|
385 |
-
# Interfaz de chat -
|
386 |
chatbot = gr.Chatbot(
|
387 |
label="Chat",
|
388 |
-
height=500
|
|
|
389 |
)
|
390 |
|
391 |
# 脕rea de entrada
|
@@ -437,48 +438,56 @@ def create_ui():
|
|
437 |
|
438 |
return demo, chatbot, question_input, submit_button, streaming_output_display
|
439 |
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
"""Add user message to chat history and clear input."""
|
445 |
-
if not user_input.strip():
|
446 |
-
return "", chat_history
|
447 |
-
logger.info(f"User message: {user_input}")
|
448 |
-
return "", chat_history + [[user_input, None]]
|
449 |
-
|
450 |
-
def bot_response(chat_history: List) -> Tuple[List, Dict]:
|
451 |
-
"""Get bot response and update chat history."""
|
452 |
-
if not chat_history or not chat_history[-1][0]:
|
453 |
-
return chat_history, gr.update(visible=False)
|
454 |
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
471 |
|
472 |
-
|
473 |
-
|
474 |
-
inputs=[question_input, chatbot],
|
475 |
-
outputs=[question_input, chatbot],
|
476 |
-
queue=True
|
477 |
-
).then(
|
478 |
-
fn=bot_response,
|
479 |
-
inputs=[chatbot],
|
480 |
-
outputs=[chatbot, streaming_output_display]
|
481 |
-
)
|
482 |
|
483 |
# Configuraci贸n para Hugging Face Spaces
|
484 |
def get_app():
|
|
|
382 |
El asistente est谩 listo para responder tus preguntas sobre la base de datos.
|
383 |
""")
|
384 |
|
385 |
+
# Interfaz de chat - usando el nuevo formato de mensajes
|
386 |
chatbot = gr.Chatbot(
|
387 |
label="Chat",
|
388 |
+
height=500,
|
389 |
+
type="messages" # Usando el nuevo formato de mensajes
|
390 |
)
|
391 |
|
392 |
# 脕rea de entrada
|
|
|
438 |
|
439 |
return demo, chatbot, question_input, submit_button, streaming_output_display
|
440 |
|
441 |
+
def create_application():
|
442 |
+
"""Create and configure the Gradio application."""
|
443 |
+
# Create the UI components
|
444 |
+
demo, chatbot, question_input, submit_button, streaming_output_display = create_ui()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
|
446 |
+
def user_message(user_input: str, chat_history: List) -> Tuple[str, List]:
|
447 |
+
"""Add user message to chat history and clear input."""
|
448 |
+
if not user_input.strip():
|
449 |
+
return "", chat_history
|
450 |
+
logger.info(f"User message: {user_input}")
|
451 |
+
return "", chat_history + [[user_input, None]]
|
452 |
+
|
453 |
+
def bot_response(chat_history: List) -> Tuple[List, Dict]:
|
454 |
+
"""Get bot response and update chat history."""
|
455 |
+
if not chat_history or not chat_history[-1][0]:
|
456 |
+
return chat_history, gr.update(visible=False)
|
457 |
+
|
458 |
+
question = chat_history[-1][0]
|
459 |
+
logger.info(f"Processing question: {question}")
|
460 |
+
return stream_agent_response(question, chat_history[:-1])
|
461 |
+
|
462 |
+
# Event handlers
|
463 |
+
with demo:
|
464 |
+
submit_click = submit_button.click(
|
465 |
+
fn=user_message,
|
466 |
+
inputs=[question_input, chatbot],
|
467 |
+
outputs=[question_input, chatbot],
|
468 |
+
queue=True
|
469 |
+
).then(
|
470 |
+
fn=bot_response,
|
471 |
+
inputs=[chatbot],
|
472 |
+
outputs=[chatbot, streaming_output_display],
|
473 |
+
api_name="ask"
|
474 |
+
)
|
475 |
+
|
476 |
+
question_input.submit(
|
477 |
+
fn=user_message,
|
478 |
+
inputs=[question_input, chatbot],
|
479 |
+
outputs=[question_input, chatbot],
|
480 |
+
queue=True
|
481 |
+
).then(
|
482 |
+
fn=bot_response,
|
483 |
+
inputs=[chatbot],
|
484 |
+
outputs=[chatbot, streaming_output_display]
|
485 |
+
)
|
486 |
+
|
487 |
+
return demo
|
488 |
|
489 |
+
# Create the application
|
490 |
+
demo = create_application()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
491 |
|
492 |
# Configuraci贸n para Hugging Face Spaces
|
493 |
def get_app():
|