sjw's picture
Rename final_app.py to app.py
024d8b6
raw
history blame
2.3 kB
# Standard library imports
import os
import time
# Related third party imports
import gradio as gr
from dotenv import load_dotenv
# Local application/library specific imports
from final_funcs import auth_page, SP_STATE
from final_agent import create_agent
from final_feedback import feedback_page
from final_msgs import INSTRUCTIONS, GREETING, CHAT_HEADER, WARNING
load_dotenv()
KEY = os.getenv("OPENAI_API_KEY")
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 len(history) == 1: # this is the first message from the user
response = GREETING
elif SP_STATE.value is None:
response = WARNING
elif user_input.strip() == '!help': # TODO: streaming !help message looks bad
response = INSTRUCTIONS
else:
response = agent_executor(user_input, include_run_info=True)
response = response["output"]
history[-1][1] = ""
for character in response:
history[-1][1] += character
gr.update(interactive=True)
time.sleep(0.0075)
yield history
with gr.Blocks() as chat_page:
gr.Markdown(CHAT_HEADER)
agent_executor = create_agent(KEY)
chatbot = gr.Chatbot([], elem_id="chatbot", height=400, 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)
gr.Examples(["Play CRAZY by AXL",
"I'm feeling great today, match my vibe",
"Make me a relaxing playlist of SZA-like songs"],
inputs=[txt], label="")
with gr.Accordion(label="Commands & Examples πŸ“œ", open=False):
gr.Markdown(INSTRUCTIONS)
apollo = gr.TabbedInterface([chat_page, auth_page, feedback_page],
["Music", "Authentication", "Feedback"],
theme = "finlaymacklon/boxy_violet")
apollo.queue()
apollo.launch()