Spaces:
Paused
Paused
| 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() | |