Text_Summarizer / app.py
ritampatra's picture
Upload 2 files
052782a verified
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
# Load model and tokenizer
model_name = "t5-small"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
# Function to summarize text
def summarize_text(text, max_length=100):
input_text = "summarize: " + text
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
summary_ids = model.generate(inputs, max_length=max_length, min_length=30, length_penalty=2.0, num_beams=4)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
# Gradio UI
iface = gr.Interface(
fn=summarize_text,
inputs=[gr.Textbox(label="Enter Text to Summarize"), gr.Slider(50, 200, step=10, label="Max Length")],
outputs="text",
title="Text Summarization App",
description="This app summarizes long texts using the T5 Transformer model.",
)
# Launch the app
if __name__ == "__main__":
iface.launch()