Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
os.system('git clone https://huggingface.co/souljoy/chatGPT')
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
open('chatGPT/Authorization', )
|
9 |
+
with open('chatGPT/Authorization', mode='r', encoding='utf-8') as f:
|
10 |
+
for line in f:
|
11 |
+
authorization = line.strip()
|
12 |
+
|
13 |
+
url = 'https://api.openai.com/v1/chat/completions'
|
14 |
+
headers = {
|
15 |
+
'Content-Type': 'application/json',
|
16 |
+
'Authorization': 'Bearer ' + authorization
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
def predict(msg, history=[]):
|
21 |
+
messages = [{"role": "system", "content": "你是小鹏汽车的数据智能中心(DIC)的智能助手小D"}]
|
22 |
+
for i in range(len(history) - 1, max(0, len(history) - 3)):
|
23 |
+
messages.append({"role": "user", "content": history[i][0]})
|
24 |
+
messages.append({"role": "assistant", "content": history[i][1]})
|
25 |
+
messages.append({"role": "user", "content": msg})
|
26 |
+
data = {
|
27 |
+
"model": "gpt-3.5-turbo",
|
28 |
+
"messages": messages
|
29 |
+
}
|
30 |
+
result = requests.post(url=url,
|
31 |
+
data=json.dumps(data),
|
32 |
+
headers=headers
|
33 |
+
)
|
34 |
+
|
35 |
+
res = result.json()['choices'][0]['message']['content']
|
36 |
+
history.append([msg, res])
|
37 |
+
return history, history, res
|
38 |
+
|
39 |
+
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
chatbot = gr.Chatbot()
|
42 |
+
state = gr.State([])
|
43 |
+
|
44 |
+
with gr.Row():
|
45 |
+
txt = gr.Textbox(label='输入框', placeholder='输入内容...')
|
46 |
+
bu = gr.Button(value='发送')
|
47 |
+
answer_text = gr.Textbox(label='回复')
|
48 |
+
|
49 |
+
bu.click(predict, [txt, state], [chatbot, state, answer_text])
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
demo.launch()
|