Upload 89 files
Browse files
app/engine/paraphrase/__init__.py
CHANGED
|
@@ -173,40 +173,49 @@ def _generate_raw(
|
|
| 173 |
tokenizer, model = loaded
|
| 174 |
import torch
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
truncation=True,
|
| 181 |
-
max_length=256,
|
| 182 |
-
)
|
| 183 |
returns = max(1, num_return)
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
cleaned: list[str] = []
|
| 211 |
seen: set[str] = set()
|
| 212 |
source_key = text.strip().lower().rstrip(".!?")
|
|
|
|
| 173 |
tokenizer, model = loaded
|
| 174 |
import torch
|
| 175 |
|
| 176 |
+
prompts = [
|
| 177 |
+
f"paraphrase: {text.strip()}",
|
| 178 |
+
f"rewrite with different wording but same meaning: {text.strip()}",
|
| 179 |
+
]
|
|
|
|
|
|
|
|
|
|
| 180 |
returns = max(1, num_return)
|
| 181 |
+
per_prompt = max(1, returns // len(prompts))
|
| 182 |
+
if per_prompt * len(prompts) < returns:
|
| 183 |
+
per_prompt += 1
|
| 184 |
+
decoded: list[str] = []
|
| 185 |
+
for prompt in prompts:
|
| 186 |
+
encoded = tokenizer(
|
| 187 |
+
prompt,
|
| 188 |
+
return_tensors="pt",
|
| 189 |
+
truncation=True,
|
| 190 |
+
max_length=256,
|
| 191 |
+
)
|
| 192 |
+
prompt_returns = per_prompt
|
| 193 |
+
groups = min(prompt_returns, 4)
|
| 194 |
+
beams = max(6, prompt_returns * 2)
|
| 195 |
+
beams = max(groups, (beams // groups) * groups)
|
| 196 |
+
with torch.no_grad():
|
| 197 |
+
try:
|
| 198 |
+
outputs = model.generate(
|
| 199 |
+
**encoded,
|
| 200 |
+
max_new_tokens=ENGINE_PARAPHRASE_MAX_NEW_TOKENS,
|
| 201 |
+
num_beams=beams,
|
| 202 |
+
num_beam_groups=groups,
|
| 203 |
+
diversity_penalty=1.0,
|
| 204 |
+
num_return_sequences=prompt_returns,
|
| 205 |
+
do_sample=False,
|
| 206 |
+
early_stopping=True,
|
| 207 |
+
)
|
| 208 |
+
except Exception:
|
| 209 |
+
# Fall back to plain beam search if diverse beams are unsupported.
|
| 210 |
+
outputs = model.generate(
|
| 211 |
+
**encoded,
|
| 212 |
+
max_new_tokens=ENGINE_PARAPHRASE_MAX_NEW_TOKENS,
|
| 213 |
+
num_beams=max(4, prompt_returns),
|
| 214 |
+
num_return_sequences=prompt_returns,
|
| 215 |
+
do_sample=False,
|
| 216 |
+
early_stopping=True,
|
| 217 |
+
)
|
| 218 |
+
decoded.extend(tokenizer.batch_decode(outputs, skip_special_tokens=True))
|
| 219 |
cleaned: list[str] = []
|
| 220 |
seen: set[str] = set()
|
| 221 |
source_key = text.strip().lower().rstrip(".!?")
|
app/engine/paraphrase/__pycache__/__init__.cpython-311.pyc
CHANGED
|
Binary files a/app/engine/paraphrase/__pycache__/__init__.cpython-311.pyc and b/app/engine/paraphrase/__pycache__/__init__.cpython-311.pyc differ
|
|
|