Spaces:
Paused
Paused
import openai | |
import json | |
import os | |
openai.api_key = os.getenv('API_KEY') | |
def ask(question, history): | |
history = history + [question] | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role":"user" if i%2==0 else "assistant", "content":content} | |
for i,content in enumerate(history) | |
] | |
)["choices"][0]["message"]["content"] | |
while response.startswith("\n"): | |
response = response[1:] | |
except Exception as e: | |
print(e) | |
response = 'Timeout! Please wait a few minutes and retry' | |
history = history + [response] | |
with open("dialogue.txt", "a", encoding='utf-8') as f: | |
f.write(json.dumps(history, ensure_ascii=False)+"\n") | |
return history | |
import gradio as gr | |
def predict(question, history=[]): | |
history = ask(question, history) | |
response = [(history[i].replace("\n","<br>"),history[i+1].replace("\n","<br>")) for i in range(0,len(history)-1,2)] | |
return "", history, response | |
with gr.Blocks() as demo: | |
examples = [ | |
['200字介绍一下凯旋门:'], | |
['网上购物有什么小窍门?'], | |
['补全下述对三亚的介绍:\n三亚位于海南岛的最南端,是'], | |
['将这句文言文翻译成英语:"逝者如斯夫,不舍昼夜。"'], | |
['Question: What\'s the best winter resort city? User: A 10-year professional traveler. Answer: '], | |
['How to help my child to make friends with his classmates? answer this question step by step:'], | |
['polish the following statement for a paper: In this section, we perform case study to give a more intuitive demonstration of our proposed strategies and corresponding explanation.'], | |
] | |
gr.Markdown( | |
""" | |
朋友你好, | |
这是我利用[gradio](https://gradio.app/creating-a-chatbot/)编写的一个小网页,用于以网页的形式给大家分享ChatGPT请求服务,希望你玩的开心 | |
p.s. 响应时间和问题复杂程度相关,<del>一般能在10~20秒内出结果</del>用了新的api已经提速到大约5秒内了 | |
""") | |
chatbot = gr.Chatbot() | |
state = gr.State([]) | |
with gr.Row(): | |
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) | |
txt.submit(predict, [txt, state], [txt, state, chatbot]) | |
with gr.Row(): | |
gen = gr.Button("Submit") | |
clr = gr.Button("Clear") | |
gen.click(fn=predict, inputs=[txt, state], outputs=[txt, state, chatbot]) | |
def clear(value): | |
return [], [] | |
clr.click(clear, inputs=clr, outputs=[chatbot, state]) | |
gr_examples = gr.Examples(examples=examples, inputs=txt) | |
demo.launch() |