| import gradio as gr | |
| from transformers import pipeline | |
| from parrot import Parrot | |
| import torch | |
| import language_tool_python | |
| import nltk | |
| nltk.download('punkt') | |
| nltk.download('averaged_perceptron_tagger') | |
| tool = language_tool_python.LanguageTool('en-US') | |
| parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5", use_gpu=False) | |
| def paraphrase_text(input_text, mode="Standard"): | |
| if not input_text.strip(): | |
| return "", "" | |
| corrected = tool.correct(input_text) | |
| para_phrases = parrot.augment( | |
| input_phrase=corrected, | |
| diversity_ranker="levenshtein", | |
| do_diverse=True, | |
| max_return_phrases=5, | |
| adequacy_threshold=0.90, | |
| fluency_threshold=0.90 | |
| ) | |
| if not para_phrases: | |
| return corrected, "No paraphrase found." | |
| best = para_phrases[0][0] | |
| if mode == "Formal": | |
| best = best.replace("gonna", "going to").replace("wanna", "want to") | |
| elif mode == "Creative": | |
| best = best.capitalize() + " π" | |
| elif mode == "Concise": | |
| best = " ".join(best.split()[:12]) + "..." | |
| return corrected, best | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("## π AI Paraphraser β Better Than QuillBot") | |
| gr.Markdown("### Enter your text and select a paraphrasing mode") | |
| input_text = gr.Textbox(label="Original Text", placeholder="Type your sentence here...", lines=5) | |
| mode = gr.Dropdown(["Standard", "Formal", "Creative", "Concise"], label="Paraphrasing Mode", value="Standard") | |
| with gr.Row(): | |
| btn = gr.Button("π Paraphrase Now") | |
| clear = gr.Button("π§Ή Clear") | |
| output_corrected = gr.Textbox(label="π§Ή Corrected Input (Grammar fixed)", lines=3) | |
| output_paraphrased = gr.Textbox(label="β¨ Paraphrased Output", lines=3) | |
| btn.click(fn=paraphrase_text, inputs=[input_text, mode], outputs=[output_corrected, output_paraphrased]) | |
| clear.click(lambda: ("", "", ""), None, [input_text, output_corrected, output_paraphrased]) | |
| demo.launch() | |