-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_generate_stems.py
More file actions
279 lines (227 loc) · 8.96 KB
/
batch_generate_stems.py
File metadata and controls
279 lines (227 loc) · 8.96 KB
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
#!/usr/bin/env python3
"""
Batch Stem Generator — pre-generates stems from lists or template contracts.
v5.1 NDF — Sonic-3 Contract + Canonical Labels
Changes vs v4.2:
• Uses canonical labels:
stem.name.<name_slug>
stem.developer.<dev_slug>
instead of stem_name_* / stem_brand_*.
• Rotational batch generation now populates the same cache keys used
by routes/generate.py and routes/rotation.py.
• Still never sends stem_id as text (uses _clean_text_from_stem for legacy).
• Respects cartesia_generate() Sonic-3 contract and cache_manager v5.0.
• Rotational entries marked with rotational=True + dataset_origin.
"""
import sys
import json
import time
import datetime
import concurrent.futures
from pathlib import Path
from typing import Iterable, Dict, Any, List, Optional, Tuple
from assemble_message import (
cartesia_generate,
load_template,
build_segments_from_template,
_clean_text_from_stem,
)
from config import (
DEBUG,
MODEL_ID,
VOICE_ID,
BASE_DIR,
OUTPUT_DIR,
CARTESIA_API_URL,
stem_label_name,
stem_label_developer,
)
# -------------------------------------------------
# Cache / rotational metadata
# -------------------------------------------------
try:
from cache_manager import (
find_or_generate_stem,
register_rotational_stem,
stem_key,
)
CACHE_OK = True
except Exception:
CACHE_OK = False
def find_or_generate_stem(text, voice_id=VOICE_ID, model_id=MODEL_ID, template=None):
stem = f"stem_generic_{abs(hash((text, voice_id, model_id))) % (10**10)}"
return cartesia_generate(text, stem, voice_id=voice_id, template=template)
def register_rotational_stem(*a, **k):
return None
def stem_key(text, voice_id=VOICE_ID, model_id=MODEL_ID):
return f"stem_generic_{abs(hash((text, voice_id, model_id))) % (10**10)}"
# -------------------------------------------------
# Helpers
# -------------------------------------------------
def _slugify(text: str) -> str:
return (
text.strip()
.lower()
.replace(" ", "_")
.replace("/", "_")
.replace("\\", "_")
)
def _ts_compact() -> str:
return datetime.datetime.now(datetime.UTC).strftime("%Y%m%d_%H%M%S")
def _make_label(prefix: str, item: str) -> str:
"""
v5.1 — Canonical label resolver:
• prefix in {"stem_name", "name"} → stem.name.<slug>
• prefix in {"stem_brand","developer"} → stem.developer.<slug>
• NEW v5.3 → {"script", "stem_script", "generic", "stem_generic"}
→ stem.script.<slug>
• else → <prefix>.<slug>
"""
if prefix in ("stem_name", "name"):
return stem_label_name(item)
if prefix in ("stem_brand", "developer", "dev"):
return stem_label_developer(item)
# NDF-030 — NEW script stem category (additive, never destructive)
if prefix in ("script", "stem_script", "generic", "stem_generic"):
return f"stem.script.{_slugify(item)}"
return f"{prefix}.{_slugify(item)}"
# -------------------------------------------------
# CORE GENERATION (v5.1)
# -------------------------------------------------
def generate_from_list(
items: Iterable[str],
prefix: str,
voice_overrides: Dict[str, Any] = None,
max_workers: int = 4,
retries: int = 2,
use_cache_key: bool = False,
rotational: bool = False,
dataset_origin: Optional[str] = None,
) -> None:
"""
Batch generator for arbitrary lists.
Notes:
• v5.1 rotational mode ignores use_cache_key and always writes canonical
labels (stem.name.* / stem.developer.*) so routes/rotation + generate
can reuse stems from the cache.
"""
raw_items = [i.strip() for i in items if i and i.strip()]
total = len(raw_items)
if not total:
print("⚠️ Empty dataset for generate_from_list.")
return
print(f"🚀 Batch prefix '{prefix}' — {total} stems")
print(f"API={'sonic-3' if 'tts/bytes' in CARTESIA_API_URL else 'legacy'}")
print(f"voice={VOICE_ID} | model={MODEL_ID}")
def worker(item: str):
# v5.1: canonical label (no more stem_name_* / stem_brand_*)
stem_name = _make_label(prefix, item)
# Legacy safety: if item is itself a stem id, reconstruct natural text
if item.lower().startswith("stem_"):
safe_text = _clean_text_from_stem(item)
else:
safe_text = item.strip()
attempt = 0
template = None # no template for bulk (voice_config handled by cartesia_generate if needed)
while attempt <= retries:
try:
# v5.1: In rotational mode we avoid cache_key indirection and
# always generate under the canonical label used by routes.
if use_cache_key and CACHE_OK and not rotational:
key = stem_key(item, VOICE_ID, MODEL_ID)
path = find_or_generate_stem(
safe_text,
voice_id=VOICE_ID,
model_id=MODEL_ID,
template=template,
)
# For non-rotational cache_key usage we don't override label.
return item, path, attempt, stem_name
# Normal Sonic-3 generation under canonical label
path = cartesia_generate(
safe_text,
stem_name,
voice_id=VOICE_ID,
template=template,
)
# v5.1: mark as rotational in cache when requested
if rotational and CACHE_OK:
register_rotational_stem(
name=stem_name,
text=safe_text,
path=path,
dataset_origin=dataset_origin or f"rotations/{prefix}",
voice_id=VOICE_ID,
model_id=MODEL_ID,
)
return item, path, attempt, stem_name
except Exception as e:
attempt += 1
if attempt > retries:
print(f"❌ {stem_name} failed → {e}")
return item, None, attempt, stem_name
print(f"⚠️ Retry {attempt}/{retries} — {stem_name}")
time.sleep(1)
completed = 0
t0 = time.time()
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as exe:
futures = {exe.submit(worker, item): item for item in raw_items}
for fut in concurrent.futures.as_completed(futures):
item = futures[fut]
_, path, attempt, label = fut.result()
completed += 1
if path and DEBUG:
print(f" ✔ {label} (try {attempt+1})")
print(f"🎯 Batch complete: {completed}/{total}")
print(f"⏳ Time: {round(time.time() - t0, 2)}s\n")
# -------------------------------------------------
# TEMPLATE MODE
# -------------------------------------------------
def generate_from_template(template_path: str, first_name="John", developer="Hilton", max_workers=4):
tpl = load_template(template_path)
segments = build_segments_from_template(tpl, first_name, developer)
texts = [t for _, t in segments]
print(f"📜 Template: {Path(template_path).name} | segments={len(texts)}")
generate_from_list(
texts,
prefix="tpl",
voice_overrides=tpl.get("voice_config", {}),
max_workers=max_workers,
)
# -------------------------------------------------
# ROTATIONAL MODE (v5.1)
# -------------------------------------------------
def generate_rotational_stems(names_path: Path, devs_path: Path, max_workers=6):
"""
v5.1 rotational batch generator.
Ensures:
• Names stored as stem.name.<slug>
• Developers stored as stem.developer.<slug>
• Cache entries are compatible with rotation/generate routes.
"""
print("\n🔁 Rotational Mode")
from rotational_engine import verify_dataset_integrity, summarize_rotational_cache
verify_dataset_integrity(names_path, devs_path)
names = json.loads(names_path.read_text()).get("items", [])
devs = json.loads(devs_path.read_text()).get("items", [])
print(f"Names={len(names)} | Developers={len(devs)}")
# Names → stem.name.*
generate_from_list(
names,
prefix="stem_name",
use_cache_key=False, # v5.1: explicit canonical labels
rotational=True,
dataset_origin="rotations/names",
max_workers=max_workers,
)
# Developers → stem.developer.*
generate_from_list(
devs,
prefix="stem_brand", # legacy prefix, label resolver maps to stem.developer.*
use_cache_key=False,
rotational=True,
dataset_origin="rotations/developers",
max_workers=max_workers,
)
summarize_rotational_cache(names, devs)
print("✅ Rotational stems complete.\n")