Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,14 @@ from parrot import Parrot
|
|
| 3 |
import nltk
|
| 4 |
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 5 |
import re
|
|
|
|
| 6 |
# -----------------------------
|
| 7 |
# Setup
|
| 8 |
# -----------------------------
|
| 9 |
-
nltk.data.path.append("./nltk_data") #
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
parrot = None # global variable
|
| 14 |
def get_parrot():
|
| 15 |
global parrot
|
| 16 |
if parrot is None:
|
|
@@ -18,56 +18,126 @@ def get_parrot():
|
|
| 18 |
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5", use_gpu=False)
|
| 19 |
print("✅ Parrot model loaded successfully!")
|
| 20 |
return parrot
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# -----------------------------
|
| 23 |
-
# Helper
|
| 24 |
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def split_long_sentence(sentence, max_tokens=MAX_TOKENS):
|
| 26 |
words = word_tokenize(sentence)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
cleaned = []
|
| 34 |
-
for s in sentences:
|
| 35 |
-
s = s.strip()
|
| 36 |
-
if s:
|
| 37 |
-
s = re.sub(r'[.!?]+$', '', s)
|
| 38 |
-
s = re.sub(r'\s{2,}', ' ', s)
|
| 39 |
-
s = s[0].upper() + s[1:]
|
| 40 |
-
cleaned.append(s)
|
| 41 |
-
result = ". ".join(cleaned)
|
| 42 |
-
if result and not result.endswith("."):
|
| 43 |
-
result += "."
|
| 44 |
-
return result
|
| 45 |
def rephrase(text):
|
| 46 |
-
model = get_parrot()
|
| 47 |
sentences = sent_tokenize(text)
|
| 48 |
rephrased = []
|
|
|
|
| 49 |
for s in sentences:
|
| 50 |
chunks = split_long_sentence(s)
|
| 51 |
paraphrased_chunks = []
|
| 52 |
for c in chunks:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
return clean_sentences(rephrased)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
iface = gr.Interface(
|
| 67 |
fn=rephrase,
|
| 68 |
inputs=gr.Textbox(lines=10, placeholder="Paste your text here..."),
|
| 69 |
outputs="text",
|
| 70 |
-
title="Parrot Rephraser",
|
| 71 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
-
|
|
|
|
|
|
| 3 |
import nltk
|
| 4 |
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 5 |
import re
|
| 6 |
+
|
| 7 |
# -----------------------------
|
| 8 |
# Setup
|
| 9 |
# -----------------------------
|
| 10 |
+
nltk.data.path.append("./nltk_data") # Local punkt
|
| 11 |
+
parrot = None # Lazy-loaded global model
|
| 12 |
+
|
| 13 |
+
|
|
|
|
| 14 |
def get_parrot():
|
| 15 |
global parrot
|
| 16 |
if parrot is None:
|
|
|
|
| 18 |
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5", use_gpu=False)
|
| 19 |
print("✅ Parrot model loaded successfully!")
|
| 20 |
return parrot
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
MAX_TOKENS = 150 # limit per chunk for stability
|
| 24 |
+
|
| 25 |
+
|
| 26 |
# -----------------------------
|
| 27 |
+
# Helper: Common utilities
|
| 28 |
# -----------------------------
|
| 29 |
+
def clean_sentence(sent):
|
| 30 |
+
sent = sent.strip()
|
| 31 |
+
sent = re.sub(r"[.!?]+$", "", sent)
|
| 32 |
+
if sent:
|
| 33 |
+
sent = sent[0].upper() + sent[1:]
|
| 34 |
+
if not sent.endswith("."):
|
| 35 |
+
sent += "."
|
| 36 |
+
return sent
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def clean_sentences(sentences):
|
| 40 |
+
cleaned = [clean_sentence(s) for s in sentences if s.strip()]
|
| 41 |
+
return " ".join(cleaned)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
def split_long_sentence(sentence, max_tokens=MAX_TOKENS):
|
| 45 |
words = word_tokenize(sentence)
|
| 46 |
+
return [" ".join(words[i:i + max_tokens]) for i in range(0, len(words), max_tokens)]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# -----------------------------
|
| 50 |
+
# 🔹 App 1: Full Paragraph Rephraser
|
| 51 |
+
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def rephrase(text):
|
| 53 |
+
model = get_parrot()
|
| 54 |
sentences = sent_tokenize(text)
|
| 55 |
rephrased = []
|
| 56 |
+
|
| 57 |
for s in sentences:
|
| 58 |
chunks = split_long_sentence(s)
|
| 59 |
paraphrased_chunks = []
|
| 60 |
for c in chunks:
|
| 61 |
+
try:
|
| 62 |
+
p = model.augment(
|
| 63 |
+
input_phrase=c,
|
| 64 |
+
do_diverse=True,
|
| 65 |
+
adequacy_threshold=0.85,
|
| 66 |
+
fluency_threshold=0.9,
|
| 67 |
+
)
|
| 68 |
+
paraphrased_chunks.append(p[0][0] if p else c)
|
| 69 |
+
except Exception:
|
| 70 |
+
paraphrased_chunks.append(c)
|
| 71 |
+
rephrased.append(" ".join(paraphrased_chunks))
|
| 72 |
+
|
| 73 |
return clean_sentences(rephrased)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
rephrase_iface = gr.Interface(
|
|
|
|
| 77 |
fn=rephrase,
|
| 78 |
inputs=gr.Textbox(lines=10, placeholder="Paste your text here..."),
|
| 79 |
outputs="text",
|
| 80 |
+
title="Parrot Rephraser (Long Text)",
|
| 81 |
+
description="Paraphrases long text while maintaining punctuation and capitalization.",
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# -----------------------------
|
| 86 |
+
# 🔹 App 2: Sentence-wise Multiple Paraphrases
|
| 87 |
+
# -----------------------------
|
| 88 |
+
def generate_unique_paraphrases(sentence, N_OPTIONS=3):
|
| 89 |
+
model = get_parrot()
|
| 90 |
+
try:
|
| 91 |
+
paraphrases = model.augment(
|
| 92 |
+
input_phrase=sentence,
|
| 93 |
+
do_diverse=True,
|
| 94 |
+
adequacy_threshold=0.85,
|
| 95 |
+
fluency_threshold=0.9,
|
| 96 |
+
)
|
| 97 |
+
except Exception:
|
| 98 |
+
paraphrases = []
|
| 99 |
+
|
| 100 |
+
if paraphrases:
|
| 101 |
+
texts = [p[0] for p in paraphrases]
|
| 102 |
+
unique = []
|
| 103 |
+
for t in texts:
|
| 104 |
+
if t not in unique:
|
| 105 |
+
unique.append(t)
|
| 106 |
+
if len(unique) == N_OPTIONS:
|
| 107 |
+
break
|
| 108 |
+
return unique
|
| 109 |
+
else:
|
| 110 |
+
return [sentence]
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def rephrase_sentencewise_unique(text, N_OPTIONS=3):
|
| 114 |
+
sentences = sent_tokenize(text.strip())
|
| 115 |
+
results = []
|
| 116 |
+
for idx, s in enumerate(sentences, 1):
|
| 117 |
+
paraphrases = generate_unique_paraphrases(s, N_OPTIONS)
|
| 118 |
+
paraphrases = [clean_sentence(p) for p in paraphrases]
|
| 119 |
+
formatted = f"Sentence {idx}: {s}\n"
|
| 120 |
+
for i, opt in enumerate(paraphrases, 1):
|
| 121 |
+
formatted += f" Option {i}: {opt}\n"
|
| 122 |
+
results.append(formatted)
|
| 123 |
+
return "\n".join(results)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
sentencewise_iface = gr.Interface(
|
| 127 |
+
fn=rephrase_sentencewise_unique,
|
| 128 |
+
inputs=gr.Textbox(lines=10, placeholder="Paste text here..."),
|
| 129 |
+
outputs="text",
|
| 130 |
+
title="Parrot Rephraser (Sentence-wise Options)",
|
| 131 |
+
description="Generates top 3 unique paraphrases per sentence. Optimized for HF free-tier.",
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# -----------------------------
|
| 136 |
+
# 🔹 Combine both interfaces into Tabs
|
| 137 |
+
# -----------------------------
|
| 138 |
+
demo = gr.TabbedInterface(
|
| 139 |
+
[rephrase_iface, sentencewise_iface],
|
| 140 |
+
["Full Text Rephraser", "Sentence-wise Paraphrases"],
|
| 141 |
)
|
| 142 |
+
|
| 143 |
+
demo.launch()
|