Spaces:
Build error
Build error
Commit
·
10c1657
1
Parent(s):
2901a7a
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,73 @@
|
|
| 1 |
-
import
|
| 2 |
-
import openai
|
| 3 |
import time
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
|
|
|
| 2 |
import time
|
| 3 |
|
| 4 |
+
import openai
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
load_dotenv()
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
messages = []
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def add_user_text(chat_history, user_text):
|
| 16 |
+
print('user_text_from_typing: ', user_text)
|
| 17 |
+
|
| 18 |
+
global messages
|
| 19 |
+
messages += [{"role":'user', 'content': user_text}]
|
| 20 |
+
|
| 21 |
+
chat_history = chat_history + [(user_text, None)]
|
| 22 |
+
return chat_history, gr.update(value="", interactive=False)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def bot_respond(chat_history, openai_gpt_key):
|
| 26 |
+
global messages
|
| 27 |
+
|
| 28 |
+
if openai_gpt_key is not "":
|
| 29 |
+
openai.api_key = openai_gpt_key
|
| 30 |
+
bot_response = openai.ChatCompletion.create(
|
| 31 |
+
model = 'gpt-3.5-turbo',
|
| 32 |
+
messages = messages,
|
| 33 |
+
)
|
| 34 |
+
bot_text = bot_response["choices"][0]["message"]["content"]
|
| 35 |
+
print("bot_text: ",bot_text)
|
| 36 |
+
|
| 37 |
+
messages = messages + [{"role":'assistant', 'content': bot_text}]
|
| 38 |
+
|
| 39 |
+
print(chat_history)
|
| 40 |
+
chat_history[-1][1] = ""
|
| 41 |
+
for character in bot_text:
|
| 42 |
+
chat_history[-1][1] += character
|
| 43 |
+
time.sleep(0.02)
|
| 44 |
+
yield chat_history
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
# radio = gr.Radio(
|
| 49 |
+
# value='gpt-3.5-turbo',
|
| 50 |
+
# choices=['gpt-3.5-turbo','gpt-4'],
|
| 51 |
+
# label='Model Options')
|
| 52 |
+
|
| 53 |
+
openai_gpt_key = gr.Textbox(label="OpenAI GPT API Key", value="", placeholder="sk..", info = "If Error raised, you have to provide your own GPT keys for this app to function properly",)
|
| 54 |
+
clear_btn = gr.Button("Clear for Restart")
|
| 55 |
+
chat_history = gr.Chatbot([], elem_id="chat_history").style(height=500)
|
| 56 |
+
|
| 57 |
+
with gr.Box():
|
| 58 |
+
user_text = gr.Textbox(
|
| 59 |
+
show_label=False,
|
| 60 |
+
placeholder="Enter text and press enter",
|
| 61 |
+
).style(container=False)
|
| 62 |
+
|
| 63 |
+
user_text.submit(
|
| 64 |
+
add_user_text, [chat_history, user_text], [chat_history, user_text], queue=False).then(
|
| 65 |
+
bot_respond, [chat_history, openai_gpt_key], chat_history).then(
|
| 66 |
+
lambda: gr.update(interactive=True), None, [user_text], queue=False)
|
| 67 |
+
|
| 68 |
+
clear_btn.click(lambda: None, None, chat_history, queue=False)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
demo.queue()
|
| 73 |
+
demo.launch()
|