Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -387,62 +387,117 @@ def main():
|
|
| 387 |
print("Failed to initialize encoder. Exiting.")
|
| 388 |
return # Exit if encoder initialization fails
|
| 389 |
|
| 390 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
if not message.strip():
|
| 392 |
return history, ""
|
| 393 |
-
|
|
|
|
|
|
|
| 394 |
return history + [{"role": "user", "content": message}, {"role": "assistant", "content": response}], ""
|
| 395 |
|
| 396 |
-
|
|
|
|
| 397 |
bot.reset_state()
|
| 398 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
with gr.
|
| 405 |
-
gr.
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
)
|
| 414 |
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
)
|
| 428 |
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
[],
|
| 432 |
-
[
|
| 433 |
)
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
|
|
|
| 443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
demo.launch(share=True, debug=True)
|
| 445 |
|
| 446 |
-
|
| 447 |
if __name__ == "__main__":
|
| 448 |
main()
|
|
|
|
| 387 |
print("Failed to initialize encoder. Exiting.")
|
| 388 |
return # Exit if encoder initialization fails
|
| 389 |
|
| 390 |
+
def start_chat_with_name(name_input_value):
|
| 391 |
+
if not name_input_value.strip():
|
| 392 |
+
# Se il nome è vuoto, non procedere e magari mostra un avviso (opzionale)
|
| 393 |
+
# Qui manteniamo visibile la sezione nome e nascosta quella chat
|
| 394 |
+
# e restituiamo un messaggio di errore nel chatbot (che però è ancora nascosto)
|
| 395 |
+
# o potremmo aggiornare un componente di testo di errore separato.
|
| 396 |
+
# Per semplicità, per ora ci aspettiamo che l'utente inserisca un nome.
|
| 397 |
+
# Potresti anche disabilitare il bottone "Inizia" se il campo nome è vuoto.
|
| 398 |
+
# Questo return presuppone che il chatbot sia già stato creato ma nascosto.
|
| 399 |
+
# E che il messaggio di errore venga mostrato in un campo apposito, o si gestisca diversamente.
|
| 400 |
+
# Per ora, se il nome è vuoto, non facciamo nulla e l'utente resta sulla schermata del nome.
|
| 401 |
+
return None, gr.update(visible=True), gr.update(visible=False), [{"role": "assistant", "content": "Per favore, inserisci un nome per continuare."}], ""
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
# Nome inserito, aggiorniamo lo stato e la visibilità delle sezioni
|
| 405 |
+
personalized_welcome = bot.get_personalized_welcome_message(name_input_value)
|
| 406 |
+
return name_input_value, gr.update(visible=False), gr.update(visible=True), [{"role": "assistant", "content": personalized_welcome}], ""
|
| 407 |
+
|
| 408 |
+
def update_chatbot_interface(message, history, current_user_name):
|
| 409 |
+
if not current_user_name: # Controllo di sicurezza
|
| 410 |
+
return history + [{"role": "assistant", "content": "Errore: nome utente non impostato."}], ""
|
| 411 |
if not message.strip():
|
| 412 |
return history, ""
|
| 413 |
+
|
| 414 |
+
# Passa current_user_name al tuo bot
|
| 415 |
+
response = bot.respond(message, history, current_user_name)
|
| 416 |
return history + [{"role": "user", "content": message}, {"role": "assistant", "content": response}], ""
|
| 417 |
|
| 418 |
+
|
| 419 |
+
def reset_chat_interface(current_user_name): # current_user_name potrebbe essere usato per un reset personalizzato
|
| 420 |
bot.reset_state()
|
| 421 |
+
# Opzione 1: Resetta la chat ma mantiene l'utente loggato e con il nome inserito
|
| 422 |
+
personalized_welcome = bot.get_personalized_welcome_message(current_user_name) if current_user_name else bot.get_initial_welcome_message()
|
| 423 |
+
return [{"role": "assistant", "content": personalized_welcome}], "", gr.update(visible=False), gr.update(visible=True)
|
| 424 |
+
# Opzione 2: Resetta TUTTO, tornando alla schermata di inserimento nome (più complesso)
|
| 425 |
+
# In tal caso, dovresti ritornare anche i valori per user_name_state (a None) e le visibilità invertite
|
| 426 |
+
# return [{"role": "assistant", "content": bot.get_initial_welcome_message()}], "", gr.update(visible=True), gr.update(visible=False), None
|
| 427 |
+
|
| 428 |
|
| 429 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 430 |
+
user_name_state = gr.State(None) # Stato per memorizzare il nome dell'utente
|
| 431 |
+
|
| 432 |
+
# --- Sezione Inserimento Nome ---
|
| 433 |
+
with gr.Column(elem_id="name_input_section", visible=True) as name_section:
|
| 434 |
+
gr.Markdown("## Benvenuto/a!")
|
| 435 |
+
gr.Markdown("Sono Marianna, 'La Testa di Napoli'. Prima di iniziare, dimmi come ti chiami.")
|
| 436 |
+
name_input_tb = gr.Textbox(label="Il tuo nome di battesimo:", placeholder="Es. Sofia")
|
| 437 |
+
start_chat_btn = gr.Button("Inizia a chattare con Marianna")
|
| 438 |
+
|
| 439 |
+
# --- Sezione Chat Principale (inizialmente nascosta) ---
|
| 440 |
+
with gr.Column(elem_id="chat_interface_section", visible=False) as chat_section:
|
| 441 |
+
with gr.Row():
|
| 442 |
+
gr.Markdown("## Chatta con Marianna - 'La Testa di Napoli'")
|
| 443 |
+
|
| 444 |
+
with gr.Row():
|
| 445 |
+
gr.Image("marianna-102.jpeg", # Assicurati che l'immagine sia accessibile
|
| 446 |
+
elem_id="marianna-image",
|
| 447 |
+
width=250)
|
| 448 |
+
|
| 449 |
+
# Il valore iniziale del chatbot verrà impostato da start_chat_with_name
|
| 450 |
+
chatbot_display = gr.Chatbot(
|
| 451 |
+
label="Chat",
|
| 452 |
+
height=500,
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
chat_message_input = gr.Textbox(
|
| 456 |
+
label="Il tuo messaggio:",
|
| 457 |
+
placeholder="Scrivi il tuo messaggio qui...",
|
| 458 |
+
container=False
|
| 459 |
)
|
| 460 |
|
| 461 |
+
with gr.Row():
|
| 462 |
+
clear_button = gr.Button("Clicca qui per ricominciare la chat")
|
| 463 |
+
|
| 464 |
+
# Get example keys safely
|
| 465 |
+
example_keys = bot.get_safe_example_keys()
|
| 466 |
+
if example_keys:
|
| 467 |
+
gr.Examples(
|
| 468 |
+
examples=[key for key in example_keys],
|
| 469 |
+
inputs=chat_message_input,
|
| 470 |
+
label="Suggerimenti:"
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
+
# --- Logica degli Eventi ---
|
| 474 |
+
start_chat_btn.click(
|
| 475 |
+
fn=start_chat_with_name,
|
| 476 |
+
inputs=[name_input_tb],
|
| 477 |
+
outputs=[user_name_state, name_section, chat_section, chatbot_display, chat_message_input] # Aggiorna stato, visibilità e chatbot
|
| 478 |
)
|
| 479 |
|
| 480 |
+
chat_message_input.submit(
|
| 481 |
+
fn=update_chatbot_interface,
|
| 482 |
+
inputs=[chat_message_input, chatbot_display, user_name_state], # Passa lo stato del nome
|
| 483 |
+
outputs=[chatbot_display, chat_message_input]
|
| 484 |
)
|
| 485 |
|
| 486 |
+
clear_button.click(
|
| 487 |
+
fn=reset_chat_interface,
|
| 488 |
+
inputs=[user_name_state], # Passa il nome per un eventuale messaggio personalizzato al reset
|
| 489 |
+
# Outputs per resettare la chat e tornare alla schermata del nome:
|
| 490 |
+
# outputs=[chatbot_display, chat_message_input, name_section, chat_section, user_name_state]
|
| 491 |
+
# Outputs per resettare solo la chat mantenendo l'utente:
|
| 492 |
+
outputs=[chatbot_display, chat_message_input, name_section, chat_section] # Assumendo che reset_chat_interface gestisca la visibilità
|
| 493 |
+
# e opzionalmente un quinto per user_name_state se lo resettasse
|
| 494 |
+
outputs=[chatbot_display, chat_message_input, name_section, chat_section]
|
| 495 |
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
# Avvia l'applicazione Gradio con autenticazione
|
| 499 |
+
# Sostituisci "tuo_username" e "tua_password" con credenziali reali e sicure
|
| 500 |
demo.launch(share=True, debug=True)
|
| 501 |
|
|
|
|
| 502 |
if __name__ == "__main__":
|
| 503 |
main()
|