Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
4 |
+
tokenizer = T5Tokenizer.from_pretrained("ClueAI/ChatYuan-large-v2")
|
5 |
+
model = T5ForConditionalGeneration.from_pretrained("ClueAI/ChatYuan-large-v2")
|
6 |
+
# 使用
|
7 |
+
device='cpu'
|
8 |
+
|
9 |
+
def preprocess(text):
|
10 |
+
text = text.replace("\n", "\\n").replace("\t", "\\t")
|
11 |
+
return text
|
12 |
+
|
13 |
+
def postprocess(text):
|
14 |
+
return text.replace("\\n", "\n").replace("\\t", "\t").replace('%20',' ')
|
15 |
+
|
16 |
+
def answer(text, sample=True, top_p=1, temperature=0.7):
|
17 |
+
'''sample:是否抽样。生成任务,可以设置为True;
|
18 |
+
top_p:0-1之间,生成的内容越多样'''
|
19 |
+
text = preprocess(text)
|
20 |
+
encoding = tokenizer(text=[text], truncation=True, padding=True, max_length=768, return_tensors="pt").to(device)
|
21 |
+
if not sample:
|
22 |
+
out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=512, num_beams=1, length_penalty=0.6)
|
23 |
+
else:
|
24 |
+
out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=512, do_sample=True, top_p=top_p, temperature=temperature, no_repeat_ngram_size=3)
|
25 |
+
out_text = tokenizer.batch_decode(out["sequences"], skip_special_tokens=True)
|
26 |
+
return postprocess(out_text[0])
|
27 |
+
|
28 |
+
def clear_session():
|
29 |
+
return '', None
|
30 |
+
|
31 |
+
def chatyuan_bot(input, history):
|
32 |
+
history = history or []
|
33 |
+
if len(history) > 5:
|
34 |
+
history = history[-5:]
|
35 |
+
|
36 |
+
context = "\n".join([f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history])
|
37 |
+
print(context)
|
38 |
+
|
39 |
+
input_text = context + "\n用户:" + input + "\n小元:"
|
40 |
+
output_text = answer(input_text)
|
41 |
+
history.append((input, output_text))
|
42 |
+
print(history)
|
43 |
+
return history, history
|
44 |
+
|
45 |
+
block = gr.Blocks()
|
46 |
+
|
47 |
+
with block as demo:
|
48 |
+
gr.Markdown("""<h1><center>元语智能——ChatYuan</center></h1>
|
49 |
+
""")
|
50 |
+
chatbot = gr.Chatbot(label='ChatYuan')
|
51 |
+
message = gr.Textbox()
|
52 |
+
state = gr.State()
|
53 |
+
message.submit(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state])
|
54 |
+
with gr.Row():
|
55 |
+
clear_history = gr.Button("👋 清除历史对话")
|
56 |
+
clear = gr.Button('🧹 清除发送框')
|
57 |
+
send = gr.Button("🚀 发送")
|
58 |
+
|
59 |
+
send.click(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state])
|
60 |
+
clear.click(lambda: None, None, message, queue=False)
|
61 |
+
clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False)
|
62 |
+
|
63 |
+
|
64 |
+
# def ChatYuan(api_key, text_prompt):
|
65 |
+
|
66 |
+
# cl = clueai.Client(api_key,
|
67 |
+
# check_api_key=True)
|
68 |
+
# # generate a prediction for a prompt
|
69 |
+
# # 需要返回得分的话,指定return_likelihoods="GENERATION"
|
70 |
+
# prediction = cl.generate(model_name='ChatYuan-large', prompt=text_prompt)
|
71 |
+
# # print the predicted text
|
72 |
+
# print('prediction: {}'.format(prediction.generations[0].text))
|
73 |
+
# response = prediction.generations[0].text
|
74 |
+
# if response == '':
|
75 |
+
# response = "很抱歉,我无法回答这个问题"
|
76 |
+
|
77 |
+
# return response
|
78 |
+
|
79 |
+
# def chatyuan_bot_api(api_key, input, history):
|
80 |
+
# history = history or []
|
81 |
+
|
82 |
+
# if len(history) > 5:
|
83 |
+
# history = history[-5:]
|
84 |
+
|
85 |
+
# context = "\n".join([f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history])
|
86 |
+
# print(context)
|
87 |
+
|
88 |
+
# input_text = context + "\n用户:" + input + "\n小元:"
|
89 |
+
# output_text = ChatYuan(api_key, input_text)
|
90 |
+
# history.append((input, output_text))
|
91 |
+
# print(history)
|
92 |
+
# return history, history
|
93 |
+
|
94 |
+
block = gr.Blocks()
|
95 |
+
|
96 |
+
with block as demo_1:
|
97 |
+
gr.Markdown("""<h1><center>元语智能——ChatYuan</center></h1>
|
98 |
+
<font size=4>在使用此功能前,你需要有个API key. API key 可以通过这个<a href='https://www.clueai.cn/' target="_blank">平台</a>获取</font>
|
99 |
+
""")
|
100 |
+
api_key = gr.inputs.Textbox(label="请输入你的api-key(必填)", default="", type='password')
|
101 |
+
chatbot = gr.Chatbot(label='ChatYuan')
|
102 |
+
message = gr.Textbox()
|
103 |
+
state = gr.State()
|
104 |
+
message.submit(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state])
|
105 |
+
with gr.Row():
|
106 |
+
clear_history = gr.Button("👋 清除历史对话")
|
107 |
+
clear = gr.Button('🧹 清除发送框')
|
108 |
+
send = gr.Button("🚀 发送")
|
109 |
+
|
110 |
+
send.click(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state])
|
111 |
+
clear.click(lambda: None, None, message, queue=False)
|
112 |
+
clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False)
|
113 |
+
|
114 |
+
block = gr.Blocks()
|
115 |
+
with block as introduction:
|
116 |
+
gr.Markdown("""<h1><center>元语智能——ChatYuan</center></h1>
|
117 |
+
|
118 |
+
<font size=4>😉ChatYuan: 元语功能型对话大模型
|
119 |
+
<br>
|
120 |
+
<br>
|
121 |
+
👏这个模型可以用于问答、结合上下文做对话、做各种生成任务, 包括创意性写作, 也能回答一些像法律、新冠等领域问题. 它基于PromptCLUE-large结合数亿条功能对话多轮对话数据进一步训练得到.<br>
|
122 |
+
<br>
|
123 |
+
👀<a href='https://www.cluebenchmarks.com/clueai.html'>PromptCLUE-large</a>在1000亿token中文语料上预训练, 累计学习1.5万亿中文token, 并且在数百种任务上进行Prompt任务式训练. 针对理解类任务, 如分类、情感分析、抽取等, 可以自定义标签体系; 针对多种生成任务, 可以进行采样自由生成. <br>
|
124 |
+
<br>
|
125 |
+
🚀<a href='https://www.clueai.cn/chat' target="_blank">在线Demo</a> | <a href='https://modelscope.cn/models/ClueAI/ChatYuan-large/summary' target="_blank">ModelScope</a> | <a href='https://huggingface.co/ClueAI/ChatYuan-large-v1' target="_blank">Huggingface</a> | <a href='https://www.clueai.cn' target="_blank">官网体验场</a> | <a href='https://github.com/clue-ai/clueai-python#ChatYuan%E5%8A%9F%E8%83%BD%E5%AF%B9%E8%AF%9D' target="_blank">ChatYuan-API</a> | <a href='https://github.com/clue-ai/ChatYuan' target="_blank">Github项目地址</a> | <a href='https://openi.pcl.ac.cn/ChatYuan/ChatYuan/src/branch/main/Fine_tuning_ChatYuan_large_with_pCLUE.ipynb' target="_blank">OpenI免费试用</a>
|
126 |
+
</font>
|
127 |
+
""")
|
128 |
+
|
129 |
+
|
130 |
+
gui = gr.TabbedInterface(interface_list=[introduction,demo, demo_1], tab_names=["相关介绍","开源模型", "API调用"])
|
131 |
+
gui.launch(quiet=True,show_api=False, share = True)
|