|
from fix_int8 import fix_pytorch_int8 |
|
fix_pytorch_int8() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import torch |
|
import logging |
|
import gradio as gr |
|
from transformers import AutoTokenizer, GenerationConfig, AutoModel |
|
|
|
|
|
chatglm = 'THUDM/chatglm-6b' |
|
chatglm_rev = '4de8efe' |
|
int8_model = 'KumaTea/twitter-int8' |
|
int8_model_rev = '1136001' |
|
|
|
max_length = 224 |
|
default_start = ["你是Kuma,请和我聊天,每句话以两个竖杠分隔。", "好的,你想聊什么?"] |
|
|
|
gr_title = """<h1 align="center">KumaGLM</h1> |
|
<h3 align='center'>这是一个 AI Kuma,你可以与他聊天,或者直接在文本框按下Enter</h3> |
|
<p align='center'>采样范围 2020/06/13 - 2023/04/15</p> |
|
<p align='center'>GitHub Repo: <a class="github-button" href="https://github.com/KumaTea/ChatGLM" aria-label="Star KumaTea/ChatGLM on GitHub">KumaTea/ChatGLM</a></p> |
|
<script async defer src="https://buttons.github.io/buttons.js"></script> |
|
""" |
|
gr_footer = """<p align='center'> |
|
本项目基于 |
|
<a href='https://github.com/ljsabc/Fujisaki' target='_blank'>ljsabc/Fujisaki</a> |
|
,模型采用 |
|
<a href='https://huggingface.co/THUDM/chatglm-6b' target='_blank'>THUDM/chatglm-6b</a> |
|
。 |
|
</p> |
|
<p align='center'> |
|
<em>每天起床第一句!</em> |
|
</p>""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig( |
|
format='%(asctime)s %(levelname)-8s %(message)s', |
|
level=logging.INFO, |
|
datefmt='%m/%d %H:%M:%S') |
|
|
|
model = AutoModel.from_pretrained( |
|
int8_model, |
|
trust_remote_code=True, |
|
revision=int8_model_rev |
|
).float() |
|
tokenizer = AutoTokenizer.from_pretrained(chatglm, trust_remote_code=True, revision=chatglm_rev) |
|
|
|
|
|
|
|
|
|
model.eval() |
|
|
|
|
|
torch.set_default_tensor_type(torch.FloatTensor) |
|
|
|
|
|
def evaluate(context, temperature, top_p): |
|
generation_config = GenerationConfig( |
|
temperature=temperature, |
|
top_p=top_p, |
|
|
|
|
|
num_beams=1, |
|
do_sample=True, |
|
) |
|
with torch.no_grad(): |
|
|
|
|
|
|
|
if not context.endswith('||'): |
|
context += '||' |
|
logging.info('[API] Request: ' + context) |
|
ids = tokenizer([context], return_tensors="pt") |
|
inputs = ids.to("cpu") |
|
out = model.generate( |
|
**inputs, |
|
max_length=max_length, |
|
generation_config=generation_config |
|
) |
|
out = out.tolist()[0] |
|
decoder_output = tokenizer.decode(out) |
|
|
|
out_text = decoder_output |
|
logging.info('[API] Results: ' + out_text) |
|
return out_text |
|
|
|
|
|
def evaluate_stream(msg, history, temperature, top_p): |
|
generation_config = GenerationConfig( |
|
temperature=temperature, |
|
top_p=top_p, |
|
|
|
num_beams=1, |
|
do_sample=True, |
|
) |
|
if not msg: |
|
msg = '……' |
|
|
|
history.append([msg, ""]) |
|
|
|
context = '||'.join(default_start) + '||' |
|
if len(history) > 4: |
|
history.pop(0) |
|
|
|
for j in range(len(history)): |
|
history[j][0] = history[j][0].replace("<br>", "") |
|
|
|
|
|
for h in history[:-1]: |
|
context += h[0] + "||" + h[1] + "||" |
|
|
|
context += history[-1][0] + "||" |
|
context = context.replace(r'<br>', '') |
|
|
|
|
|
|
|
while len(tokenizer.encode(context)) > max_length: |
|
|
|
context = context[15:] |
|
|
|
h = [] |
|
logging.info('[UI] Request: ' + context) |
|
for response, h in model.stream_chat(tokenizer, context, h, max_length=max_length, top_p=top_p, temperature=temperature): |
|
history[-1][1] = response |
|
yield history, "" |
|
logging.info('[UI] Results: ' + response) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.HTML(gr_title) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=2): |
|
temp = gr.components.Slider(minimum=0, maximum=1.1, value=0.5, label="Temperature", |
|
info="温度参数,越高的温度生成的内容越丰富,但是有可能出现语法问题。小的温度也能帮助生成更相关的回答。") |
|
top_p = gr.components.Slider(minimum=0.5, maximum=1.0, value=0.8, label="Top-p", |
|
info="top-p参数,只输出前p>top-p的文字,越大生成的内容越丰富,但也可能出现语法问题。数字越小似乎上下文的衔接性越好。") |
|
|
|
|
|
|
|
|
|
with gr.Column(scale=3): |
|
chatbot = gr.Chatbot(label="聊天框", info="") |
|
msg = gr.Textbox(label="输入框", placeholder="最近过得怎么样?", |
|
info="输入你的内容,按 [Enter] 发送。什么都不填经常会出错。") |
|
clear = gr.Button("清除聊天") |
|
api_handler = gr.Button("API", visible=False) |
|
textbox_for_api = gr.Textbox(visible=False) |
|
|
|
msg.submit(evaluate_stream, [msg, chatbot, temp, top_p], [chatbot, msg]) |
|
clear.click(lambda: None, None, chatbot, queue=False) |
|
api_handler.click(evaluate, [textbox_for_api, temp, top_p], [textbox_for_api], api_name='chat') |
|
gr.HTML(gr_footer) |
|
|
|
demo.queue() |
|
demo.launch(debug=False) |
|
|