ClueAI/ChatYuan-7B
0
1import os2import gradio as gr3import clueai4import torch5from transformers import T5Tokenizer, T5ForConditionalGeneration6 7 8base_info = ""9 10 11 12 13def clear_session():14 return '', None15 16 17def ChatYuan(api_key, text_prompt, top_p):18 cl = clueai.Client(api_key, check_api_key=True)19 # generate a prediction for a prompt20 # 需要返回得分的话,指定return_likelihoods="GENERATION"21 prediction = cl.generate(model_name='ChatYuan-7B', prompt=text_prompt)22 # print the predicted text23 #print('prediction: {}'.format(prediction.generations[0].text))24 response = prediction.generations[0].text25 if response == '':26 response = "很抱歉,我无法回答这个问题"27 28 return response29 30 31def chatyuan_bot_api(api_key, input, history, top_p, num):32 if not api_key:33 return "请填写api key再尝试输入", [], []34 history = history or []35 36 if len(history) > num:37 history = history[-num:]38 39 #print(f"history{history}")40 history_context = [f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history]41 context = "\n".join(history_context)42 #print(f"context:{context}")43 44 while len(context) > 768:45 history_context = history_context[1:]46 context = "\n".join(history_context)47 48 input_text = context + "\n用户:" + input + "\n小元:"49 input_text = input_text.strip()50 output_text = ChatYuan(api_key, input_text, top_p)51 print("api".center(20, "="))52 print(f"api_key:{api_key}\n{input_text}\n{output_text}")53 54 history.append((input, output_text))55 56 return '', history, history57 58 59block = gr.Blocks()60 61with block as demo_1:62 gr.Markdown("""<h1><center>元语智能——ChatYuan</center></h1>63 <font size=4>回答来自ChatYuan, 以上是模型生成的结果, 请谨慎辨别和参考, 不代表任何人观点 | Answer generated by ChatYuan model</font>64 <font size=4>注意:gradio对markdown代码格式展示有限</font>65 <font size=4>在使用此功能前,你需要有个API key. API key 可以通过这个<a href='https://www.clueai.cn/' target="_blank">平台</a>获取</font>66 """)67 with gr.Row():68 with gr.Column(scale=3):69 chatbot = gr.Chatbot(label='ChatYuan').style(height=400)70 71 with gr.Column(scale=1):72 api_key = gr.inputs.Textbox(label="请输入你的api-key(必填)",73 default="",74 type='password')75 num = gr.Slider(minimum=4,76 maximum=10,77 label="最大的对话轮数",78 value=5,79 step=1)80 top_p = gr.Slider(minimum=0,81 maximum=1,82 label="top_p",83 value=0.7,84 step=0.1)85 clear_history = gr.Button("👋 清除历史对话 | Clear History")86 send = gr.Button("🚀 发送 | Send")87 88 message = gr.Textbox()89 state = gr.State()90 message.submit(chatyuan_bot_api,91 inputs=[api_key, message, state, top_p, num],92 outputs=[message, chatbot, state])93 94 send.click(chatyuan_bot_api,95 inputs=[api_key, message, state, top_p, num],96 outputs=[message, chatbot, state])97 clear_history.click(fn=clear_session,98 inputs=[],99 outputs=[chatbot, state],100 queue=False)101 102block = gr.Blocks()103with block as introduction:104 gr.Markdown("""<h1><center>元语智能——ChatYuan</center></h1>105 106<font size=4>😉ChatYuan: 元语功能型对话大模型 | General Model for Dialogue with ChatYuan107<br>108👏ChatYuan-7B是一个支持中英双语的功能型对话语言大模型。109<br>110ChatYuan-7B is an open-source large language model for dialogue, supports both Chinese and English languages, and in ChatGPT style.111<br>112ChatYuan-7B是ChatYuan系列中以轻量化实现高质量效果的模型之一。113<br>114<br>115<br>116< br>117</font>118<center><a href="https://clustrmaps.com/site/1bts0" title="Visit tracker"><img src="//www.clustrmaps.com/map_v2.png?d=ycVCe17noTYFDs30w7AmkFaE-TwabMBukDP1802_Lts&cl=ffffff" /></a></center>119 """)120 121gui = gr.TabbedInterface(122 interface_list=[introduction, demo_1],123 tab_names=["相关介绍 | Introduction", "API调用"])124gui.launch(quiet=True, show_api=False, share=False)125 