Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import T5Tokenizer, T5ForConditionalGeneration | |
| from urllib.parse import unquote | |
| # Load the T5 model and tokenizer | |
| tokenizer = T5Tokenizer.from_pretrained("t5-small") | |
| model = T5ForConditionalGeneration.from_pretrained("t5-small") | |
| # Function to summarize text | |
| def summarize_text(text): | |
| input_text = "summarize: " + text | |
| inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True) | |
| summary_ids = model.generate(inputs, max_length=50, num_beams=4, early_stopping=True) | |
| return tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
| # Function to get text from URL query parameters | |
| def get_text_from_url(request: gr.Request): | |
| params = request.query_params | |
| input_text = params.get("text", "") | |
| return unquote(input_text) # Decode URL-encoded text | |
| # Wrap inside a Blocks container | |
| with gr.Blocks() as demo: | |
| textbox = gr.Textbox(lines=5, placeholder="Enter text to summarize...") | |
| output = gr.Textbox(label="Summary") | |
| button = gr.Button("Summarize") | |
| button.click(summarize_text, inputs=textbox, outputs=output) | |
| demo.load(get_text_from_url, outputs=textbox) # Now inside Blocks | |
| demo.launch() |