Spaces:
Runtime error
Runtime error
| 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") |