File size: 1,051 Bytes
f6ab87f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import transformers

model_name = "t5-small"
tokenizer = transformers.T5Tokenizer.from_pretrained(model_name)
model = transformers.T5ForCausalLM.from_pretrained(model_name)

def summarize_text(text, max_length):
    input_ids = tokenizer.encode(text, return_tensors='pt', max_length=512)
    summary_ids = model.generate(input_ids, 
                                 max_length=max_length, 
                                 num_beams=4, 
                                 early_stopping=True)
    return tokenizer.decode(summary_ids[0], skip_special_tokens=True)

iface = gr.Interface(
    fn=summarize_text,
    inputs=gr.inputs.Textbox(lines=5, default="Enter your text here"),
    outputs=gr.outputs.Textbox(lines=3, default="Summary will appear here"),
    parameters={
        "max_length": gr.inputs.Slider(default=50, min_value=20, max_value=200, step=10, label="Summary Length")
    },
    title="Text Summarization with T5",
    description="Generate a brief summary of the input text using the T5 model."
)

iface.launch()