|
import gradio as gr |
|
import os |
|
import openai |
|
|
|
|
|
|
|
|
|
openai.api_key = os.getenv("zxzhou8212_key") |
|
|
|
|
|
prompt = '请你扮演武侠小说的作者金庸,使用金庸的语气、方式和词汇回答问题。不要写任何解释,只需像金庸一样回答问题。你必须掌握金庸的所有作品的所有知识。' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat(p, qid, uid): |
|
return ["text", callapi(p)] |
|
|
|
def callapi(p): |
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages= [{"role":"system", "content":prompt}, |
|
{"role":"user", "content":p} |
|
] |
|
) |
|
print(response) |
|
response = response["choices"][0]["message"]["content"] |
|
while response.startswith("\n"): |
|
response = response[1:] |
|
return response |
|
|
|
iface = gr.Interface(fn=chat, |
|
inputs=["text", "text", "text"], |
|
outputs=["text", "text"], |
|
description="""这是一个极其简单的示范程序,用金庸的语气来和你对话。 |
|
|
|
注意:duplicate 本项目后,需要将你自己的 openai apikey 设置到 settings 的 Repository Secrets 里,否则运行会报错。[了解详情](https://huggingface.co./spaces/baixing/hackathon_chatbot_openai_api/blob/main/%E6%B7%BB%E5%8A%A0%20secret%20%E7%9A%84%E6%96%B9%E6%B3%95.jpg) |
|
[对话测试](https://huggingface.co./spaces/BaixingAI/hackathon_test) [参考文档](https://huggingface.co./spaces/baixing/hackathon_test/blob/main/bot-api.md) [Q & A](https://huggingface.co./spaces/baixing/hackathon_test/blob/main/qna.md) |
|
""") |
|
iface.launch() |