Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from final_funcs import global_vars
|
| 3 |
+
from final_agent import create_agent
|
| 4 |
+
from messages import instructions, greeting, description
|
| 5 |
+
from final_funcs import auth
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
start_of_chat = True
|
| 11 |
+
def add_text(history, text):
|
| 12 |
+
history = history + [(text, None)]
|
| 13 |
+
return history, gr.update(value="", interactive=False)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def bot(history):
|
| 17 |
+
user_input = history[-1][0]
|
| 18 |
+
global start_of_chat
|
| 19 |
+
if start_of_chat:
|
| 20 |
+
start_of_chat = False
|
| 21 |
+
response = greeting
|
| 22 |
+
elif user_input.strip() == '!help':
|
| 23 |
+
response = instructions
|
| 24 |
+
else:
|
| 25 |
+
key = global_vars['OPENAI_API_KEY']
|
| 26 |
+
if key is None:
|
| 27 |
+
yield [("OpenAI client not initialized. Authenticate OpenAI first.", "Try again")]
|
| 28 |
+
return
|
| 29 |
+
agent_executor = create_agent(key)
|
| 30 |
+
response = agent_executor(user_input, include_run_info=True)
|
| 31 |
+
response = response["output"]
|
| 32 |
+
|
| 33 |
+
history[-1][1] = ""
|
| 34 |
+
for character in response:
|
| 35 |
+
history[-1][1] += character
|
| 36 |
+
time.sleep(0.0075)
|
| 37 |
+
yield history
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
with gr.Blocks() as chat:
|
| 41 |
+
gr.Markdown(description)
|
| 42 |
+
|
| 43 |
+
chatbot = gr.Chatbot([], elem_id="chatbot", height=750)
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
txt = gr.Textbox(
|
| 47 |
+
show_label=False,
|
| 48 |
+
placeholder="What would you like to hear?",
|
| 49 |
+
container=False
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 53 |
+
bot, chatbot, chatbot
|
| 54 |
+
)
|
| 55 |
+
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
demo = gr.TabbedInterface([auth, chat], ["Authentication Station", "Music Room"],
|
| 59 |
+
theme="finlaymacklon/boxy_violet",
|
| 60 |
+
css=None
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
demo.queue()
|
| 65 |
+
demo.launch()
|