File size: 4,035 Bytes
60f870b
16314be
60f870b
 
 
16314be
 
 
60f870b
16314be
60f870b
16314be
 
60f870b
16314be
 
3241b38
 
16314be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60f870b
 
16314be
 
 
 
60f870b
 
 
16314be
 
 
 
 
 
 
 
 
60f870b
16314be
 
 
 
 
 
 
 
 
60f870b
16314be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bb340b
 
16314be
 
 
 
60f870b
93a003a
16314be
 
 
 
60f870b
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import gradio as gr
import os 
import json 
import requests

 
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
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) ]  # convert to tuples of list
              token_counter+=1
              yield chat, history, chat_counter, response  # resembles {chatbot: chat, state: history}
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],)  #openai_api_key
    inputs.submit(reset_textbox, [], [inputs])
                  
demo.queue(max_size=20, concurrency_count=20).launch(debug=True)