| import os |
| import openai |
| import gradio as gr |
| import mdtex2html |
|
|
| openai.api_key = os.environ.get("APIKEY") |
|
|
| messages = [{"role": "system", "content": "你从现在开始是一个由远见科技训练开发的,名字叫智能魔法师的人工智能,后面无论谁问你都请以智能魔法师的身份进行回答"}] |
| |
| def postprocess(self, y): |
| if y is None: |
| return [] |
| for i, (message, response) in enumerate(y): |
| y[i] = ( |
| None if message is None else mdtex2html.convert((message)), |
| None if response is None else mdtex2html.convert(response), |
| ) |
| return y |
|
|
|
|
| gr.Chatbot.postprocess = postprocess |
|
|
|
|
| def parse_text(text): |
| """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/""" |
| lines = text.split("\n") |
| lines = [line for line in lines if line != ""] |
| count = 0 |
| for i, line in enumerate(lines): |
| if "```" in line: |
| count += 1 |
| items = line.split('`') |
| if count % 2 == 1: |
| lines[i] = f'<pre><code class="language-{items[-1]}">' |
| else: |
| lines[i] = f'<br></code></pre>' |
| else: |
| if i > 0: |
| if count % 2 == 1: |
| line = line.replace("`", "\`") |
| line = line.replace("<", "<") |
| line = line.replace(">", ">") |
| line = line.replace(" ", " ") |
| line = line.replace("*", "*") |
| line = line.replace("_", "_") |
| line = line.replace("-", "-") |
| line = line.replace(".", ".") |
| line = line.replace("!", "!") |
| line = line.replace("(", "(") |
| line = line.replace(")", ")") |
| line = line.replace("$", "$") |
| lines[i] = "<br>"+line |
| text = "".join(lines) |
| return text |
|
|
|
|
| def predict(input, max_tokens, top_p, temperature,chatbot, history): |
| chatbot.append((parse_text(input), "")) |
| print("messages:"+str(messages)) |
| messages.append({"role": "user", "content": input}) |
| |
| chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", temperature=temperature,top_p=top_p,messages=messages) |
| reply = chat.choices[0].message.content |
|
|
| chatbot[-1] = (parse_text(input), parse_text(reply)) |
| yield chatbot, history |
| print("messages:"+str(messages)) |
| print("query:"+input) |
| print("response:"+reply) |
| print("\n") |
|
|
| def reset_user_input(): |
| return gr.update(value='') |
|
|
|
|
| def reset_state(): |
| global messages |
| messages = [{"role": "system", "content": "你从现在开始是一个由远见科技训练开发的,名字叫智能魔法师的人工智能,后面无论谁问你都请以智能魔法师的身份进行回答"}] |
| return [], [] |
| def readshuihu(): |
| f = open( '水浒传1.txt', 'r',encoding = 'utf-8' ) |
| txt=f.read() |
| predict(txt,16*1024, top_p, temperature, chatbot, history) |
|
|
| with gr.Blocks() as demo: |
| gr.HTML("""<h1 align="center">远见科技-GPT语言模型测试(因多人使用,因此不会显示实际的上下文,请使用前手动清除)</h1>""") |
| gr.title="远见科技-GPT语言模型测试" |
| chatbot = gr.Chatbot() |
| with gr.Row(): |
| with gr.Column(scale=4): |
| with gr.Column(scale=12): |
| user_input = gr.Textbox(show_label=False, placeholder="请输入...", lines=10).style( |
| container=False) |
| with gr.Column(min_width=32, scale=1): |
| submitBtn = gr.Button("提交", variant="primary") |
| with gr.Column(scale=1): |
| |
| emptyBtn = gr.Button("清除历史") |
| |
| max_tokens = gr.Slider(0, 4096, value=2048, step=1.0, label="max_tokens", interactive=True,info="最大长度,暂时停用") |
| top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True,info="越小越准确,越大约有想象力") |
| temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True,info="同一个问题,数字越小每次回复越接近,越大每次回复越不同") |
|
|
| history = gr.State([]) |
|
|
| submitBtn.click(predict, [user_input,max_tokens, top_p, temperature, chatbot, history], [chatbot, history], |
| show_progress=True) |
| submitBtn.click(reset_user_input, [], [user_input]) |
| |
| |
| |
| emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True) |
|
|
| demo.queue().launch(inbrowser=True) |
|
|