Halo Master commited on
Commit
145c8a7
1 Parent(s): 55183e2
Files changed (2) hide show
  1. app.py +34 -3
  2. requirements.txt +4 -0
app.py CHANGED
@@ -1,7 +1,38 @@
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  iface.launch()
 
1
  import gradio as gr
2
 
 
 
3
 
4
+
5
+
6
+ import torch
7
+ from transformers import AutoTokenizer
8
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
9
+
10
+ # tokenizer = T5Tokenizer.from_pretrained("ClueAI/PromptCLUE-base")
11
+ # model = T5ForConditionalGeneration.from_pretrained("ClueAI/PromptCLUE-base")
12
+ tokenizer = T5Tokenizer.from_pretrained("ClueAI/PromptCLUE-base-v1-5")
13
+ model = T5ForConditionalGeneration.from_pretrained("ClueAI/PromptCLUE-base-v1-5")
14
+
15
+ device = torch.device('cpu')
16
+ model.to(device)
17
+ def preprocess(text):
18
+ return text.replace("\n", "_")
19
+
20
+ def postprocess(text):
21
+ return text.replace("_", "\n")
22
+
23
+ def answer(text, sample=False, top_p=0.6):
24
+ '''sample:是否抽样。生成任务,可以设置为True;
25
+ top_p:0-1之间,生成的内容越多样'''
26
+ text = preprocess(text)
27
+ encoding = tokenizer(text=[text], truncation=True, padding=True, max_length=768, return_tensors="pt").to(device)
28
+ if not sample:
29
+ out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=128, num_beams=4, length_penalty=0.6)
30
+ else:
31
+ out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=128, do_sample=True, top_p=top_p)
32
+ out_text = tokenizer.batch_decode(out["sequences"], skip_special_tokens=True)
33
+ return postprocess(out_text[0])
34
+
35
+
36
+
37
+ iface = gr.Interface(fn=answer, inputs="text", outputs="text")
38
  iface.launch()
requirements.txt CHANGED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio
4
+ sentencepiece