File size: 1,953 Bytes
aaec095
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from final_funcs import auth_page, sp_state
from final_agent import create_agent
from messages import instructions, greeting, description, custom_theme, css
import os
import time

from dotenv import load_dotenv
load_dotenv()  
key = os.getenv("OPENAI_API_KEY")


start_of_chat_state = gr.State()

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


def bot(history):    
    user_input = history[-1][0]

    if sp_state.value is None:
        response = "I really want to play music for you, but please authenticate your Spotify first."

    elif start_of_chat_state.value is None or start_of_chat_state.value:
        start_of_chat_state.value = False
        response = greeting
    elif user_input.strip() == '!help': # streaming looks bad
        response = instructions
    else:
        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


with gr.Blocks() as chat_page:
    gr.Markdown(description)
    
    chatbot = gr.Chatbot([], elem_id="chatbot", height=500, label="Apollo 🎵")

    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)


demo = gr.TabbedInterface([auth_page, chat_page], ["Authentication", "Music"], 
                          theme="finlaymacklon/boxy_violet", 
                          #theme=custom_theme,
                          css=None
)


demo.queue() 
demo.launch()