Spaces:
Runtime error
Runtime error
update password
Browse files
app.py
CHANGED
|
@@ -1,52 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
-
import random
|
| 4 |
-
import time
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 8 |
openai.organization = os.environ["Organization_KEY"]
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
openai.organization = "org-Gj9KBS6PafLRg7PzdOO6nZ2W"
|
| 12 |
system_message = {"role": "system", "content": "You are a helpful assistant."}
|
| 13 |
|
| 14 |
-
with gr.Blocks() as demo:
|
| 15 |
-
chatbot = gr.Chatbot()
|
| 16 |
-
msg = gr.Textbox()
|
| 17 |
-
sub = gr.Button("发送")
|
| 18 |
-
clear = gr.Button("清除")
|
| 19 |
-
state = gr.State([])
|
| 20 |
-
|
| 21 |
-
def user(user_message, history):
|
| 22 |
-
return "", history + [[user_message, None]]
|
| 23 |
-
|
| 24 |
-
def bot(history, messages_history):
|
| 25 |
-
user_message = history[-1][0]
|
| 26 |
-
bot_message, messages_history = ask_gpt(user_message, messages_history)
|
| 27 |
-
messages_history += [{"role": "assistant", "content": bot_message}]
|
| 28 |
-
history[-1][1] = bot_message
|
| 29 |
-
time.sleep(1)
|
| 30 |
-
return history, messages_history
|
| 31 |
-
|
| 32 |
-
def ask_gpt(message, messages_history):
|
| 33 |
-
messages_history += [{"role": "user", "content": message}]
|
| 34 |
-
response = openai.ChatCompletion.create(
|
| 35 |
-
model=model,
|
| 36 |
-
messages=messages_history
|
| 37 |
-
)
|
| 38 |
-
return response['choices'][0]['message']['content'], messages_history
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return messages_history
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import openai
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 8 |
openai.organization = os.environ["Organization_KEY"]
|
| 9 |
+
zhanghao = os.environ["zhanghao"]
|
| 10 |
+
mima = os.environ["mimamimamima"]
|
| 11 |
|
|
|
|
| 12 |
system_message = {"role": "system", "content": "You are a helpful assistant."}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def user(user_message, history):
|
| 16 |
+
return "", history + [[user_message, None]]
|
| 17 |
+
|
|
|
|
| 18 |
|
| 19 |
+
def bot_3(history, messages_history):
|
| 20 |
+
model = "gpt-3.5-turbo"
|
| 21 |
+
user_message = history[-1][0]
|
| 22 |
+
bot_message, messages_history = ask_gpt(user_message, messages_history, model)
|
| 23 |
+
messages_history += [{"role": "assistant", "content": bot_message}]
|
| 24 |
+
history[-1][1] = bot_message
|
| 25 |
+
time.sleep(1)
|
| 26 |
+
return history, messages_history
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def ask_gpt(message, messages_history, model):
|
| 30 |
+
messages_history += [{"role": "user", "content": message}]
|
| 31 |
+
response = openai.ChatCompletion.create(
|
| 32 |
+
model=model,
|
| 33 |
+
messages=messages_history
|
| 34 |
)
|
| 35 |
+
return response['choices'][0]['message']['content'], messages_history
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def init_history(messages_history):
|
| 39 |
+
messages_history = []
|
| 40 |
+
messages_history += [system_message]
|
| 41 |
+
return messages_history
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("ChatGPT非科学接入")
|
| 46 |
+
with gr.Tab("GPT-3.5"):
|
| 47 |
+
chatbot = gr.Chatbot()
|
| 48 |
+
msg = gr.Textbox('输入栏')
|
| 49 |
+
sub = gr.Button("发送")
|
| 50 |
+
clear = gr.Button("清除")
|
| 51 |
+
state = gr.State([])
|
| 52 |
+
|
| 53 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 54 |
+
bot_3, [chatbot, state], [chatbot, state]
|
| 55 |
+
)
|
| 56 |
+
sub.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 57 |
+
bot_3, [chatbot, state], [chatbot, state])
|
| 58 |
+
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
|
| 59 |
|
| 60 |
+
demo.launch(auth=(zhanghao, mima))
|