File size: 1,789 Bytes
0006ae1
a76270d
 
 
 
 
 
 
 
 
 
0006ae1
 
a76270d
 
 
0006ae1
a76270d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0006ae1
a76270d
 
0006ae1
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import BartTokenizer, BartForConditionalGeneration
import torch

# Load the fine-tuned BART model and tokenizer from the local directory
MODEL_DIR = './BART model small/model'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

tokenizer = BartTokenizer.from_pretrained(MODEL_DIR)
model = BartForConditionalGeneration.from_pretrained(MODEL_DIR).to(device)

# Define the summarization function
def summarize(text):
    try:
        # Tokenize the input article
        inputs = tokenizer(
            text,
            return_tensors="pt",
            max_length=1024,
            truncation=True
        ).to(device)

        # Generate the summary
        summary_ids = model.generate(
            inputs['input_ids'],
            attention_mask=inputs['attention_mask'],
            max_length=150,  # Set maximum length for the summary
            min_length=30,   # Set minimum length for the summary
            num_beams=4,     # Use beam search to generate the summary
            early_stopping=True
        )

        # Decode the summary
        summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)

        return summary
    
    except Exception as e:
        return str(e)

# Create Gradio interface
# Textbox input for the article and output for the summary
interface = gr.Interface(
    fn=summarize,          # The function to summarize the article
    inputs="text",         # Input is a text box where users can input the article text
    outputs="text",        # Output is a text box displaying the summary
    title="BART Summarization",  # The title of the app
    description="Enter an article to generate a summary using a fine-tuned BART model."
)

# Launch the Gradio app
interface.launch()