-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassemble_message.py
More file actions
429 lines (352 loc) · 11.5 KB
/
Copy pathassemble_message.py
File metadata and controls
429 lines (352 loc) · 11.5 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python3
"""
Hybrid Audio Assembly — Sonic-3 Edition
v5.1 NDF — Sonic-3 Contract + Cache + Rotation Final Alignment
This version:
• Fully matches Cartesia’s 2025 requirements:
- transcript (string)
- voice.mode="id", voice.id=<VOICE_ID>
- output_format.container="wav"
- output_format.encoding="pcm_s16le"
"""
import json
import time
import datetime
import requests
from pathlib import Path
from typing import Any, Dict, List, Tuple, Optional
from config import build_sonic3_payload
from config import (
STEMS_DIR,
OUTPUT_DIR,
CARTESIA_API_URL,
CARTESIA_API_KEY,
CARTESIA_VERSION,
MODEL_ID,
VOICE_ID,
CROSSFADE_MS,
DEBUG,
ENABLE_SEMANTIC_TIMING,
SAMPLE_RATE,
get_template_path,
)
from cache_manager import register_stem, get_cached_stem
from bitmerge_semantic import assemble_with_timing_map_bitmerge
from audio_utils import assemble_clean_merge
# Optional GCS
try:
from gcloud_storage import upload_to_gcs
from config import is_gcs_enabled, GCS_FOLDER_OUTPUTS
except Exception:
upload_to_gcs = None
def is_gcs_enabled() -> bool:
return False
GCS_FOLDER_OUTPUTS = "outputs"
# Rotational hooks
try:
from rotational_engine import pre_tts_hook, post_tts_hook
except Exception:
def pre_tts_hook(text, stem_name, **_):
return text, stem_name
def post_tts_hook(*_, **__):
return None
# ============================================================
# Timestamp helpers
# ============================================================
def ts() -> str:
return datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
def ts_compact() -> str:
return datetime.datetime.utcnow().strftime("%Y%m%d_%H%M%S")
# ============================================================
# Template loading
# ============================================================
def load_template(template_name: Optional[str]) -> Dict[str, Any]:
try:
tpl_path = get_template_path(template_name)
if not tpl_path.exists():
raise RuntimeError(f"Template not found: {tpl_path}")
with open(tpl_path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
print(f"[{ts()}] ⚠️ Failed to load template: {e}")
return {"segments": [], "timing_map": [], "voice_config": {}}
# ============================================================
# Template segment rendering
# ============================================================
def build_segments_from_template(
template: Dict[str, Any],
name: str,
developer: str,
) -> List[Tuple[str, str]]:
out: List[Tuple[str, str]] = []
for seg in template.get("segments", []):
seg_id = seg.get("id", "")
txt = (
seg.get("text", "")
.replace("{name}", name)
.replace("{developer}", developer)
)
out.append((seg_id, txt))
return out
# ============================================================
# Convert stem labels back into natural human language
# ============================================================
def _clean_text_from_stem(stem: str) -> str:
cleaned = (
stem.replace("stem_name_", "")
.replace("stem_brand_", "")
.replace("stem.name.", "")
.replace("stem.developer.", "")
.replace("_", " ")
.strip()
.title()
)
return cleaned
# ============================================================
# ⚡ Sonic-3 TTS Generator
# ============================================================
def cartesia_generate(
text: str,
stem_name: str,
voice_id: str = VOICE_ID,
template: Optional[Dict[str, Any]] = None,
) -> str:
raw = text.strip()
if raw.lower() == stem_name.lower():
raw = _clean_text_from_stem(stem_name)
processed_text, _ = pre_tts_hook(raw, stem_name, voice_id=voice_id)
true_text = processed_text
cached = get_cached_stem(stem_name)
if cached:
if DEBUG:
print(f"[{ts()}] 🔁 Cache hit → {stem_name}")
return cached
print(f"[{ts()}] 🎤 Generating new stem → {stem_name}")
STEMS_DIR.mkdir(exist_ok=True)
# ============================================================
# ⭐ NDF-SAFE PATCH (ÚNICO CAMBIO PERMITIDO)
# Sustituye el target plano por el target estructurado
# ============================================================
try:
from naming_contract import infer_stem_category, build_stem_path
category = infer_stem_category(stem_name)
structured_path = build_stem_path(category, stem_name)
structured_path.parent.mkdir(parents=True, exist_ok=True)
except Exception:
structured_path = None
# Target original (NO eliminado)
out_path = Path(STEMS_DIR) / f"{stem_name}.wav"
# NDF: prefer structured folder if available
if structured_path:
out_path = structured_path
# ============================================================
vc = (template or {}).get("voice_config", {})
speed = float(vc.get("speed", 1.0))
volume = float(vc.get("volume", 1.0))
payload = {
"transcript": true_text,
"voice": {
"mode": "id",
"id": voice_id,
},
"generation_config": {
"speed": speed,
"volume": volume,
},
"output_format": {
"container": "wav",
"encoding": "pcm_s16le",
"sample_rate": SAMPLE_RATE,
},
"model_id": MODEL_ID,
}
headers = {
"Content-Type": "application/json",
"X-API-Key": CARTESIA_API_KEY,
"Cartesia-Version": CARTESIA_VERSION,
}
try:
r = requests.post(
CARTESIA_API_URL,
json=payload,
headers=headers,
timeout=60,
)
r.raise_for_status()
with open(out_path, "wb") as f:
f.write(r.content)
register_stem(
name=stem_name,
text=true_text,
path=str(out_path),
voice_id=voice_id,
)
post_tts_hook(
stem_name,
true_text,
str(out_path),
voice_id=voice_id,
template=template,
)
return str(out_path)
except Exception as e:
print(f"[{ts()}] ❌ Sonic-3 failure for stem={stem_name}: {e}")
try:
print(json.dumps(payload, indent=2))
except Exception:
pass
raise
# ============================================================
# Output basename
# ============================================================
def build_output_basename(
name: str,
developer: str,
mode: str = "semantic",
) -> str:
n = name.strip().replace(" ", "_")
d = developer.strip().replace(" ", "_")
return f"{n}_{d}_{ts_compact()}_{mode}"
# ============================================================
# Semantic timing wrapper
# ============================================================
def assemble_with_timing_map_ndf(
stems: List[str],
timing_map: Any,
basename: str,
) -> str:
out_path = Path(OUTPUT_DIR) / f"{basename}.wav"
if isinstance(timing_map, dict):
timing_map = [
{
"from": k[0],
"to": k[1],
**v,
}
for k, v in timing_map.items()
]
return assemble_with_timing_map_bitmerge(
stems,
timing_map,
str(out_path),
)
# ============================================================
# Full E2E Pipeline
# ============================================================
def assemble_pipeline(
name: str,
developer: str,
clean_merge: bool = True,
template_name: Optional[str] = None,
) -> str:
print(f"\n[{ts()}] 🚀 Assembling message for {name}/{developer}")
template = load_template(template_name)
segments = build_segments_from_template(template, name, developer)
# fallback if template empty
if not segments:
print(f"[{ts()}] ⚠️ Missing template → using fallback segments")
segments = [
("static_hey", "Hey"),
(f"name_{name}", name),
("static_info", "it's Luis about your"),
(f"dev_{developer}", developer),
("static_end", "Thank you."),
]
stems: List[str] = []
for seg_id, text in segments:
cached = get_cached_stem(seg_id)
if cached:
stems.append(cached)
continue
stems.append(
cartesia_generate(
text=text,
stem_name=seg_id,
voice_id=VOICE_ID,
template=template,
)
)
mode = "semantic" if ENABLE_SEMANTIC_TIMING else "clean"
basename = build_output_basename(name, developer, mode)
if ENABLE_SEMANTIC_TIMING:
return assemble_with_timing_map_ndf(
stems,
template.get("timing_map", []),
basename,
)
out = Path(OUTPUT_DIR) / f"{basename}.wav"
return assemble_clean_merge(stems, out, crossfade_ms=CROSSFADE_MS)
# ============================================================
# Pipeline + GCS upload
# ============================================================
def assemble_pipeline_with_upload(
name: str,
developer: str,
template_name: Optional[str] = None,
upload: bool = True,
clean_merge: bool = True,
) -> Dict[str, Any]:
start = time.time()
out_path = assemble_pipeline(
name=name,
developer=developer,
clean_merge=clean_merge,
template_name=template_name,
)
file_path = Path(out_path)
if upload and is_gcs_enabled() and upload_to_gcs:
upload_meta = upload_to_gcs(str(file_path), folder=GCS_FOLDER_OUTPUTS)
else:
upload_meta = {
"ok": False,
"mode": "local-only",
"file_path": str(file_path),
}
return {
"status": "ok",
"output_file": str(file_path),
"file_url": upload_meta.get("file_url"),
"upload": upload_meta,
"duration_sec": round(time.time() - start, 3),
"timestamp": ts(),
"name": name,
"developer": developer,
}
# ============================================================
# Unified safe wrapper
# ============================================================
def assemble_pipeline_unified(
name: str,
developer: str,
template_name: Optional[str] = None,
upload: bool = True,
clean_merge: bool = True,
) -> Dict[str, Any]:
try:
return assemble_pipeline_with_upload(
name=name,
developer=developer,
template_name=template_name,
upload=upload,
clean_merge=clean_merge,
)
except Exception as e:
return {
"status": "error",
"error": str(e),
"output_file": None,
"file_url": None,
"upload": {},
"timestamp": ts(),
"name": name,
"developer": developer,
}
# ============================================================
# Diagnostic mode
# ============================================================
if __name__ == "__main__":
print("🔧 assemble_message.py — Sonic-3 Diagnostic")
print(" • OUTPUT_DIR:", OUTPUT_DIR)
print(" • STEMS_DIR :", STEMS_DIR)
print(" • CARTESIA_API_URL:", CARTESIA_API_URL)
print(" • SAMPLE_RATE:", SAMPLE_RATE)