mcgame commited on
Commit
47735dd
1 Parent(s): 9ef0275

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -42
app.py CHANGED
@@ -1,42 +1,29 @@
1
- import clueai
2
-
3
- # initialize the Clueai Client with an API Key
4
- cl = cl = clueai.Client('ffILfMwQydmK1YXyFplYc100010000010')
5
-
6
- # generate a prediction for a prompt
7
-
8
- generate_config = {
9
- "do_sample": True,
10
- "top_p": 0.8,
11
- "max_length": 128,
12
- "min_length": 10,
13
- "length_penalty": 1.0,
14
- "num_beams": 1
15
- }
16
- # 如果需要自由调整参数自由采样生成,添加额外参数信息设置方式:generate_config=generate_config
17
-
18
- # 需要返回得分的话,指定return_likelihoods="GENERATION"
19
-
20
- import gradio as gr
21
- def question_answer(context, question):
22
- pass # Implement your question-answering model here...
23
- import gradio as gr
24
-
25
- def greet(question):
26
- prompt = "" \
27
- "问答:" \
28
- "问题:" + question
29
- "答案:"
30
- prediction = cl.generate(
31
- model_name='clueai-base',
32
- prompt=prompt)
33
-
34
- return "小矢机器人:" + prediction.generations[0].text + "!"
35
-
36
- with gr.Blocks() as demo:
37
- question = gr.Textbox(label="Question")
38
- output = gr.Textbox(label="Answer Box")
39
- greet_btn = gr.Button("Ask")
40
- greet_btn.click(fn=greet, inputs=question, outputs=output)
41
-
42
- demo.launch()
 
1
+ # 加载模型
2
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
3
+ tokenizer = T5Tokenizer.from_pretrained("ClueAI/ChatYuan-large-v1")
4
+ model = T5ForConditionalGeneration.from_pretrained("ClueAI/ChatYuan-large-v1")
5
+ # 使用
6
+ import torch
7
+ from transformers import AutoTokenizer
8
+ # 修改colab笔记本设置为gpu,推理更快
9
+ device = torch.device('cuda')
10
+ model.to(device)
11
+ def preprocess(text):
12
+ text = text.replace("\n", "\\n").replace("\t", "\\t")
13
+ return text
14
+
15
+ def postprocess(text):
16
+ return text.replace("\\n", "\n").replace("\\t", "\t")
17
+
18
+ def answer(text, sample=True, top_p=1, temperature=0.7):
19
+ '''sample:是否抽样。生成任务,可以设置为True;
20
+ top_p:0-1之间,生成的内容越多样'''
21
+ text = preprocess(text)
22
+ encoding = tokenizer(text=[text], truncation=True, padding=True, max_length=768, return_tensors="pt").to(device)
23
+ if not sample:
24
+ out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=512, num_beams=1, length_penalty=0.6)
25
+ else:
26
+ 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)
27
+ out_text = tokenizer.batch_decode(out["sequences"], skip_special_tokens=True)
28
+ return postprocess(out_text[0])
29
+ print("end...")