File size: 1,579 Bytes
8a2b97e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
import os

os.system('git clone https://huggingface.co/souljoy/chatGPT')
import requests
import json
import gradio as gr

open('chatGPT/Authorization', )
with open('chatGPT/Authorization', mode='r', encoding='utf-8') as f:
    for line in f:
        authorization = line.strip()

url = 'https://api.openai.com/v1/chat/completions'
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + authorization
}


def predict(msg, history=[]):
    messages = [{"role": "system", "content": "你是小鹏汽车的数据智能中心(DIC)的智能助手小D"}]
    for i in range(len(history) - 1, max(0, len(history) - 3)):
        messages.append({"role": "user", "content": history[i][0]})
        messages.append({"role": "assistant", "content": history[i][1]})
    messages.append({"role": "user", "content": msg})
    data = {
        "model": "gpt-3.5-turbo",
        "messages": messages
    }
    result = requests.post(url=url,
                           data=json.dumps(data),
                           headers=headers
                           )

    res = result.json()['choices'][0]['message']['content']
    history.append([msg, res])
    return history, history, res


with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    state = gr.State([])

    with gr.Row():
        txt = gr.Textbox(label='输入框', placeholder='输入内容...')
        bu = gr.Button(value='发送')
    answer_text = gr.Textbox(label='回复')

    bu.click(predict, [txt, state], [chatbot, state, answer_text])

if __name__ == "__main__":
    demo.launch()