Spaces:
Runtime error
Runtime error
File size: 1,908 Bytes
e13d635 73ca839 565fd7a 73ca839 565fd7a 73b667c e13d635 d3d8ae4 bb06522 565fd7a bb06522 565fd7a e13d635 565fd7a e13d635 565fd7a e13d635 7cda732 2e7e3cf e13d635 565fd7a 098f3c6 | 1 2 3 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 | import os
import time
import gradio as gr
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
openai.organization = os.environ["Organization_KEY"]
zhanghao = os.environ["zhanghao"]
mima = os.environ["mima"]
system_message = {"role": "system", "content": "You are a helpful assistant."}
def user(user_message, history):
return "", history + [[user_message, None]]
def bot_3(history, messages_history):
model = "gpt-3.5-turbo"
user_message = history[-1][0]
bot_message, messages_history = ask_gpt(user_message, messages_history, model)
messages_history += [{"role": "assistant", "content": bot_message}]
history[-1][1] = bot_message
time.sleep(1)
return history, messages_history
def ask_gpt(message, messages_history, model):
messages_history += [{"role": "user", "content": message}]
response = openai.ChatCompletion.create(
model=model,
messages=messages_history
)
return response['choices'][0]['message']['content'], messages_history
def init_history(messages_history):
messages_history = []
messages_history += [system_message]
return messages_history
with gr.Blocks() as demo:
gr.Markdown("ChatGPT非科学接入")
with gr.Tab("GPT-3.5"):
chatbot = gr.Chatbot(label ='对话记录')
msg = gr.Textbox(label ='输入框')
sub = gr.Button("发送")
clear = gr.Button("清除")
state = gr.State([])
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot_3, [chatbot, state], [chatbot, state]
)
sub.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot_3, [chatbot, state], [chatbot, state])
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
demo.launch(auth_message = "账号密码发送到了微信上了",auth=(zhanghao, mima))
|