jslin09 commited on
Commit
dccf4fa
1 Parent(s): 9bd8a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -39
app.py CHANGED
@@ -1,38 +1,29 @@
1
  import gradio as gr
 
 
2
  from transformers import pipeline, set_seed
3
- import random
4
 
5
- generator = pipeline('text-generation', model='jslin09/bloom-560m-finetuned-fraud')
 
 
6
 
7
- def rnd_generate(text):
8
- rnd_seed = random.randint(10, 500)
9
- set_seed(rnd_seed)
10
- result = generator(text,
11
- max_length=500,
12
- num_return_sequences=1,
13
- do_sample=True,
14
- temperature=0.75,
15
  top_k=50,
16
- top_p=0.9)
17
- return result[0]["generated_text"]
18
-
19
- def generate(text):
20
- set_seed(55)
21
- result = generator(text,
22
- max_length=500,
23
- num_return_sequences=1,
24
- do_sample=True,
25
- temperature=0.75,
26
- top_k=50,
27
- top_p=0.9)
28
- return result[0]["generated_text"]
29
-
30
 
31
  examples = [
32
  ["闕很大明知金融帳戶之存摺、提款卡及密碼係供自己使用之重要理財工具,"],
33
  ["梅友乾明知其無資力支付酒店消費,亦無付款意願,竟意圖為自己不法之所有,"],
34
- ["王大明意圖為自己不法所有,基於竊盜之犯意,"],
35
- ["陳小智知悉吳良醫院可配合假病患製作不實之診斷證明書、病歷資料,以供渠等向保險公司詐領住院保險給付,即意圖為自己不法之所有,"]
36
  ]
37
 
38
  with gr.Blocks() as demo:
@@ -42,17 +33,12 @@ with gr.Blocks() as demo:
42
  """)
43
  with gr.Row():
44
  with gr.Column():
45
- prompt = gr.components.Textbox(lines=5, label="Input Prompt", placeholder=examples[0])
46
- with gr.Row():
47
- with gr.Column():
48
- btn = gr.Button("Random Drafting")
49
- with gr.Column():
50
- btn2 = gr.Button("Drafting")
51
- gr.Examples(examples, inputs=[prompt])
52
- with gr.Column():
53
- result = gr.components.Textbox(lines=15, label="Generative")
54
- btn.click(rnd_generate, inputs=[prompt], outputs=[result])
55
- btn2.click(generate, inputs=[prompt], outputs=[result])
56
-
57
  if __name__ == "__main__":
58
- demo.launch() # 在遠端啟動時,需要 share=True 。
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM
3
+ from transformers import BloomTokenizerFast
4
  from transformers import pipeline, set_seed
 
5
 
6
+ model_name = "bloom-560m"
7
+ model = AutoModelForCausalLM.from_pretrained(f"jslin09/{model_name}-finetuned-fraud")
8
+ tokenizer = BloomTokenizerFast.from_pretrained(f'bigscience/{model_name}', bos_token = '<s>', eos_token = '</s>')
9
 
10
+ def generate(prompt):
11
+ result_length = len(prompt) + 4
12
+ inputs = tokenizer(prompt, return_tensors="pt") # 回傳的張量使用 Pytorch的格式。如果是 Tensorflow 格式的話,則指定為 "tf"。
13
+ results = model.generate(inputs["input_ids"],
14
+ num_return_sequences=5, # 產生五個句子回來。
15
+ max_length=result_length,
16
+ early_stopping=True,
17
+ do_sample=True,
18
  top_k=50,
19
+ top_p=0.9
20
+ )
21
+ return tokenizer.decode(results[0])
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  examples = [
24
  ["闕很大明知金融帳戶之存摺、提款卡及密碼係供自己使用之重要理財工具,"],
25
  ["梅友乾明知其無資力支付酒店消費,亦無付款意願,竟意圖為自己不法之所有,"],
26
+ ["王大明意圖為自己不法所有,基於竊盜之犯意,"]
 
27
  ]
28
 
29
  with gr.Blocks() as demo:
 
33
  """)
34
  with gr.Row():
35
  with gr.Column():
36
+ result = gr.components.Textbox(lines=7, label="生成的草稿", show_label=True)
37
+ prompt = gr.components.Textbox(lines=2, label="輸入提示文字", placeholder=examples[0],visible=False)
38
+ gr.Examples(examples, label='例句', inputs=[prompt])
39
+ prompt.change(generate, inputs=[prompt], outputs=[result])
40
+ btn = gr.Button("下一句")
41
+ btn.click(generate, inputs=[result], outputs=[result])
42
+
 
 
 
 
 
43
  if __name__ == "__main__":
44
+ demo.launch()