Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243import gradio as grimport nltknltk.download('punkt') # Download necessary datafrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM# Load model and tokenizermodel_name = "Vamsi/T5_Paraphrase_Paws"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForSeq2SeqLM.from_pretrained(model_name)def paraphrase(text):if not text.strip():return "Please enter some text to paraphrase."input_text = f"paraphrase: {text} </s>"encoding = tokenizer.encode_plus(input_text,max_length=256,padding="max_length",return_tensors="pt",truncation=True)input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]outputs = model.generate(input_ids=input_ids,attention_mask=attention_mask,max_length=256,num_beams=5,num_return_sequences=1,temperature=1.5)return tokenizer.decode(outputs[0], skip_special_tokens=True)# Gradio Interfacedemo = gr.Interface(fn=paraphrase,inputs=gr.Textbox(label="Enter your text to paraphrase"),outputs=gr.Textbox(label="Paraphrased text"),title="AI Paraphrasing Tool",description="Enter your sentence or paragraph, and the model will return a paraphrased version.")demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import gradio as grfrom parrot import Parrotimport torchimport language_tool_python# Initialize Parrot modelparrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5",use_gpu=torch.cuda.is_available())# Grammar checkertool = language_tool_python.LanguageTool('en-US')def grammar_correct(text):"""Correct grammar using LanguageTool."""matches = tool.check(text)return language_tool_python.utils.correct(text, matches)def generate_paraphrase(text: str, do_grammar=True) -> str:if not text.strip():return "Please enter some text to paraphrase."try:# Generate a paraphrase using Parrotparas = parrot.augment(input_phrase=text,max_return_phrases=1,adequacy_threshold=0.9,fluency_threshold=0.9,do_diverse=False)if paras and paras[0]:output = paras[0][0]else:output = "Could not generate a meaningful paraphrase."# Apply grammar correction if selectedif do_grammar:output = grammar_correct(output)return outputexcept Exception as e:return f"Error during paraphrasing: {str(e)}"# Gradio UIwith gr.Blocks(css=".gradio-container { max-width: 800px !important; margin: auto; }") as demo:gr.Markdown("# ✨ Paraphrasing Tool\nWrite better, clearer, and smarter!")with gr.Row():input_text = gr.Textbox(label="Enter your text", placeholder="Type or paste your sentence here...", lines=4)with gr.Row():do_grammar = gr.Checkbox(label="Apply grammar correction", value=True)run_btn = gr.Button("Paraphrase")with gr.Row():output_text = gr.Textbox(label="Paraphrased Output", lines=4)run_btn.click(fn=generate_paraphrase, inputs=[input_text, do_grammar], outputs=output_text)demo.launch()
app.py+55-35