Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
from transformers import GenerationConfig
|
4 |
+
from transformers import AutoModelForSeq2SeqLM
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
7 |
+
headline = AutoModelForSeq2SeqLM.from_pretrained("wetey/content-summarizer")
|
8 |
+
generate_long = AutoModelForSeq2SeqLM.from_pretrained("wetey/content-generator")
|
9 |
+
|
10 |
+
def generate_headline(text):
|
11 |
+
inputs = tokenizer(text, return_tensors="pt").input_ids
|
12 |
+
|
13 |
+
generation_config = GenerationConfig(temperature = 1.2,
|
14 |
+
encoder_no_repeat_ngram_size = 4)
|
15 |
+
|
16 |
+
outputs = headline.generate(inputs,
|
17 |
+
do_sample = True,
|
18 |
+
generation_config = generation_config)
|
19 |
+
|
20 |
+
return tokenizer.decode(outputs[0], skip_special_tokens = True)
|
21 |
+
|
22 |
+
def generate_content(text):
|
23 |
+
inputs = tokenizer(text, return_tensors="pt").input_ids
|
24 |
+
generation_config = GenerationConfig(temperature = 1.2,
|
25 |
+
encoder_no_repeat_ngram_size = 2,
|
26 |
+
min_length = 50,
|
27 |
+
max_length = 512,
|
28 |
+
length_penalty = 1.5,
|
29 |
+
num_beams = 4,
|
30 |
+
repetition_penalty = 1.5,
|
31 |
+
no_repeat_ngram_size = 3)
|
32 |
+
outputs = generate_long.generate(inputs,
|
33 |
+
do_sample = True,
|
34 |
+
generation_config = generation_config)
|
35 |
+
|
36 |
+
return tokenizer.decode(outputs[0], skip_special_tokens = True)
|
37 |
+
|
38 |
+
textbox = gr.Textbox(label="Type your text here", lines=2)
|
39 |
+
|
40 |
+
demo = gr.Blocks()
|
41 |
+
|
42 |
+
with demo:
|
43 |
+
text_input = gr.Textbox()
|
44 |
+
text_output = gr.Textbox()
|
45 |
+
|
46 |
+
b1 = gr.Button("Generate headline")
|
47 |
+
b2 = gr.Button("Generate long content")
|
48 |
+
|
49 |
+
b1.click(generate_headline, inputs=text_input, outputs=text_output)
|
50 |
+
b2.click(generate_content, inputs=text_input, outputs=text_output)
|
51 |
+
|
52 |
+
demo.launch()
|