Nguyen17 commited on
Commit
32f2451
·
verified ·
1 Parent(s): 82f2fb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -4
app.py CHANGED
@@ -1,7 +1,45 @@
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
+ import torch
3
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel
4
+
5
+
6
+ tokenizer = GPT2Tokenizer.from_pretrained('NlpHUST/gpt2-vietnamese')
7
+ model = GPT2LMHeadModel.from_pretrained('NlpHUST/gpt2-vietnamese')
8
+ # max_length = 100
9
+
10
+
11
+ def run(text, intensity):
12
+ res=""
13
+ max_length=intensity
14
+ input_ids = tokenizer.encode(text, return_tensors='pt')
15
+ sample_outputs = model.generate(input_ids,pad_token_id=tokenizer.eos_token_id,
16
+ do_sample=True,
17
+ max_length=max_length,
18
+ min_length=max_length,
19
+ top_k=40,
20
+ num_beams=5,
21
+ early_stopping=True,
22
+ no_repeat_ngram_size=2,
23
+ num_return_sequences=3)
24
+
25
+ for i, sample_output in enumerate(sample_outputs):
26
+ res +="Generated text {}\n\n{}".format(i+1, tokenizer.decode(sample_output.tolist()))
27
+ res +='\n---'
28
+ return res
29
+
30
+ demo = gr.Interface(
31
+ fn=run,
32
+ inputs=["text", "slider"],
33
+ outputs=["text"],
34
+ )
35
+
36
+ demo.launch()
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
 
 
 
45