Spaces:
Sleeping
Sleeping
File size: 1,067 Bytes
052782a |
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 |
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()
|