Spaces:
Sleeping
Sleeping
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() | |