Spaces:
Runtime error
Runtime error
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() | |