chatbot / app.py
vmoras's picture
Initial comit
a9c3ae7
import gradio
from functions import *
with gr.Blocks() as app:
msg_history = gr.State() # Messages with the format used by OpenAI
waiting_time = gr.State([]) # Seconds needed to get each answer
is_working = gr.State(True) # It's updated in each sumit, it saves if the API is working
# ------------------------------------------- Welcome -------------------------------------------
with gr.Row() as welcoming:
with gr.Column():
done = gr.State('')
with gr.Row():
gr.Markdown(
"""
# Holiii
Bienvenido al chat. Soy Roomie. Me gustaria saber un poco mas de ti antes de continuar.
"""
)
with gr.Row():
with gr.Column():
name = gr.Textbox(lines=1, max_lines=1, label='¿Cual es tu nombre?')
age = gr.Number(label='¿Cual es tu edad?')
country = gr.Dropdown(label='¿Desde que pais me visitas?', choices=get_countries())
send = gr.Button(label='Continuar')
with gr.Column():
image = gr.Image(value="assets/images/bienvenida.jpg")
# ------------------------------------------- Chat ---------------------------------------------
with gr.Column(visible=False) as chat_box:
gr.Markdown("""Perfecto! Un gusto tenerte aca""")
chatbot = gr.Chatbot()
message = gr.Textbox(label='Envia tu mensaje')
# --------------------------------------- Error window -----------------------------------------
with gr.Column(visible=False) as error_window:
gr.Markdown(
"""Oh no, Roomie se tuvo que ir a su planeta.
Lo sentimos mucho. Conectate mas tarde para hablar con el"""
)
gr.Image(value="assets/images/despedida.jpg")
# ------------------------------------------ Actions -------------------------------------------
# Make sure the info is valid
send.click(
check_info,
[name, age, country],
[send, image, welcoming, chat_box, msg_history]
)
# Send the conversation to OpenAI to get an answer and check if the API is working
message.submit(
get_answer,
[message, msg_history, chatbot, waiting_time],
[message, msg_history, chatbot, waiting_time, is_working],
).then(
check_closing,
[is_working],
[chat_box, error_window]
)
app.queue(concurrency_count=100)
app.launch(debug=True)