Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
def
|
| 13 |
if not text.strip():
|
| 14 |
return "Please enter some text to paraphrase."
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from parrot import Parrot
|
| 3 |
+
import torch
|
| 4 |
+
import language_tool_python
|
| 5 |
|
| 6 |
+
# Initialize Parrot model
|
| 7 |
+
parrot = Parrot(
|
| 8 |
+
model_tag="prithivida/parrot_paraphraser_on_T5",
|
| 9 |
+
use_gpu=torch.cuda.is_available()
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Grammar checker
|
| 13 |
+
tool = language_tool_python.LanguageTool('en-US')
|
| 14 |
|
| 15 |
+
def grammar_correct(text):
|
| 16 |
+
"""Correct grammar using LanguageTool."""
|
| 17 |
+
matches = tool.check(text)
|
| 18 |
+
return language_tool_python.utils.correct(text, matches)
|
| 19 |
|
| 20 |
+
def generate_paraphrase(text: str, do_grammar=True) -> str:
|
| 21 |
if not text.strip():
|
| 22 |
return "Please enter some text to paraphrase."
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Generate a paraphrase using Parrot
|
| 26 |
+
paras = parrot.augment(
|
| 27 |
+
input_phrase=text,
|
| 28 |
+
max_return_phrases=1,
|
| 29 |
+
adequacy_threshold=0.9,
|
| 30 |
+
fluency_threshold=0.9,
|
| 31 |
+
do_diverse=False
|
| 32 |
+
)
|
| 33 |
+
if paras and paras[0]:
|
| 34 |
+
output = paras[0][0]
|
| 35 |
+
else:
|
| 36 |
+
output = "Could not generate a meaningful paraphrase."
|
| 37 |
+
|
| 38 |
+
# Apply grammar correction if selected
|
| 39 |
+
if do_grammar:
|
| 40 |
+
output = grammar_correct(output)
|
| 41 |
+
|
| 42 |
+
return output
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error during paraphrasing: {str(e)}"
|
| 46 |
+
|
| 47 |
+
# Gradio UI
|
| 48 |
+
with gr.Blocks(css=".gradio-container { max-width: 800px !important; margin: auto; }") as demo:
|
| 49 |
+
gr.Markdown("# ✨ Paraphrasing Tool\nWrite better, clearer, and smarter!")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
input_text = gr.Textbox(label="Enter your text", placeholder="Type or paste your sentence here...", lines=4)
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
do_grammar = gr.Checkbox(label="Apply grammar correction", value=True)
|
| 56 |
+
run_btn = gr.Button("Paraphrase")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
output_text = gr.Textbox(label="Paraphrased Output", lines=4)
|
| 60 |
+
|
| 61 |
+
run_btn.click(fn=generate_paraphrase, inputs=[input_text, do_grammar], outputs=output_text)
|
| 62 |
|
| 63 |
demo.launch()
|