File size: 1,624 Bytes
2f9e23c
ad7b5db
 
2f9e23c
ad7b5db
2f9e23c
ad7b5db
 
 
 
 
 
 
 
 
2f9e23c
8ccb87b
ad7b5db
 
2f9e23c
ad7b5db
 
 
 
 
 
 
 
 
 
 
2f9e23c
ad7b5db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

MODEL_NAME = "facebook/bart-large-cnn"

# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)

device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)


def summarize(text, max_length, min_length):
    if not text.strip():
        return "Please enter some text to summarize."

    inputs = tokenizer(
        text,
        return_tensors="pt",
        max_length=1024,
        truncation=True
    ).to(device)

    summary_ids = model.generate(
        inputs["input_ids"],
        num_beams=4,
        max_length=max_length,
        min_length=min_length,
        early_stopping=True
    )

    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary


with gr.Blocks(title="Advanced BART Summarizer") as demo:
    gr.Markdown("# Advanced BART Text Summarizer")
    gr.Markdown("Summarization using facebook/bart-large-cnn")

    input_text = gr.Textbox(
        lines=12,
        placeholder="Enter long article or paragraph here..."
    )

    with gr.Row():
        max_len = gr.Slider(50, 300, value=150, label="Max Summary Length")
        min_len = gr.Slider(20, 100, value=40, label="Min Summary Length")

    output_text = gr.Textbox(
        lines=8,
        label="Generated Summary"
    )

    summarize_btn = gr.Button("Summarize")

    summarize_btn.click(
        summarize,
        inputs=[input_text, max_len, min_len],
        outputs=output_text
    )

demo.launch()