Spaces:
Runtime error
Runtime error
| import torch | |
| from transformers import T5ForConditionalGeneration, T5Tokenizer | |
| import gradio as gr | |
| model_name = "ramsrigouthamg/t5_paraphraser" # or smaller variant if available | |
| tokenizer = T5Tokenizer.from_pretrained(model_name) | |
| model = T5ForConditionalGeneration.from_pretrained(model_name) | |
| def paraphrase(text): | |
| if not text.strip(): | |
| return "Please enter text to paraphrase." | |
| input_text = "paraphrase: " + text + " </s>" | |
| encoding = tokenizer.encode_plus( | |
| input_text, max_length=128, padding="max_length", truncation=True, return_tensors="pt" | |
| ) | |
| outputs = model.generate( | |
| input_ids=encoding["input_ids"], | |
| attention_mask=encoding["attention_mask"], | |
| max_length=128, | |
| num_beams=3, # Reduced beams for faster inference | |
| num_return_sequences=1, | |
| early_stopping=True, | |
| do_sample=False # Deterministic output, faster | |
| ) | |
| paraphrased = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return paraphrased | |
| demo = gr.Interface( | |
| fn=paraphrase, | |
| inputs=gr.Textbox(lines=4, placeholder="Enter text to paraphrase..."), | |
| outputs="text", | |
| title="Fast & Accurate Paraphraser (T5)", | |
| description="Paraphrase text using a fine-tuned T5 paraphraser optimized for speed." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |