RohitCSharp's picture
Update app.py
343c6bb verified
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
# Load the model and tokenizer
model_name = "t5-small"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
# Define the summarization function
def summarize_text(text):
input_text = "summarize: " + text.strip()
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=500, truncation=True)
summary_ids = model.generate(input_ids, max_length=140, min_length=40, length_penalty=2.0, num_beams=2, early_stopping=True)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
# Gradio interface
iface = gr.Interface(fn=summarize_text,
inputs=gr.Textbox(lines=15, placeholder="Paste your text here..."),
outputs=gr.Textbox(label="Summary"),
title="T5 Text Summarizer",
description="Enter any long English text to get a summarized version using the T5 model.")
# Launch
iface.launch()