File size: 1,071 Bytes
42ae938
37830df
 
42ae938
37830df
 
 
9ef6dc2
42ae938
 
37830df
 
 
 
9ef6dc2
37830df
 
 
 
 
 
42ae938
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

model_id = "hramphul/bart-large-cnn"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
model.eval()

def summarize(text, max_len, min_len):
    inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
    with torch.no_grad():
        ids = model.generate(
            inputs["input_ids"],
            attention_mask=inputs["attention_mask"],
            max_length=int(max_len),
            min_length=int(min_len),
            num_beams=4,
            early_stopping=True,
        )
    return tokenizer.decode(ids[0], skip_special_tokens=True)

demo = gr.Interface(
    fn=summarize,
    inputs=[
        gr.Textbox(lines=10, label="Text to summarize"),
        gr.Slider(50, 300, value=130, step=10, label="Max length"),
        gr.Slider(10, 100, value=30, step=5, label="Min length"),
    ],
    outputs=gr.Textbox(label="Summary"),
    title="BART Large CNN Summarizer",
)

demo.launch()