Spaces:
Sleeping
Sleeping
Tolga
commited on
Commit
β’
51ad49f
1
Parent(s):
8695dc5
Upload 2 files
Browse files- app.py +51 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#### Import Dependencies ####
|
2 |
+
import gradio as gr
|
3 |
+
import transformers
|
4 |
+
from transformers import pipeline
|
5 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
6 |
+
import torch
|
7 |
+
|
8 |
+
#### Model 1 ####
|
9 |
+
model_name = "snrspeaks/t5-one-line-summary"
|
10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
|
13 |
+
#### Model 2 ####
|
14 |
+
summarizer = pipeline(
|
15 |
+
"summarization",
|
16 |
+
"pszemraj/long-t5-tglobal-base-16384-book-summary",
|
17 |
+
device=0 if torch.cuda.is_available() else -1,
|
18 |
+
)
|
19 |
+
|
20 |
+
params = {
|
21 |
+
"max_length": 256,
|
22 |
+
"min_length": 8,
|
23 |
+
"no_repeat_ngram_size": 3,
|
24 |
+
"early_stopping": True,
|
25 |
+
"repetition_penalty": 3.5,
|
26 |
+
"length_penalty": 0.3,
|
27 |
+
"encoder_no_repeat_ngram_size": 3,
|
28 |
+
"num_beams": 4,
|
29 |
+
} # parameters for text generation out of model
|
30 |
+
|
31 |
+
|
32 |
+
#### Run the model 1####
|
33 |
+
def summarize(text):
|
34 |
+
input_ids = tokenizer.encode("summarize: " + text, return_tensors="pt", add_special_tokens=True)
|
35 |
+
generated_id = model.generate(input_ids=input_ids,num_beams=5,max_length=50,repetition_penalty=2.5,length_penalty=1,early_stopping=True,num_return_sequences=1)
|
36 |
+
pred = tokenizer.decode(generated_id[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
37 |
+
|
38 |
+
result = summarizer(text, **params)
|
39 |
+
pred2 = result[0]['summary_text']
|
40 |
+
|
41 |
+
return pred, pred2
|
42 |
+
|
43 |
+
#### Display summarized text ####
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
text = gr.Textbox(label="Text", lines=10, placeholder="Enter text here")
|
46 |
+
t1 = gr.Textbox(label="Output")
|
47 |
+
t2 = gr.Textbox(label="Output2")
|
48 |
+
btn = gr.Button("Summarise")
|
49 |
+
btn.click(fn=summarize, inputs=text, outputs=[t1,t2])
|
50 |
+
|
51 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
tensorflow
|