File size: 2,576 Bytes
a9c3ae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)