Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,88 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import torch, gradio as gr
|
| 3 |
import re
|
| 4 |
|
| 5 |
+
# --- Load Model ---
|
| 6 |
+
model_name = "prithivida/parrot_paraphraser_on_T5"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
model = model.to(device)
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
# --- Helpers ---
|
| 15 |
+
def split_paragraphs(text):
|
| 16 |
+
"""Split text into paragraphs based on line breaks."""
|
| 17 |
+
paragraphs = [p.strip() for p in text.split("\n") if p.strip()]
|
| 18 |
+
return paragraphs
|
| 19 |
+
|
| 20 |
+
def split_sentences(text):
|
| 21 |
+
"""Split paragraph into sentences."""
|
| 22 |
+
sentences = re.split(r'(?<=[.!?])\s+', text.strip())
|
| 23 |
+
return [s for s in sentences if s]
|
| 24 |
+
|
| 25 |
+
def clean_sentence(sent):
|
| 26 |
+
"""Clean and ensure sentence ends with punctuation."""
|
| 27 |
+
sent = re.sub(r'\s+', ' ', sent).strip()
|
| 28 |
+
if not sent.endswith(('.', '!', '?')):
|
| 29 |
+
sent += "."
|
| 30 |
+
return sent
|
| 31 |
+
|
| 32 |
+
# --- Main function ---
|
| 33 |
+
def paraphrase_fn(text, num_return_sequences=1, temperature=1.2, top_p=0.92):
|
| 34 |
+
if not text.strip():
|
| 35 |
+
return "Enter some text"
|
| 36 |
+
|
| 37 |
+
num_return_sequences = int(num_return_sequences)
|
| 38 |
+
paragraphs = split_paragraphs(text)
|
| 39 |
+
paraphrased_paragraphs = []
|
| 40 |
+
|
| 41 |
+
for para in paragraphs:
|
| 42 |
+
sentences = split_sentences(para)
|
| 43 |
+
paraphrased_sentences = []
|
| 44 |
+
|
| 45 |
+
for sent in sentences:
|
| 46 |
+
input_text = "paraphrase: " + sent + " </s>"
|
| 47 |
+
inputs = tokenizer([input_text], return_tensors="pt", truncation=True, padding=True).to(device)
|
| 48 |
+
|
| 49 |
+
outputs = model.generate(
|
| 50 |
+
**inputs,
|
| 51 |
+
max_new_tokens=128,
|
| 52 |
+
num_return_sequences=num_return_sequences,
|
| 53 |
+
do_sample=True,
|
| 54 |
+
top_p=float(top_p),
|
| 55 |
+
temperature=float(temperature),
|
| 56 |
+
)
|
| 57 |
+
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 58 |
+
|
| 59 |
+
seen, unique = set(), []
|
| 60 |
+
for d in decoded:
|
| 61 |
+
d = clean_sentence(d)
|
| 62 |
+
if d not in seen:
|
| 63 |
+
unique.append(d)
|
| 64 |
+
seen.add(d)
|
| 65 |
+
|
| 66 |
+
paraphrased_sentences.append(unique[0])
|
| 67 |
+
|
| 68 |
+
# Join sentences for this paragraph
|
| 69 |
+
paraphrased_paragraphs.append(" ".join(paraphrased_sentences))
|
| 70 |
+
|
| 71 |
+
# Join paragraphs with double line breaks to preserve paragraphing
|
| 72 |
+
return "\n\n".join(paraphrased_paragraphs)
|
| 73 |
+
|
| 74 |
+
# --- Gradio Interface ---
|
| 75 |
+
iface = gr.Interface(
|
| 76 |
+
fn=paraphrase_fn,
|
| 77 |
+
inputs=[
|
| 78 |
+
gr.Textbox(lines=12, placeholder="Paste text here..."),
|
| 79 |
+
gr.Slider(1, 3, step=1, value=1, label="Variants"),
|
| 80 |
+
gr.Slider(0.5, 2.0, step=0.1, value=1.2, label="Temperature"),
|
| 81 |
+
gr.Slider(0.6, 1.0, step=0.01, value=0.92, label="Top-p"),
|
| 82 |
+
],
|
| 83 |
+
outputs=gr.Textbox(label="Output"),
|
| 84 |
+
title="📝 Writenix API",
|
| 85 |
+
description="This Space provides a UI *and* an API for paraphrasing text while preserving paragraphs."
|
| 86 |
)
|
| 87 |
|
| 88 |
+
iface.launch()
|
|
|