Spaces:
Sleeping
Sleeping
Commit
·
2f669da
1
Parent(s):
2b29150
initial commit
Browse files- app.py +48 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Set up OpenAI API key
|
| 7 |
+
openai.api_key = os.getenv('gpt-3.5-turb_key')
|
| 8 |
+
|
| 9 |
+
system_message = {"role": "system", "content": "You are a helpful assistant."}
|
| 10 |
+
|
| 11 |
+
with gr.Blocks() as demo:
|
| 12 |
+
chatbot = gr.Chatbot()
|
| 13 |
+
msg = gr.Textbox()
|
| 14 |
+
clear = gr.Button("Clear")
|
| 15 |
+
|
| 16 |
+
state = gr.State([])
|
| 17 |
+
|
| 18 |
+
def user(user_message, history):
|
| 19 |
+
return "", history + [[user_message, None]]
|
| 20 |
+
|
| 21 |
+
def bot(history, messages_history):
|
| 22 |
+
user_message = history[-1][0]
|
| 23 |
+
bot_message, messages_history = ask_gpt(user_message, messages_history)
|
| 24 |
+
messages_history += [{"role": "assistant", "content": bot_message}]
|
| 25 |
+
history[-1][1] = bot_message
|
| 26 |
+
time.sleep(1)
|
| 27 |
+
return history, messages_history
|
| 28 |
+
|
| 29 |
+
def ask_gpt(message, messages_history):
|
| 30 |
+
messages_history += [{"role": "user", "content": message}]
|
| 31 |
+
response = openai.ChatCompletion.create(
|
| 32 |
+
model="gpt-3.5-turbo",
|
| 33 |
+
messages=messages_history
|
| 34 |
+
)
|
| 35 |
+
return response['choices'][0]['message']['content'], messages_history
|
| 36 |
+
|
| 37 |
+
def init_history(messages_history):
|
| 38 |
+
messages_history = []
|
| 39 |
+
messages_history += [system_message]
|
| 40 |
+
return messages_history
|
| 41 |
+
|
| 42 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 43 |
+
bot, [chatbot, state], [chatbot, state]
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
|
| 47 |
+
|
| 48 |
+
demo.launch(auth=([("gemaoheng", "Password@123456"),("dongchl", "Password@123456")]))
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openai
|