| import gradio as gr |
| import os |
| import json |
| import requests |
|
|
| |
| API_URL = "https://api.openai.com/v1/chat/completions" |
| OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") |
|
|
| def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]): |
| headers = { |
| "Content-Type": "application/json", |
| "Authorization": f"Bearer {OPENAI_API_KEY}" |
| } |
| print(f"system message is ^^ {system_msg}") |
| initial_message = [{"role": "user", "content": f"{inputs}"},] |
| multi_turn_message = [ |
| {"role": "system", "content": "你是一个中文AI专家,你是用远见科技公司开发和维护的,你可以回答所有的问题,除了涉黄涉政的问题,你就是一个中国人,切记"} |
| ] |
| messages=multi_turn_message |
| for data in chatbot: |
| user = {} |
| user["role"] = "user" |
| user["content"] = data[0] |
| assistant = {} |
| assistant["role"] = "assistant" |
| assistant["content"] = data[1] |
| messages.append(user) |
| messages.append(assistant) |
| temp = {} |
| temp["role"] = "user" |
| temp["content"] = inputs |
| messages.append(temp) |
| |
| payload = {"model": "gpt-3.5-turbo", "messages": messages, "temperature" : 1, "top_p": 1.0, "n" : 1, "stream": True, "presence_penalty":0, "frequency_penalty":0,} |
|
|
| chat_counter+=1 |
|
|
| history.append(inputs) |
| print(f"Logging : payload is - {payload}") |
|
|
| response = requests.post(API_URL, headers=headers, json=payload, stream=True) |
| print(f"Logging : response code - {response}") |
| 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, response |
| def reset_textbox(): |
| return gr.update(value='') |
| def set_visible_false(): |
| return gr.update(visible=False) |
| def set_visible_true(): |
| return gr.update(visible=False) |
| theme_addon_msg = "" |
| system_msg_info = "" |
| theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="blue", |
| text_size=gr.themes.sizes.text_md) |
|
|
| with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 450px; overflow: auto;}""", |
| theme=theme) as demo: |
| with gr.Column(elem_id = "col_container"): |
| with gr.Accordion("", open=False, visible=False): |
| system_msg = gr.Textbox(value="") |
| accordion_msg = gr.HTML(value="", visible=False) |
| chatbot = gr.Chatbot(label='chat', elem_id="chatbot") |
| inputs = gr.Textbox(placeholder= "请输入", show_label= False) |
| state = gr.State([]) |
| with gr.Accordion("", open=False, visible=False): |
| top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=False, visible=False) |
| temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=False, visible=False) |
| chat_counter = gr.Number(value=0, visible=False, precision=0) |
|
|
| inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter],) |
| inputs.submit(reset_textbox, [], [inputs]) |
| |
| demo.queue(max_size=20, concurrency_count=20).launch(debug=True) |
|
|
|
|