Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import json | |
import requests | |
API_URL = "https://api.openai.com/v1/chat/completions" | |
def predict(inputs, system_input, presence_penalty, frequency_penalty, temperature, openai_api_key, model, chat_counter, chatbot=[], history=[], state=[]): | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {openai_api_key}" | |
} | |
if not state: | |
state.append(system_input) | |
else: | |
state[0] = system_input | |
if chat_counter == 0: | |
payload = { | |
"model": model, | |
"messages": [{"role": "system", "content": f"{state[0]}"}, {"role": "user", "content": f"{inputs}"}], | |
"temperature" : 0.7, | |
"presence_penalty": 0.0, | |
"frequency_penalty": 0.0, | |
"n" : 1, | |
"stream": True, | |
} | |
if chat_counter != 0 : | |
messages=[] | |
temp1 = {} | |
temp1["role"] = "system" | |
temp1["content"] = state[0] | |
messages.append(temp1) | |
for data in chatbot: | |
temp1 = {} | |
temp1["role"] = "user" | |
temp1["content"] = data[0] | |
temp2 = {} | |
temp2["role"] = "assistant" | |
temp2["content"] = data[1] | |
messages.append(temp1) | |
messages.append(temp2) | |
temp3 = {} | |
temp3["role"] = "user" | |
temp3["content"] = inputs | |
messages.append(temp3) | |
payload = { | |
"model": "gpt-3.5-turbo", | |
"messages": messages, | |
"temperature" : temperature, | |
"presence_penalty": presence_penalty, | |
"frequency_penalty": frequency_penalty, | |
"n" : 1, | |
"stream": True, | |
} | |
chat_counter+=1 | |
history.append(inputs) | |
response = requests.post(API_URL, headers=headers, json=payload, stream=True) | |
token_counter = 0 | |
partial_words = "" | |
counter=0 | |
for chunk in response.iter_lines(): | |
if counter == 0: | |
counter+=1 | |
continue | |
if chunk.decode() : | |
chunk = chunk.decode() | |
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']: | |
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"] | |
if token_counter == 0: | |
history.append(" " + partial_words) | |
else: | |
history[-1] = partial_words | |
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] | |
token_counter+=1 | |
yield chat, history, chat_counter | |
def reset_textbox(): | |
return gr.update(value='') | |
def reset_all(chatbot=[], state=[], chat_counter=0): | |
chatbot.clear() | |
state.clear() | |
chat_counter = 0 | |
return chatbot, state, chat_counter | |
title = """<h1 align="center">🚀GPT Cooperation🚀</h1>""" | |
description = """<p align="center">欢迎来到 GPT Cooperation!通过调整不同模型参数以及不同的system,或是让两个模型互相交流,又或者用一个模型生成另一个模型需要的system,也许能得到更好的结果,试试看吧!</p>""" | |
with gr.Blocks(css = """ | |
body {background-color: #efefef; color: #333;} | |
h1 {color: #4a4a4a; font-family: 'Arial'; font-size: 24px;} | |
button {background-color: #ff6347; color: #fff; border: none; padding: 5px 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 14px; margin: 4px 2px; cursor: pointer;} | |
input {border: none; border-bottom: 1px solid #ccc; outline: none;} | |
#col_container {background-color: #fff; border-radius: 8px; padding: 20px; box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.1); width: 1000px; margin-left: auto; margin-right: auto;} | |
#chatbot, #chatbot2 {height: 520px; overflow: auto; background-color: #f5f5f5; border-radius: 8px; padding: 10px;}""") as demo: | |
gr.HTML(title) | |
gr.HTML(description) | |
with gr.Column(elem_id = "col_container"): | |
openai_api_key = gr.Textbox(type='password', label="在这里输入你的 OpenAI API 密钥 🔑") | |
with gr.Row(): | |
with gr.Column(width=9): | |
model = gr.Radio(choices=['gpt-3.5-turbo','gpt-3.5-turbo-16k', 'gpt-4'], label='模型 🤖') | |
gr.HTML('<h2 style="font-size: 18px; color: #333;">🗨️ 主聊天框 🗨️</h2>') | |
chatbot = gr.Chatbot(elem_id='chatbot') | |
inputs = gr.Textbox(placeholder= "你好呀!", label= "🙋♀️输入并按 Enter ↵") | |
state = gr.State([]) | |
b1 = gr.Button("🚀 发送", color='#5e9ca0') | |
b2 = gr.Button("🧹 清除所有聊天内容", color='#5e9ca0') | |
with gr.Accordion("参数 🛠️", open=False): | |
system_input = gr.Textbox(placeholder= "System message 📝", label= "输入系统消息") | |
presence_penalty = gr.Slider( minimum=-0, maximum=1.0, value=0.0, step=0.01, interactive=True, label="Presence Penalty 🚦",) | |
frequency_penalty = gr.Slider( minimum=-0, maximum=1.0, value=0.0, step=0.01, interactive=True, label="Frequency Penalty 📊",) | |
temperature = gr.Slider( minimum=-0, maximum=1.0, value=0.7, step=0.01, interactive=True, label="Temperature 🌡️",) | |
chat_counter = gr.Number(value=0, visible=False, precision=0) | |
inputs.submit( predict, [inputs, system_input, presence_penalty, frequency_penalty, temperature, openai_api_key, model, chat_counter, chatbot, state], [chatbot, state, chat_counter],) | |
b1.click( predict, [inputs, system_input, presence_penalty, frequency_penalty, temperature, openai_api_key, model, chat_counter, chatbot, state], [chatbot, state, chat_counter],) | |
b2.click(reset_all, [chatbot, state, chat_counter], [chatbot, state, chat_counter]) | |
inputs.submit(reset_textbox, [], [inputs]) | |
b1.click(reset_textbox, [], [inputs]) | |
with gr.Column(width=3): | |
model2 = gr.Radio(choices=['gpt-3.5-turbo','gpt-3.5-turbo-16k', 'gpt-4'], label='模型 🤖') | |
gr.HTML('<h2 style="font-size: 18px; color: #333;">🗨️ 副聊天框(可用于生成主聊天的系统消息等)🗨️</h2>') | |
chatbot2 = gr.Chatbot(elem_id='chatbot2') | |
inputs2 = gr.Textbox(placeholder= "哈喽!", label= "🙋♀️输入并按 Enter ↵") | |
state2 = gr.State([]) | |
b3 = gr.Button("🚀 发送", color='#5e9ca0') | |
b4 = gr.Button("🧹 清除所有聊天内容", color='#5e9ca0') | |
with gr.Accordion("参数 🛠️", open=False): | |
system_input2 = gr.Textbox(placeholder= "System message 📝", label= "输入系统消息") | |
presence_penalty2 = gr.Slider( minimum=-0, maximum=1.0, value=0.0, step=0.01, interactive=True, label="Presence Penalty 🚦",) | |
frequency_penalty2 = gr.Slider( minimum=-0, maximum=1.0, value=0.0, step=0.01, interactive=True, label="Frequency Penalty 📊",) | |
temperature2 = gr.Slider( minimum=-0, maximum=1.0, value=0.7, step=0.01, interactive=True, label="Temperature 🌡️",) | |
chat_counter2 = gr.Number(value=0, visible=False, precision=0) | |
inputs2.submit( predict, [inputs2, system_input2, presence_penalty2, frequency_penalty2, temperature2, openai_api_key, model2, chat_counter2, chatbot2, state2], [chatbot2, state2, chat_counter2],) | |
b3.click( predict, [inputs2, system_input2, presence_penalty2, frequency_penalty2, temperature2, openai_api_key, model2, chat_counter2, chatbot2, state2], [chatbot2, state2, chat_counter2],) | |
b4.click(reset_all, [chatbot2, state2, chat_counter2], [chatbot2, state2, chat_counter2]) | |
inputs2.submit(reset_textbox, [], [inputs2]) | |
b3.click(reset_textbox, [], [inputs2]) | |
demo.queue().launch(debug=True) |