ChatPDF / app.py
souljoy's picture
Update app.py
0fcf950
raw
history blame
1.6 kB
import os
os.system('git clone https://huggingface.co/souljoy/chatGPT')
import requests
import json
import gradio as gr
# 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 ' + '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'
}
def predict(msg, history=[]):
messages = []
for i in range(len(history) - 1, max(0, len(history) - 3), -1):
messages.append({"role": "user", "content": history[i][0]})
messages.append({"role": "assistant", "content": history[i][1]})
messages.append({"role": "user", "content": msg})
data = {
"model": "text-davinci-003",
"messages": messages
}
result = requests.post(url=url,
data=json.dumps(data),
headers=headers
)
res = result.json()['choices'][0]['message']['content']
print(res)
history.append([msg, res])
return history, history, res
with gr.Blocks() as demo:
state = gr.State([])
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot()
txt = gr.Textbox(label='输入框', placeholder='输入消息...')
bu = gr.Button(value='发送消息')
with gr.Column():
answer_text = gr.Textbox(label='回复')
bu.click(predict, [txt, state], [chatbot, state, answer_text])
if __name__ == "__main__":
demo.launch()