File size: 2,245 Bytes
b78ab93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87b420e
 
 
 
0a95db7
ac200eb
 
 
 
87b420e
 
b78ab93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87b420e
 
 
 
b78ab93
 
 
 
 
 
 
5e172c3
e48dd3f
 
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
71
72
73
74
75
76
77
78
79
80
import gradio as gr
from final_funcs import global_vars
from final_agent import create_agent
from messages import instructions, greeting, description
from final_funcs import auth
import os
import time


start_of_chat = True
def add_text(history, text):
    history = history + [(text, None)]
    return history, gr.update(value="", interactive=False)


def bot(history):
    user_input = history[-1][0]
    global start_of_chat
    if start_of_chat:
        start_of_chat = False
        response = greeting
    elif user_input.strip() == '!help':
        response = instructions
    else:
        key = global_vars['OPENAI_API_KEY']
        if key is None:
            yield [("OpenAI client not initialized. Authenticate OpenAI first.", "Try again")]
            return 
        agent_executor = create_agent(key)
        response = agent_executor(user_input, include_run_info=True)
        response = response["output"]

    history[-1][1] = ""
    for character in response:
        history[-1][1] += character
        time.sleep(0.0075)
        yield history


def end_session():
    global_vars['OPENAI_API_KEY'] = None
    sp = None
    device_id = None
    return f"""
            <div style="text-align: center;">
                <span style="color: red; font-size: 30px;">Session Ended. Thank you for using Apollo!</span>
            </div>
            """


with gr.Blocks() as chat:
    gr.Markdown(description)
    
    chatbot = gr.Chatbot([], elem_id="chatbot", height=750)

    with gr.Row():
        txt = gr.Textbox(
            show_label=False,
            placeholder="What would you like to hear?",
            container=False
        )

    txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
        bot, chatbot, chatbot
    )
    txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
    
    logout_button = gr.Button("End Your Session")
    logout_result = gr.Markdown()
    logout_button.click(fn=end_session, outputs=logout_result)

demo = gr.TabbedInterface([auth, chat], ["Authentication Station", "Music Room"], 
                          theme="finlaymacklon/boxy_violet", 
                          css=None
)


demo.queue()
demo.launch()
os.remove(".cache")