Update api_client.py
Browse files- api_client.py +332 -303
api_client.py
CHANGED
|
@@ -1,303 +1,332 @@
|
|
| 1 |
-
# api_client.py (UI - Space "veureu")
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import
|
| 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 |
-
if
|
| 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 |
-
except requests.exceptions.RequestException as e:
|
| 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 |
-
return {"
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# api_client.py (UI - Space "veureu")
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
import zipfile
|
| 7 |
+
import requests
|
| 8 |
+
from typing import Iterable, Dict, Any
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class APIClient:
|
| 12 |
+
"""
|
| 13 |
+
High-level client for communicating with the Veureu Engine API.
|
| 14 |
+
|
| 15 |
+
Endpoints managed:
|
| 16 |
+
POST /jobs
|
| 17 |
+
→ {"job_id": "..."}
|
| 18 |
+
|
| 19 |
+
GET /jobs/{job_id}/status
|
| 20 |
+
→ {"status": "queued|processing|done|failed", ...}
|
| 21 |
+
|
| 22 |
+
GET /jobs/{job_id}/result
|
| 23 |
+
→ JobResult such as {"book": {...}, "une": {...}, ...}
|
| 24 |
+
|
| 25 |
+
This class is used by the Streamlit UI to submit videos, poll job status,
|
| 26 |
+
retrieve results, generate audio, and interact with the TTS and casting services.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
def __init__(
|
| 30 |
+
self,
|
| 31 |
+
base_url: str,
|
| 32 |
+
use_mock: bool = False,
|
| 33 |
+
data_dir: str | None = None,
|
| 34 |
+
token: str | None = None,
|
| 35 |
+
timeout: int = 180
|
| 36 |
+
):
|
| 37 |
+
"""
|
| 38 |
+
Initialize the API client.
|
| 39 |
+
|
| 40 |
+
Args:
|
| 41 |
+
base_url: Base URL of the engine or TTS service.
|
| 42 |
+
use_mock: Whether to respond with mock data instead of real API calls.
|
| 43 |
+
data_dir: Optional data folder for local mock/test files.
|
| 44 |
+
token: Authentication token (fallback: API_SHARED_TOKEN env var).
|
| 45 |
+
timeout: Timeout in seconds for requests.
|
| 46 |
+
"""
|
| 47 |
+
self.base_url = base_url.rstrip("/")
|
| 48 |
+
self.tts_url = self.base_url # For HF Spaces, TTS lives at same base URL
|
| 49 |
+
self.use_mock = use_mock
|
| 50 |
+
self.data_dir = data_dir
|
| 51 |
+
self.timeout = timeout
|
| 52 |
+
self.session = requests.Session()
|
| 53 |
+
|
| 54 |
+
# Authorization header if token provided
|
| 55 |
+
token = token or os.getenv("API_SHARED_TOKEN")
|
| 56 |
+
if token:
|
| 57 |
+
self.session.headers.update({"Authorization": f"Bearer {token}"})
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# -------------------------------------------------------------------------
|
| 61 |
+
# Internal engine calls
|
| 62 |
+
# -------------------------------------------------------------------------
|
| 63 |
+
|
| 64 |
+
def _post_jobs(self, video_path: str, modes: Iterable[str]) -> Dict[str, Any]:
|
| 65 |
+
"""Submit a video and processing modes to /jobs."""
|
| 66 |
+
url = f"{self.base_url}/jobs"
|
| 67 |
+
files = {
|
| 68 |
+
"file": (os.path.basename(video_path), open(video_path, "rb"), "application/octet-stream")
|
| 69 |
+
}
|
| 70 |
+
data = {"modes": ",".join(modes)}
|
| 71 |
+
|
| 72 |
+
r = self.session.post(url, files=files, data=data, timeout=self.timeout)
|
| 73 |
+
r.raise_for_status()
|
| 74 |
+
return r.json()
|
| 75 |
+
|
| 76 |
+
def _get_status(self, job_id: str) -> Dict[str, Any]:
|
| 77 |
+
"""Query job status."""
|
| 78 |
+
url = f"{self.base_url}/jobs/{job_id}/status"
|
| 79 |
+
r = self.session.get(url, timeout=self.timeout)
|
| 80 |
+
r.raise_for_status()
|
| 81 |
+
return r.json()
|
| 82 |
+
|
| 83 |
+
def _get_result(self, job_id: str) -> Dict[str, Any]:
|
| 84 |
+
"""Retrieve job result."""
|
| 85 |
+
url = f"{self.base_url}/jobs/{job_id}/result"
|
| 86 |
+
r = self.session.get(url, timeout=self.timeout)
|
| 87 |
+
r.raise_for_status()
|
| 88 |
+
return r.json()
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# -------------------------------------------------------------------------
|
| 92 |
+
# Public API used by streamlit_app.py
|
| 93 |
+
# -------------------------------------------------------------------------
|
| 94 |
+
|
| 95 |
+
def process_video(self, video_path: str, modes: Iterable[str]) -> Dict[str, Any]:
|
| 96 |
+
"""Return {"job_id": "..."} either from mock or engine."""
|
| 97 |
+
if self.use_mock:
|
| 98 |
+
return {"job_id": "mock-123"}
|
| 99 |
+
return self._post_jobs(video_path, modes)
|
| 100 |
+
|
| 101 |
+
def get_job(self, job_id: str) -> Dict[str, Any]:
|
| 102 |
+
"""
|
| 103 |
+
Returns UI-friendly job data:
|
| 104 |
+
{"status": "done", "results": {"book": {...}, "une": {...}}}
|
| 105 |
+
|
| 106 |
+
Maps engine responses into the expected 'results' format.
|
| 107 |
+
"""
|
| 108 |
+
if self.use_mock:
|
| 109 |
+
return {
|
| 110 |
+
"status": "done",
|
| 111 |
+
"results": {
|
| 112 |
+
"book": {"text": "Example text (book)", "mp3_bytes": b""},
|
| 113 |
+
"une": {
|
| 114 |
+
"srt": "1\n00:00:00,000 --> 00:00:01,000\nExample UNE\n",
|
| 115 |
+
"mp3_bytes": b""
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
status_data = self._get_status(job_id)
|
| 121 |
+
|
| 122 |
+
# If still processing, return minimal structure
|
| 123 |
+
if status_data.get("status") in {"queued", "processing"}:
|
| 124 |
+
return {"status": status_data.get("status", "queued")}
|
| 125 |
+
|
| 126 |
+
raw_result = self._get_result(job_id)
|
| 127 |
+
results = {}
|
| 128 |
+
|
| 129 |
+
# Direct mapping of book/une sections
|
| 130 |
+
if "book" in raw_result:
|
| 131 |
+
results["book"] = {"text": raw_result["book"].get("text")}
|
| 132 |
+
if "une" in raw_result:
|
| 133 |
+
results["une"] = {"srt": raw_result["une"].get("srt")}
|
| 134 |
+
|
| 135 |
+
# Preserve characters/metrics if present
|
| 136 |
+
for section in ("book", "une"):
|
| 137 |
+
if section in raw_result:
|
| 138 |
+
if "characters" in raw_result[section]:
|
| 139 |
+
results[section]["characters"] = raw_result[section]["characters"]
|
| 140 |
+
if "metrics" in raw_result[section]:
|
| 141 |
+
results[section]["metrics"] = raw_result[section]["metrics"]
|
| 142 |
+
|
| 143 |
+
final_status = "done" if results else status_data.get("status", "unknown")
|
| 144 |
+
return {"status": final_status, "results": results}
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# -------------------------------------------------------------------------
|
| 148 |
+
# TTS Services
|
| 149 |
+
# -------------------------------------------------------------------------
|
| 150 |
+
|
| 151 |
+
def tts_matxa(self, text: str, voice: str = "central/grau") -> dict:
|
| 152 |
+
"""
|
| 153 |
+
Call the TTS /tts/text endpoint to synthesize short audio.
|
| 154 |
+
|
| 155 |
+
Returns:
|
| 156 |
+
{"mp3_bytes": b"..."} on success
|
| 157 |
+
{"error": "..."} on failure
|
| 158 |
+
"""
|
| 159 |
+
if not self.tts_url:
|
| 160 |
+
raise ValueError("TTS service URL not configured.")
|
| 161 |
+
|
| 162 |
+
url = f"{self.tts_url.rstrip('/')}/tts/text"
|
| 163 |
+
data = {"texto": text, "voice": voice, "formato": "mp3"}
|
| 164 |
+
|
| 165 |
+
try:
|
| 166 |
+
r = requests.post(url, data=data, timeout=self.timeout)
|
| 167 |
+
r.raise_for_status()
|
| 168 |
+
return {"mp3_bytes": r.content}
|
| 169 |
+
except requests.exceptions.RequestException as e:
|
| 170 |
+
return {"error": str(e)}
|
| 171 |
+
|
| 172 |
+
def rebuild_video_with_ad(self, video_path: str, srt_path: str) -> dict:
|
| 173 |
+
"""
|
| 174 |
+
Rebuild a video including audio description (AD)
|
| 175 |
+
by calling /tts/srt. The server returns a ZIP containing an MP4.
|
| 176 |
+
"""
|
| 177 |
+
if not self.tts_url:
|
| 178 |
+
raise ValueError("TTS service URL not configured.")
|
| 179 |
+
|
| 180 |
+
url = f"{self.tts_url.rstrip('/')}/tts/srt"
|
| 181 |
+
|
| 182 |
+
try:
|
| 183 |
+
files = {
|
| 184 |
+
"video": (os.path.basename(video_path), open(video_path, "rb"), "video/mp4"),
|
| 185 |
+
"srt": (os.path.basename(srt_path), open(srt_path, "rb"), "application/x-subrip")
|
| 186 |
+
}
|
| 187 |
+
data = {"include_final_mp4": 1}
|
| 188 |
+
|
| 189 |
+
r = requests.post(url, files=files, data=data, timeout=self.timeout * 5)
|
| 190 |
+
r.raise_for_status()
|
| 191 |
+
|
| 192 |
+
with zipfile.ZipFile(io.BytesIO(r.content)) as z:
|
| 193 |
+
for name in z.namelist():
|
| 194 |
+
if name.endswith(".mp4"):
|
| 195 |
+
return {"video_bytes": z.read(name)}
|
| 196 |
+
|
| 197 |
+
return {"error": "MP4 file not found inside ZIP."}
|
| 198 |
+
|
| 199 |
+
except zipfile.BadZipFile:
|
| 200 |
+
return {"error": "Invalid ZIP response from server."}
|
| 201 |
+
except requests.exceptions.RequestException as e:
|
| 202 |
+
return {"error": str(e)}
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
# -------------------------------------------------------------------------
|
| 206 |
+
# Engine casting services
|
| 207 |
+
# -------------------------------------------------------------------------
|
| 208 |
+
|
| 209 |
+
def create_initial_casting(
|
| 210 |
+
self,
|
| 211 |
+
video_path: str = None,
|
| 212 |
+
video_bytes: bytes = None,
|
| 213 |
+
video_name: str = None,
|
| 214 |
+
epsilon: float = 0.5,
|
| 215 |
+
min_cluster_size: int = 2
|
| 216 |
+
) -> dict:
|
| 217 |
+
"""
|
| 218 |
+
Calls /create_initial_casting to produce the initial actor/face clustering.
|
| 219 |
+
|
| 220 |
+
Args:
|
| 221 |
+
video_path: Load video from disk.
|
| 222 |
+
video_bytes: Provide video already in memory.
|
| 223 |
+
video_name: Name used if video_bytes is provided.
|
| 224 |
+
epsilon: DBSCAN epsilon for clustering.
|
| 225 |
+
min_cluster_size: Minimum number of samples for DBSCAN.
|
| 226 |
+
"""
|
| 227 |
+
url = f"{self.base_url}/create_initial_casting"
|
| 228 |
+
|
| 229 |
+
try:
|
| 230 |
+
# Prepare video input
|
| 231 |
+
if video_bytes:
|
| 232 |
+
files = {"video": (video_name or "video.mp4", video_bytes, "video/mp4")}
|
| 233 |
+
elif video_path:
|
| 234 |
+
with open(video_path, "rb") as f:
|
| 235 |
+
files = {"video": (os.path.basename(video_path), f.read(), "video/mp4")}
|
| 236 |
+
else:
|
| 237 |
+
return {"error": "Either video_path or video_bytes must be provided."}
|
| 238 |
+
|
| 239 |
+
data = {
|
| 240 |
+
"epsilon": str(epsilon),
|
| 241 |
+
"min_cluster_size": str(min_cluster_size)
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
r = self.session.post(url, files=files, data=data, timeout=self.timeout * 5)
|
| 245 |
+
r.raise_for_status()
|
| 246 |
+
|
| 247 |
+
if r.headers.get("content-type", "").startswith("application/json"):
|
| 248 |
+
return r.json()
|
| 249 |
+
|
| 250 |
+
return {"ok": True}
|
| 251 |
+
|
| 252 |
+
except Exception as e:
|
| 253 |
+
return {"error": str(e)}
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
# -------------------------------------------------------------------------
|
| 257 |
+
# Long text TTS helpers
|
| 258 |
+
# -------------------------------------------------------------------------
|
| 259 |
+
|
| 260 |
+
def generate_audio_from_text_file(self, text_content: str, voice: str = "central/grau") -> dict:
|
| 261 |
+
"""
|
| 262 |
+
Converts a large text into an SRT-like structure, calls /tts/srt,
|
| 263 |
+
and extracts 'ad_master.mp3' from the resulting ZIP.
|
| 264 |
+
|
| 265 |
+
Useful for audiobook-like generation.
|
| 266 |
+
"""
|
| 267 |
+
if not self.tts_url:
|
| 268 |
+
raise ValueError("TTS service URL not configured.")
|
| 269 |
+
|
| 270 |
+
# Build synthetic SRT in memory
|
| 271 |
+
srt_content = ""
|
| 272 |
+
start = 0
|
| 273 |
+
|
| 274 |
+
for idx, raw_line in enumerate(text_content.strip().split("\n")):
|
| 275 |
+
line = raw_line.strip()
|
| 276 |
+
if not line:
|
| 277 |
+
continue
|
| 278 |
+
|
| 279 |
+
end = start + 5 # simplistic 5 seconds per subtitle
|
| 280 |
+
|
| 281 |
+
def fmt(seconds):
|
| 282 |
+
h = seconds // 3600
|
| 283 |
+
m = (seconds % 3600) // 60
|
| 284 |
+
s = seconds % 60
|
| 285 |
+
return f"{h:02d}:{m:02d}:{s:02d},000"
|
| 286 |
+
|
| 287 |
+
srt_content += f"{idx+1}\n"
|
| 288 |
+
srt_content += f"{fmt(start)} --> {fmt(end)}\n"
|
| 289 |
+
srt_content += f"{line}\n\n"
|
| 290 |
+
start = end
|
| 291 |
+
|
| 292 |
+
if not srt_content:
|
| 293 |
+
return {"error": "Provided text is empty or cannot be processed."}
|
| 294 |
+
|
| 295 |
+
# Call server
|
| 296 |
+
url = f"{self.tts_url.rstrip('/')}/tts/srt"
|
| 297 |
+
|
| 298 |
+
try:
|
| 299 |
+
files = {"srt": ("fake_ad.srt", srt_content, "application/x-subrip")}
|
| 300 |
+
data = {"voice": voice, "ad_format": "mp3"}
|
| 301 |
+
|
| 302 |
+
r = requests.post(url, files=files, data=data, timeout=self.timeout * 5)
|
| 303 |
+
r.raise_for_status()
|
| 304 |
+
|
| 305 |
+
with zipfile.ZipFile(io.BytesIO(r.content)) as z:
|
| 306 |
+
if "ad_master.mp3" in z.namelist():
|
| 307 |
+
return {"mp3_bytes": z.read("ad_master.mp3")}
|
| 308 |
+
|
| 309 |
+
return {"error": "'ad_master.mp3' not found inside ZIP."}
|
| 310 |
+
|
| 311 |
+
except requests.exceptions.RequestException as e:
|
| 312 |
+
return {"error": f"Error calling SRT API: {e}"}
|
| 313 |
+
except zipfile.BadZipFile:
|
| 314 |
+
return {"error": "Invalid ZIP response from server."}
|
| 315 |
+
|
| 316 |
+
def tts_long_text(self, text: str, voice: str = "central/grau") -> dict:
|
| 317 |
+
"""
|
| 318 |
+
Call /tts/text_long for very long text TTS synthesis.
|
| 319 |
+
Returns raw MP3 bytes.
|
| 320 |
+
"""
|
| 321 |
+
if not self.tts_url:
|
| 322 |
+
raise ValueError("TTS service URL not configured.")
|
| 323 |
+
|
| 324 |
+
url = f"{self.tts_url.rstrip('/')}/tts/text_long"
|
| 325 |
+
data = {"texto": text, "voice": voice, "formato": "mp3"}
|
| 326 |
+
|
| 327 |
+
try:
|
| 328 |
+
r = requests.post(url, data=data, timeout=self.timeout * 10)
|
| 329 |
+
r.raise_for_status()
|
| 330 |
+
return {"mp3_bytes": r.content}
|
| 331 |
+
except requests.exceptions.RequestException as e:
|
| 332 |
+
return {"error": str(e)}
|