File size: 11,863 Bytes
0514442 492ec2c 6086714 492ec2c 0514442 6086714 492ec2c cacd467 492ec2c cacd467 492ec2c 0514442 492ec2c 0514442 492ec2c 0514442 492ec2c 0514442 492ec2c 0514442 492ec2c 0514442 f15f34c 0514442 f15f34c 0514442 f15f34c 0514442 6086714 492ec2c | 1 2 3 4 5 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | """Any2human rewrite pipeline — claim-atom content creation flow."""
from __future__ import annotations
import os
import re
import time
from dataclasses import dataclass
from typing import Callable, Optional
from openai import OpenAI
import prompts
from mechanics import mechanical_humanize
ProgressCallback = Optional[Callable[[str], None]]
CHUNK_TARGET_WORDS = 600
CHUNK_OVERLAP_WORDS = 40
MAX_INPUT_WORDS = 4500
MAX_RETRIES = 4
@dataclass
class RewriteResult:
text: str
mode: str
chunks: int
api_calls: int
input_words: int
output_words: int
model: str
warnings: list[str]
def _env(name: str, default: str = "") -> str:
return (os.environ.get(name) or default).strip()
def get_client() -> OpenAI:
api_key = _env("OPENROUTER_API_KEY")
if not api_key:
raise RuntimeError(
"Missing OPENROUTER_API_KEY. Add it as a Space secret or local env var."
)
return OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=api_key,
default_headers={
"HTTP-Referer": _env("SPACE_HOST", "https://huggingface.co/spaces"),
"X-Title": _env("APP_TITLE", "Any2human"),
},
)
def default_model() -> str:
return _env("OPENROUTER_MODEL", "openrouter/free")
def _notify(cb: ProgressCallback, message: str) -> None:
if cb:
cb(message)
def _chat(
client: OpenAI,
*,
model: str,
system: str,
user: str,
temperature: float = 0.85,
presence_penalty: float = 0.35,
frequency_penalty: float = 0.35,
progress: ProgressCallback = None,
) -> str:
last_error: Exception | None = None
for attempt in range(1, MAX_RETRIES + 1):
try:
response = client.chat.completions.create(
model=model,
temperature=temperature,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
messages=[
{"role": "system", "content": system},
{"role": "user", "content": user},
],
)
content = (response.choices[0].message.content or "").strip()
if not content:
raise RuntimeError("Empty response from the model.")
return _strip_fences(content)
except Exception as exc: # noqa: BLE001
last_error = exc
msg = str(exc).lower()
retryable = any(
token in msg
for token in ("429", "rate", "timeout", "temporar", "503", "502", "overloaded")
)
if not retryable or attempt == MAX_RETRIES:
break
wait = min(2**attempt, 20)
_notify(progress, f"Rate limited / busy — retrying in {wait}s…")
time.sleep(wait)
raise RuntimeError(f"OpenRouter request failed: {last_error}")
def _strip_fences(text: str) -> str:
text = text.strip()
if text.startswith("```"):
text = re.sub(r"^```(?:\w+)?\s*", "", text)
text = re.sub(r"\s*```$", "", text)
return text.strip()
def _split_into_chunks(text: str, target: int = CHUNK_TARGET_WORDS) -> list[str]:
words = text.split()
if len(words) <= target:
return [text.strip()]
paragraphs = [p.strip() for p in re.split(r"\n\s*\n", text) if p.strip()]
if not paragraphs:
paragraphs = [text.strip()]
chunks: list[str] = []
current: list[str] = []
current_words = 0
for para in paragraphs:
p_words = len(para.split())
if current and current_words + p_words > target:
chunks.append("\n\n".join(current))
if current and len(current[-1].split()) <= CHUNK_OVERLAP_WORDS:
current = [current[-1], para]
current_words = len(current[-1].split()) + p_words
else:
current = [para]
current_words = p_words
else:
current.append(para)
current_words += p_words
if current:
chunks.append("\n\n".join(current))
return chunks
def _merge_chunks(parts: list[str]) -> str:
if len(parts) == 1:
return parts[0].strip()
merged: list[str] = []
for part in parts:
cleaned = part.strip()
if not cleaned:
continue
if merged:
prev_tail = " ".join(merged[-1].split()[-25:]).lower()
lead = " ".join(cleaned.split()[:25]).lower()
if lead and lead in prev_tail:
sentences = re.split(r"(?<=[.!?])\s+", cleaned)
cleaned = " ".join(sentences[1:]).strip() or cleaned
merged.append(cleaned)
return "\n\n".join(merged).strip()
def _maybe_compress(
client: OpenAI,
*,
model: str,
text: str,
max_words: int,
progress: ProgressCallback,
api_calls: int,
) -> tuple[str, int]:
if len(text.split()) <= int(max_words * 1.12):
return text, api_calls
_notify(progress, "Compressing to length budget…")
out = _chat(
client,
model=model,
system=prompts.system_compress(),
user=prompts.user_compress(text, max_words),
temperature=0.35,
presence_penalty=0.15,
frequency_penalty=0.15,
progress=progress,
)
return out, api_calls + 1
def _pipeline_chunk(
client: OpenAI,
*,
chunk: str,
chunk_index: int,
tone: str,
voice_sample: str,
quality: str,
preserve_length: bool,
model: str,
progress: ProgressCallback,
) -> tuple[str, int]:
"""New flow per chunk. Returns (text, api_calls_used)."""
calls = 0
label = f"section {chunk_index}"
chunk_words = len(chunk.split())
_, max_words, length_rule = prompts.length_budget(chunk_words, preserve_length)
seed = prompts.style_seed(chunk_index - 1)
if quality == "Fast":
_notify(progress, f"Fast rewrite {label}…")
out = _chat(
client,
model=model,
system=prompts.system_fast(tone),
user=prompts.user_fast(chunk, tone, seed, length_rule, voice_sample),
temperature=0.95,
presence_penalty=0.55,
frequency_penalty=0.5,
progress=progress,
)
calls += 1
out, calls = _maybe_compress(
client, model=model, text=out, max_words=max_words, progress=progress, api_calls=calls
)
return out, calls
# Shared: atomize (throws away AI sentence skeleton)
_notify(progress, f"Atomizing claims for {label}…")
atoms = _chat(
client,
model=model,
system=prompts.system_atomize(),
user=prompts.user_atomize(chunk),
temperature=0.15,
presence_penalty=0.0,
frequency_penalty=0.0,
progress=progress,
)
calls += 1
if quality == "Best":
# Interview → answers → weave (rebuilds discourse from scratch)
_notify(progress, f"Building questions for {label}…")
questions = _chat(
client,
model=model,
system=prompts.system_interview(),
user=prompts.user_interview(atoms),
temperature=0.5,
presence_penalty=0.2,
frequency_penalty=0.2,
progress=progress,
)
calls += 1
_notify(progress, f"Answering in human bursts ({label})…")
answers = _chat(
client,
model=model,
system=prompts.system_answer(tone),
user=prompts.user_answer(questions, atoms),
temperature=0.95,
presence_penalty=0.65,
frequency_penalty=0.55,
progress=progress,
)
calls += 1
material = answers
else:
# Balanced: weave directly from atoms
material = atoms
_notify(progress, f"Weaving prose for {label} (seed: {seed[:28]}…)…")
woven = _chat(
client,
model=model,
system=prompts.system_weave(tone),
user=prompts.user_weave(
material=material,
tone=tone,
seed=seed,
length_rule=length_rule,
voice_sample=voice_sample,
anti_source=chunk,
),
temperature=0.92,
presence_penalty=0.6,
frequency_penalty=0.5,
progress=progress,
)
calls += 1
if quality == "Best":
_notify(progress, f"Bridging seams for {label}…")
woven = _chat(
client,
model=model,
system=prompts.system_bridge(tone),
user=prompts.user_bridge(woven, max_words=max_words),
temperature=0.7,
presence_penalty=0.4,
frequency_penalty=0.35,
progress=progress,
)
calls += 1
woven, calls = _maybe_compress(
client,
model=model,
text=woven,
max_words=max_words,
progress=progress,
api_calls=calls,
)
return woven, calls
def rewrite_document(
text: str,
*,
tone: str = "Neutral",
voice_sample: str = "",
quality: str = "Best",
preserve_length: bool = True,
model: str | None = None,
progress: ProgressCallback = None,
) -> RewriteResult:
text = (text or "").strip()
if not text:
raise ValueError("Paste or upload some text first.")
words = len(text.split())
warnings: list[str] = []
if words > MAX_INPUT_WORDS:
raise ValueError(
f"Input is {words:,} words. Please keep under {MAX_INPUT_WORDS:,} words "
"on the free tier (split long documents)."
)
model = (model or default_model()).strip() or default_model()
client = get_client()
api_calls = 0
chunks = _split_into_chunks(text)
if quality == "Fast":
mode = "fast: meaning rewrite + mechanics"
elif quality == "Best":
mode = "best: atoms → interview → answers → weave → bridge + mechanics"
else:
mode = "balanced: atoms → weave → mechanics"
if len(chunks) > 1:
warnings.append(f"Split into {len(chunks)} sections; each uses its own style seed.")
outputs: list[str] = []
for i, chunk in enumerate(chunks, start=1):
out, used = _pipeline_chunk(
client,
chunk=chunk,
chunk_index=i,
tone=tone,
voice_sample=voice_sample,
quality=quality,
preserve_length=preserve_length,
model=model,
progress=progress,
)
api_calls += used
outputs.append(out)
final = _merge_chunks(outputs)
_, global_max, _ = prompts.length_budget(words, preserve_length)
final, api_calls = _maybe_compress(
client,
model=model,
text=final,
max_words=global_max,
progress=progress,
api_calls=api_calls,
)
if len(final.split()) > int(global_max * 1.05):
warnings.append(f"Trimmed toward ~{global_max} words length budget.")
_notify(progress, "Applying mechanical human rhythm…")
final = mechanical_humanize(final, tone=tone)
_notify(progress, "Done.")
return RewriteResult(
text=final,
mode=mode,
chunks=len(chunks),
api_calls=api_calls,
input_words=words,
output_words=len(final.split()),
model=model,
warnings=warnings,
)
|