File size: 1,633 Bytes
51ad49f
 
 
 
 
 
 
 
529cfcc
 
 
51ad49f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529cfcc
 
 
51ad49f
 
 
 
529cfcc
 
51ad49f
 
 
 
 
 
23e2e3f
51ad49f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#### Import Dependencies ####
import gradio as gr 
import transformers 
from transformers import pipeline
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch

#### Model 1 ####
#model_name = "snrspeaks/t5-one-line-summary"
#model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
#tokenizer = AutoTokenizer.from_pretrained(model_name)

#### Model 2 ####
summarizer = pipeline(
    "summarization",
    "pszemraj/long-t5-tglobal-base-16384-book-summary",
    device=0 if torch.cuda.is_available() else -1,
)

params = {
    "max_length": 256,
    "min_length": 8,
    "no_repeat_ngram_size": 3,
    "early_stopping": True,
    "repetition_penalty": 3.5,
    "length_penalty": 0.3,
    "encoder_no_repeat_ngram_size": 3,
    "num_beams": 4,
} # parameters for text generation out of model


#### Run the model 1####
def summarize(text):
    #input_ids = tokenizer.encode("summarize: " + text, return_tensors="pt", add_special_tokens=True)
    #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)
    #pred = tokenizer.decode(generated_id[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)

    result = summarizer(text, **params)
    pred2 = result[0]['summary_text']

    #output = pred + "\n\n" + pred2
    return pred2

#### Display summarized text ####
with gr.Blocks() as demo:
  text = gr.Textbox(label="Text", lines=10, placeholder="Enter text here")
  t1 = gr.Textbox(label="Output")
  btn = gr.Button("Summarise")
  btn.click(fn=summarize, inputs=text, outputs=t1)

demo.launch()