import os import gradio as gr import clueai import torch from transformers import T5Tokenizer, T5ForConditionalGeneration tokenizer = T5Tokenizer.from_pretrained("huolongguo10/HR_Chat") model = T5ForConditionalGeneration.from_pretrained("huolongguo10/HR_Chat") # 使用 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) base_info = "用户:你是谁?\n小元:我是huolongguo10助手智能助手。\n" def preprocess(text): text = f"{base_info}{text}" text = text.replace("\n", "\\n").replace("\t", "\\t") return text def postprocess(text): return text.replace("\\n", "\n").replace("\\t", "\t").replace('%20',' ')#.replace(" ", " ") generate_config = {'do_sample': True, 'top_p': 0.9, 'top_k': 50, 'temperature': 0.9, 'num_beams': 1, 'max_length': 1024, 'min_length': 3, 'no_repeat_ngram_size': 5, 'length_penalty': 0.6, 'return_dict_in_generate': True, 'output_scores': True} def answer(text, sample=True, top_p=0.9, temperature=0.9): '''sample:是否抽样。生成任务,可以设置为True; top_p:0-1之间,生成的内容越多样''' text = preprocess(text) encoding = tokenizer(text=[text], truncation=True, padding=True, max_length=1024, return_tensors="pt").to(device) if not sample: out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=1024, num_beams=1, length_penalty=0.6) else: out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=1024, do_sample=True, top_p=top_p, temperature=temperature, no_repeat_ngram_size=12) #out=model.generate(**encoding, **generate_config) out_text = tokenizer.batch_decode(out["sequences"], skip_special_tokens=True) return postprocess(out_text[0]) def clear_session(): return '', None def chatyuan_bot(input, history): history = history or [] if len(history) > 5: history = history[-5:] context = "\n".join([f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history]) #print(context) input_text = context + "\n用户:" + input + "\n小元:" input_text = input_text.strip() output_text = answer(input_text) print("open_model".center(20, "=")) print(f"{input_text}\n{output_text}") #print("="*20) history.append((input, output_text)) #print(history) return history, history def chatyuan_bot_regenerate(input, history): history = history or [] if history: input=history[-1][0] history=history[:-1] if len(history) > 1: history = history[-1:] context = "\n".join([f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history]) #print(context) input_text = context + "\n用户:" + input + "\n小元:" input_text = input_text.strip() output_text = answer(input_text) print("open_model".center(20, "=")) print(f"{input_text}\n{output_text}") history.append((input, output_text)) #print(history) return history, history block = gr.Blocks() with block as demo: gr.Markdown("""

huolongguo10助手

""") chatbot = gr.Chatbot(label='ChatYuan') message = gr.Textbox() state = gr.State() message.submit(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state]) with gr.Row(): clear_history = gr.Button("👋 清除历史对话 | Clear History") clear = gr.Button('🧹 清除发送框 | Clear Input') send = gr.Button("🚀 发送 | Send") regenerate = gr.Button("🚀 重新生成本次结果 | regenerate") regenerate.click(chatyuan_bot_regenerate, inputs=[message, state], outputs=[chatbot, state]) send.click(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state]) clear.click(lambda: None, None, message, queue=False) clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False) def ChatYuan(api_key, text_prompt): cl = clueai.Client(api_key, check_api_key=True) # generate a prediction for a prompt # 需要返回得分的话,指定return_likelihoods="GENERATION" prediction = cl.generate(model_name='ChatYuan-large', prompt=text_prompt) # print the predicted text #print('prediction: {}'.format(prediction.generations[0].text)) response = prediction.generations[0].text if response == '': response = "很抱歉,我无法回答这个问题" return response def chatyuan_bot_api(api_key, input, history): history = history or [] if len(history) > 5: history = history[-5:] context = "\n".join([f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history]) #print(context) input_text = context + "\n用户:" + input + "\n小元:" input_text = input_text.strip() output_text = ChatYuan(api_key, input_text) print("api".center(20, "=")) print(f"api_key:{api_key}\n{input_text}\n{output_text}") #print("="*20) history.append((input, output_text)) #print(history) return history, history block = gr.Blocks() with block as demo_1: gr.Markdown(""" """) api_key = gr.inputs.Textbox(label="请输入你的api-key(必填)", default="", type='password') chatbot = gr.Chatbot(label='ChatYuan') message = gr.Textbox() state = gr.State() message.submit(chatyuan_bot_api, inputs=[api_key,message, state], outputs=[chatbot, state]) with gr.Row(): clear_history = gr.Button("👋 清除历史对话 | Clear Context") clear = gr.Button('🧹 清除发送框 | Clear Input') send = gr.Button("🚀 发送 | Send") send.click(chatyuan_bot_api, inputs=[api_key,message, state], outputs=[chatbot, state],api_name='send') clear.click(lambda: None, None, message, queue=False) clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False) block = gr.Blocks() with block as introduction: gr.Markdown("""啥也没有 """) gui = gr.TabbedInterface(interface_list=[demo], tab_names=["开源模型"]) gui.launch(quiet=True,show_api=True, share = False)