Spaces:
Paused
Paused
Add app.py and requirements.py
Browse files- app.py +79 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
openai.api_key = os.getenv('API_KEY')
|
| 6 |
+
|
| 7 |
+
def ask(question, history):
|
| 8 |
+
history = history + [question]
|
| 9 |
+
try:
|
| 10 |
+
response = openai.ChatCompletion.create(
|
| 11 |
+
model="gpt-3.5-turbo",
|
| 12 |
+
messages=[
|
| 13 |
+
{"role":"user" if i%2==0 else "assistant", "content":content}
|
| 14 |
+
for i,content in enumerate(history)
|
| 15 |
+
]
|
| 16 |
+
)["choices"][0]["message"]["content"]
|
| 17 |
+
while response.startswith("\n"):
|
| 18 |
+
response = response[1:]
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(e)
|
| 21 |
+
response = 'Timeout! Please wait a few minutes and retry'
|
| 22 |
+
history = history + [response]
|
| 23 |
+
with open("dialogue.txt", "a", encoding='utf-8') as f:
|
| 24 |
+
f.write(json.dumps(history, ensure_ascii=False)+"\n")
|
| 25 |
+
return history
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
import gradio as gr
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def predict(question, history=[]):
|
| 33 |
+
history = ask(question, history)
|
| 34 |
+
response = [(history[i].replace("\n","<br>"),history[i+1].replace("\n","<br>")) for i in range(0,len(history)-1,2)]
|
| 35 |
+
return "", history, response
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
|
| 40 |
+
examples = [
|
| 41 |
+
['200字介绍一下凯旋门:'],
|
| 42 |
+
['网上购物有什么小窍门?'],
|
| 43 |
+
['补全下述对三亚的介绍:\n三亚位于海南岛的最南端,是'],
|
| 44 |
+
['将这句文言文翻译成英语:"逝者如斯夫,不舍昼夜。"'],
|
| 45 |
+
['Question: What\'s the best winter resort city? User: A 10-year professional traveler. Answer: '],
|
| 46 |
+
['How to help my child to make friends with his classmates? answer this question step by step:'],
|
| 47 |
+
['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.'],
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
gr.Markdown(
|
| 51 |
+
"""
|
| 52 |
+
朋友你好,
|
| 53 |
+
|
| 54 |
+
这是我利用[gradio](https://gradio.app/creating-a-chatbot/)编写的一个小网页,用于以网页的形式给大家分享ChatGPT请求服务,希望你玩的开心
|
| 55 |
+
|
| 56 |
+
p.s. 响应时间和问题复杂程度相关,<del>一般能在10~20秒内出结果</del>用了新的api已经提速到大约5秒内了
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
chatbot = gr.Chatbot()
|
| 60 |
+
state = gr.State([])
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
| 64 |
+
|
| 65 |
+
txt.submit(predict, [txt, state], [txt, state, chatbot])
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
gen = gr.Button("Submit")
|
| 69 |
+
clr = gr.Button("Clear")
|
| 70 |
+
|
| 71 |
+
gen.click(fn=predict, inputs=[txt, state], outputs=[txt, state, chatbot])
|
| 72 |
+
|
| 73 |
+
def clear(value):
|
| 74 |
+
return [], []
|
| 75 |
+
clr.click(clear, inputs=clr, outputs=[chatbot, state])
|
| 76 |
+
|
| 77 |
+
gr_examples = gr.Examples(examples=examples, inputs=txt)
|
| 78 |
+
|
| 79 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openai==0.27.0
|