File size: 1,078 Bytes
d08456b
 
 
 
 
 
 
 
 
 
 
343c6bb
 
d08456b
 
 
 
 
 
 
 
 
 
 
 
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
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()