# https://github.com/THUDM/ChatGLM2-6B from transformers import AutoTokenizer, AutoModel import gradio as gr tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True) #model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True, device='cuda') #model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).float() model = AutoModel.from_pretrained("THUDM/chatglm2-6b-int4",trust_remote_code=True).float() model = model.eval() def generate_prompt(prompt: str, style: str) -> str: if len(prompt) == 0: return "请输入您的提示语" generated_prompt = '以“' + prompt + '”为主题,撰写一段' + style + ',字数在100字左右' print('功能:' + style + ',generated_prompt:' + generated_prompt) return generated_prompt def text_generation(original_prompt: str, generated_prompt: str, style: str) -> str: if not len(generated_prompt) == 0: prompt = generated_prompt elif not len(original_prompt) == 0: prompt = original_prompt else: return "请输入您的提示语" print('功能:' + style + ',提示语:' + prompt) response, history = model.chat(tokenizer, prompt, history=[]) result = '按照您的提示语:' + prompt + ',生成的文案如下:\n\n' + response print(result) return result css_style = "#fixed_size_img {height: 240px;} " title = "文案创作 by宁侠" description = ''' 本服务的主要应用场景涵盖多种文案输入生成和续写,例如用户可以自行输入各种内容,之后服务将会对其进行回答、续写或者按照指令进行回复。 ''' with gr.Blocks(title=title, css=css_style) as demo: gr.HTML('''