Spaces:
Sleeping
Sleeping
File size: 1,443 Bytes
5370780 6addd48 84bd6e0 2ea8b8c b1ed013 6addd48 b1ed013 6addd48 b1ed013 e8b43aa b1ed013 12f5a43 2ea8b8c 6addd48 b1ed013 6addd48 b1ed013 6addd48 b1ed013 aab700e b1ed013 38af40f |
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 |
import gradio as gr
import openai
# 系统提示
system_prompt = [
{
"role": "system",
"content": "你是一个非常厉害的音乐制作人,毕业于伯克利音乐学院,专业是音乐制作,你在和弦编配上有独特的见解,擅长用局部离调的方式营造歌曲给人的新鲜感"
}
]
# 清空用户消息
def clear(user_message, chat_history):
return "", chat_history + [[user_message, None]]
# 响应用户消息
def respond(openai_apikey, chat_history):
openai.api_key = openai_apikey
if chat_history is None:
chat_history = []
re_messages = system_prompt
for chat in chat_history:
re_messages.append({"role": "user", "content": chat[0]})
re_messages.append({"role": "assistant", "content": chat[1]})
re_chat_completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=re_messages,
temperature=0.7
)
chat_history[-1][1] = ""
for re_chunk in re_chat_completion.choices:
chat_history[-1][1] += re_chunk.delta.get("content", "")
return chat_history
with gr.Blocks() as demo:
openai_apikey = gr.Textbox(label="输入你的 OpenAI 的 API Key")
chatbot = gr.Chatbot(label="历史对话")
msg = gr.Textbox(label="输入消息,按回车发送")
msg.submit(clear, [msg, chatbot],[msg, chatbot]).then(
respond, openai_apikey, chatbot
)
demo.launch() |