File size: 1,014 Bytes
379e449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import PegasusTokenizer, PegasusForConditionalGeneration

# Load Pegasus model and tokenizer
model_name = "google/pegasus-xsum"
tokenizer = PegasusTokenizer.from_pretrained(model_name)
model = PegasusForConditionalGeneration.from_pretrained(model_name)

# Function to summarize text
def summarize_text(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
    summary_ids = model.generate(inputs.input_ids, max_length=128, min_length=30, length_penalty=2.0, num_beams=5)
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary

# Gradio interface
iface = gr.Interface(fn=summarize_text, 
                     inputs=gr.Textbox(label="Enter text to summarize"), 
                     outputs=gr.Textbox(label="Summary"),
                     title="Pegasus Text Summarizer",
                     description="This AI agent summarizes long text using the Pegasus model.")

# Launch the app
iface.launch()