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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ai_task_manager.py
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
import hashlib
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
import shutil
|
| 8 |
+
import subprocess
|
| 9 |
+
import tempfile
|
| 10 |
+
import threading
|
| 11 |
+
import uuid
|
| 12 |
+
from dataclasses import dataclass, field
|
| 13 |
+
from datetime import datetime, timezone
|
| 14 |
+
from enum import Enum
|
| 15 |
+
from typing import Any, Callable, Dict, List, Optional, Tuple
|
| 16 |
+
|
| 17 |
+
from pydub import AudioSegment
|
| 18 |
+
|
| 19 |
+
from language_router import LanguageRouter, RoutedSegment
|
| 20 |
+
from storage_manager import CONVERSION_MANIFEST_SCHEMA, StorageManager
|
| 21 |
+
from report_manager import ReportManager
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
VERSION = "ai_task_manager_v9_bark_execution"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class TaskStatus(str, Enum):
|
| 28 |
+
CREATED = "CREATED"
|
| 29 |
+
READING = "READING"
|
| 30 |
+
ANALYZING = "ANALYZING"
|
| 31 |
+
SEGMENTING = "SEGMENTING"
|
| 32 |
+
SYNTHESIZING = "SYNTHESIZING"
|
| 33 |
+
VALIDATING = "VALIDATING"
|
| 34 |
+
MERGING = "MERGING"
|
| 35 |
+
SAVING = "SAVING"
|
| 36 |
+
DONE = "DONE"
|
| 37 |
+
FAILED = "FAILED"
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@dataclass
|
| 41 |
+
class VoiceTask:
|
| 42 |
+
text: str
|
| 43 |
+
source_type: str
|
| 44 |
+
engine: str
|
| 45 |
+
voice_name: str = ""
|
| 46 |
+
speaker_name: str = ""
|
| 47 |
+
repo_key: str = ""
|
| 48 |
+
task_mode: str = "single"
|
| 49 |
+
source_manifest: List[Dict[str, Any]] = field(default_factory=list)
|
| 50 |
+
provider_options: Dict[str, Any] = field(default_factory=dict)
|
| 51 |
+
task_id: str = field(default_factory=lambda: uuid.uuid4().hex)
|
| 52 |
+
status: TaskStatus = TaskStatus.CREATED
|
| 53 |
+
created_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
|
| 54 |
+
error: str = ""
|
| 55 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 56 |
+
trace_id: str = ""
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@dataclass
|
| 60 |
+
class BookConversionTask:
|
| 61 |
+
book_id: str
|
| 62 |
+
engine: str
|
| 63 |
+
text_repo_key: str
|
| 64 |
+
audio_repo_key: str
|
| 65 |
+
segmentation_manifest: Dict[str, Any]
|
| 66 |
+
voice_name: str = ""
|
| 67 |
+
speaker_name: str = ""
|
| 68 |
+
provider_options: Dict[str, Any] = field(default_factory=dict)
|
| 69 |
+
task_id: str = field(default_factory=lambda: uuid.uuid4().hex)
|
| 70 |
+
status: TaskStatus = TaskStatus.CREATED
|
| 71 |
+
created_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
|
| 72 |
+
error: str = ""
|
| 73 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 74 |
+
trace_id: str = ""
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class AITaskManager:
|
| 78 |
+
"""
|
| 79 |
+
مدير تنفيذ مهام الصوت.
|
| 80 |
+
|
| 81 |
+
execute(): مسار التوليد المباشر وطلبات API القصيرة.
|
| 82 |
+
execute_prepared_book(): ينفذ كتاباً تم تقسيمه وحفظه مسبقاً بواسطة
|
| 83 |
+
BookPreparationManager. لا يقرر بنية الكتاب ولا التقسيم الأساسي.
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
def __init__(
|
| 87 |
+
self,
|
| 88 |
+
synthesize_callback: Callable,
|
| 89 |
+
merge_callback: Callable,
|
| 90 |
+
validate_callback: Callable,
|
| 91 |
+
router: Optional[LanguageRouter] = None,
|
| 92 |
+
report_manager: Optional[ReportManager] = None,
|
| 93 |
+
):
|
| 94 |
+
self.synthesize_callback = synthesize_callback
|
| 95 |
+
self.merge_callback = merge_callback
|
| 96 |
+
self.validate_callback = validate_callback
|
| 97 |
+
self.router = router or LanguageRouter()
|
| 98 |
+
self.report_manager = report_manager
|
| 99 |
+
|
| 100 |
+
# إعدادات مهام التوليد المباشر / API / الخزنة.
|
| 101 |
+
self.microsoft_api_chars = int(os.getenv("MICROSOFT_API_CHUNK_CHARS", "320"))
|
| 102 |
+
self.microsoft_vault_chars = int(os.getenv("MICROSOFT_VAULT_CHUNK_CHARS", "1800"))
|
| 103 |
+
self.voice_clone_chunk_chars = int(os.getenv("VOICE_CLONE_CHUNK_CHARS", "220"))
|
| 104 |
+
self.chatbox_api_chars = int(os.getenv("CHATBOX_API_CHUNK_CHARS", "700"))
|
| 105 |
+
self.chatbox_vault_chars = int(os.getenv("CHATBOX_VAULT_CHUNK_CHARS", "1400"))
|
| 106 |
+
self.bark_chunk_chars = int(os.getenv("BARK_CHUNK_CHARS", "250"))
|
| 107 |
+
|
| 108 |
+
# إعدادات التنفيذ للكتب فقط.
|
| 109 |
+
self.book_chunk_retries = max(0, int(os.getenv("BOOK_CHUNK_RETRIES", "2")))
|
| 110 |
+
self.book_pause_ms = max(0, int(os.getenv("BOOK_CHUNK_PAUSE_MS", "180")))
|
| 111 |
+
self.book_mp3_bitrate = os.getenv("BOOK_MP3_BITRATE", "128k").strip() or "128k"
|
| 112 |
+
self.book_output_dir = os.getenv("BOOK_OUTPUT_DIR", "./book_outputs").strip() or "./book_outputs"
|
| 113 |
+
|
| 114 |
+
self._task_lock = threading.Lock()
|
| 115 |
+
self._tasks: Dict[str, Any] = {}
|
| 116 |
+
|
| 117 |
+
def _report(self, event: str, task: Any, *, status: str = "ok", segment_id: str = "", duration_ms: float = 0.0, error: str = "", metadata: Optional[Dict[str, Any]] = None) -> None:
|
| 118 |
+
if not self.report_manager:
|
| 119 |
+
return
|
| 120 |
+
try:
|
| 121 |
+
self.report_manager.record(
|
| 122 |
+
event=event, trace_id=getattr(task, "trace_id", ""), manager="AITaskManager",
|
| 123 |
+
task_id=getattr(task, "task_id", ""), book_id=getattr(task, "book_id", ""),
|
| 124 |
+
segment_id=segment_id, engine=getattr(task, "engine", ""), status=status,
|
| 125 |
+
duration_ms=duration_ms, error=error, metadata=metadata or {},
|
| 126 |
+
)
|
| 127 |
+
except Exception:
|
| 128 |
+
pass
|
| 129 |
+
|
| 130 |
+
# =========================================================
|
| 131 |
+
# مهام التوليد المباشر / API / الخزنة
|
| 132 |
+
# =========================================================
|
| 133 |
+
|
| 134 |
+
def create_task(
|
| 135 |
+
self,
|
| 136 |
+
text: str,
|
| 137 |
+
source_type: str,
|
| 138 |
+
engine: str,
|
| 139 |
+
voice_name: str = "",
|
| 140 |
+
speaker_name: str = "",
|
| 141 |
+
repo_key: str = "",
|
| 142 |
+
task_mode: str = "single",
|
| 143 |
+
source_manifest: Optional[List[Dict[str, Any]]] = None,
|
| 144 |
+
provider_options: Optional[Dict[str, Any]] = None,
|
| 145 |
+
trace_id: str = "",
|
| 146 |
+
) -> VoiceTask:
|
| 147 |
+
task = VoiceTask(
|
| 148 |
+
text=(text or "").strip(),
|
| 149 |
+
source_type=(source_type or "api").strip().lower(),
|
| 150 |
+
engine=(engine or "microsoft").strip().lower(),
|
| 151 |
+
voice_name=(voice_name or "").strip(),
|
| 152 |
+
speaker_name=(speaker_name or "").strip(),
|
| 153 |
+
repo_key=(repo_key or "").strip(),
|
| 154 |
+
task_mode=(task_mode or "single").strip().lower(),
|
| 155 |
+
source_manifest=list(source_manifest or []),
|
| 156 |
+
provider_options=dict(provider_options or {}),
|
| 157 |
+
trace_id=(trace_id or "").strip(),
|
| 158 |
+
)
|
| 159 |
+
if not task.text:
|
| 160 |
+
raise ValueError("لا يمكن إنشاء مهمة صوت لنص فارغ.")
|
| 161 |
+
with self._task_lock:
|
| 162 |
+
self._tasks[task.task_id] = task
|
| 163 |
+
return task
|
| 164 |
+
|
| 165 |
+
def _split_hard_limit(self, text: str, max_chars: int) -> List[str]:
|
| 166 |
+
value = (text or "").strip()
|
| 167 |
+
if not value:
|
| 168 |
+
return []
|
| 169 |
+
if len(value) <= max_chars:
|
| 170 |
+
return [value]
|
| 171 |
+
|
| 172 |
+
words = value.split()
|
| 173 |
+
chunks: List[str] = []
|
| 174 |
+
buffer = ""
|
| 175 |
+
|
| 176 |
+
for word in words:
|
| 177 |
+
candidate = f"{buffer} {word}".strip()
|
| 178 |
+
if len(candidate) <= max_chars:
|
| 179 |
+
buffer = candidate
|
| 180 |
+
continue
|
| 181 |
+
if buffer:
|
| 182 |
+
chunks.append(buffer)
|
| 183 |
+
buffer = word
|
| 184 |
+
|
| 185 |
+
if buffer:
|
| 186 |
+
chunks.append(buffer)
|
| 187 |
+
|
| 188 |
+
return chunks
|
| 189 |
+
|
| 190 |
+
def _microsoft_segments(self, task: VoiceTask) -> List[RoutedSegment]:
|
| 191 |
+
limit = self.microsoft_vault_chars if task.source_type == "vault" else self.microsoft_api_chars
|
| 192 |
+
routed = self.router.route_for_microsoft(task.text)
|
| 193 |
+
routed = self.router.merge_adjacent_segments(routed, max_chars=limit)
|
| 194 |
+
final_segments: List[RoutedSegment] = []
|
| 195 |
+
for segment in routed:
|
| 196 |
+
for chunk in self._split_hard_limit(segment.text, limit):
|
| 197 |
+
final_segments.append(RoutedSegment(
|
| 198 |
+
index=len(final_segments),
|
| 199 |
+
text=chunk,
|
| 200 |
+
language=segment.language,
|
| 201 |
+
provider=segment.provider,
|
| 202 |
+
))
|
| 203 |
+
return final_segments
|
| 204 |
+
|
| 205 |
+
def _voice_clone_segments(self, task: VoiceTask) -> List[RoutedSegment]:
|
| 206 |
+
sentences = self.router.split_sentences(task.text)
|
| 207 |
+
final_segments: List[RoutedSegment] = []
|
| 208 |
+
for sentence in sentences:
|
| 209 |
+
for chunk in self._split_hard_limit(sentence, self.voice_clone_chunk_chars):
|
| 210 |
+
final_segments.append(RoutedSegment(
|
| 211 |
+
index=len(final_segments),
|
| 212 |
+
text=chunk,
|
| 213 |
+
language=self.router.detect_language(chunk),
|
| 214 |
+
provider="coqui_voice_clone",
|
| 215 |
+
))
|
| 216 |
+
return final_segments
|
| 217 |
+
|
| 218 |
+
def _bark_segments(self, task: VoiceTask) -> List[RoutedSegment]:
|
| 219 |
+
sentences = self.router.split_sentences(task.text) or [task.text]
|
| 220 |
+
final_segments: List[RoutedSegment] = []
|
| 221 |
+
for sentence in sentences:
|
| 222 |
+
for chunk in self._split_hard_limit(sentence, self.bark_chunk_chars):
|
| 223 |
+
final_segments.append(RoutedSegment(
|
| 224 |
+
index=len(final_segments),
|
| 225 |
+
text=chunk,
|
| 226 |
+
language=self.router.detect_language(chunk),
|
| 227 |
+
provider="bark",
|
| 228 |
+
))
|
| 229 |
+
return final_segments
|
| 230 |
+
|
| 231 |
+
def _chatbox_segments(self, task: VoiceTask) -> List[RoutedSegment]:
|
| 232 |
+
limit = self.chatbox_vault_chars if task.source_type == "vault" else self.chatbox_api_chars
|
| 233 |
+
sentences = self.router.split_sentences(task.text)
|
| 234 |
+
if not sentences:
|
| 235 |
+
sentences = [task.text]
|
| 236 |
+
final_segments: List[RoutedSegment] = []
|
| 237 |
+
buffer = ""
|
| 238 |
+
|
| 239 |
+
def append_buffer(value: str) -> None:
|
| 240 |
+
clean = (value or "").strip()
|
| 241 |
+
if not clean:
|
| 242 |
+
return
|
| 243 |
+
for chunk in self._split_hard_limit(clean, limit):
|
| 244 |
+
final_segments.append(RoutedSegment(
|
| 245 |
+
index=len(final_segments),
|
| 246 |
+
text=chunk,
|
| 247 |
+
language=self.router.detect_language(chunk),
|
| 248 |
+
provider="chatbox_florate",
|
| 249 |
+
))
|
| 250 |
+
|
| 251 |
+
for sentence in sentences:
|
| 252 |
+
sentence = (sentence or "").strip()
|
| 253 |
+
if not sentence:
|
| 254 |
+
continue
|
| 255 |
+
candidate = f"{buffer} {sentence}".strip()
|
| 256 |
+
if len(candidate) <= limit:
|
| 257 |
+
buffer = candidate
|
| 258 |
+
continue
|
| 259 |
+
if buffer:
|
| 260 |
+
append_buffer(buffer)
|
| 261 |
+
buffer = ""
|
| 262 |
+
if len(sentence) <= limit:
|
| 263 |
+
buffer = sentence
|
| 264 |
+
else:
|
| 265 |
+
append_buffer(sentence)
|
| 266 |
+
if buffer:
|
| 267 |
+
append_buffer(buffer)
|
| 268 |
+
return final_segments
|
| 269 |
+
|
| 270 |
+
def execute(self, task: VoiceTask) -> Tuple[str, Dict[str, Any]]:
|
| 271 |
+
temp_paths: List[str] = []
|
| 272 |
+
self._report("task.started", task)
|
| 273 |
+
try:
|
| 274 |
+
task.status = TaskStatus.SEGMENTING
|
| 275 |
+
if task.engine in {"coqui", "coqui_voice_clone", "live_teacher"}:
|
| 276 |
+
segments = self._voice_clone_segments(task)
|
| 277 |
+
elif task.engine in {"bark", "bark_tts", "suno_bark", "bark_small"}:
|
| 278 |
+
segments = self._bark_segments(task)
|
| 279 |
+
elif task.engine in {
|
| 280 |
+
"chatbox", "florate", "chatbox_florate",
|
| 281 |
+
"chatbox/florate", "chatbox_florate_tts",
|
| 282 |
+
}:
|
| 283 |
+
segments = self._chatbox_segments(task)
|
| 284 |
+
else:
|
| 285 |
+
segments = self._microsoft_segments(task)
|
| 286 |
+
|
| 287 |
+
if not segments:
|
| 288 |
+
raise RuntimeError("لم ينتج مدير المهام أي مقطع صالح.")
|
| 289 |
+
|
| 290 |
+
task.status = TaskStatus.SYNTHESIZING
|
| 291 |
+
segment_meta: List[Dict[str, Any]] = []
|
| 292 |
+
for segment in segments:
|
| 293 |
+
path = self.synthesize_callback(
|
| 294 |
+
provider=segment.provider,
|
| 295 |
+
text=segment.text,
|
| 296 |
+
voice_name=task.voice_name,
|
| 297 |
+
speaker_name=task.speaker_name,
|
| 298 |
+
source_type=task.source_type,
|
| 299 |
+
provider_options=task.provider_options,
|
| 300 |
+
trace_id=task.trace_id,
|
| 301 |
+
segment_id=f"segment_{segment.index + 1:04d}",
|
| 302 |
+
)
|
| 303 |
+
self.validate_callback(path)
|
| 304 |
+
temp_paths.append(path)
|
| 305 |
+
segment_meta.append({
|
| 306 |
+
"index": segment.index,
|
| 307 |
+
"language": segment.language,
|
| 308 |
+
"provider": segment.provider,
|
| 309 |
+
"chars": len(segment.text),
|
| 310 |
+
})
|
| 311 |
+
|
| 312 |
+
task.status = TaskStatus.MERGING
|
| 313 |
+
final_path = self.merge_callback(temp_paths, prefix=f"tts_task_{task.task_id}")
|
| 314 |
+
audio_info = self.validate_callback(final_path)
|
| 315 |
+
|
| 316 |
+
task.status = TaskStatus.DONE
|
| 317 |
+
task.metadata = {
|
| 318 |
+
"task_id": task.task_id,
|
| 319 |
+
"source_type": task.source_type,
|
| 320 |
+
"engine": task.engine,
|
| 321 |
+
"voice_name": task.voice_name,
|
| 322 |
+
"repo_key": task.repo_key,
|
| 323 |
+
"segments_count": len(segments),
|
| 324 |
+
"segments": segment_meta,
|
| 325 |
+
"audio": audio_info,
|
| 326 |
+
"status": task.status.value,
|
| 327 |
+
}
|
| 328 |
+
self._report("task.completed", task, metadata={"segments_count": len(segments)})
|
| 329 |
+
return final_path, task.metadata
|
| 330 |
+
except Exception as exc:
|
| 331 |
+
task.status = TaskStatus.FAILED
|
| 332 |
+
task.error = str(exc)
|
| 333 |
+
task.metadata = {
|
| 334 |
+
"task_id": task.task_id,
|
| 335 |
+
"status": task.status.value,
|
| 336 |
+
"error": task.error,
|
| 337 |
+
}
|
| 338 |
+
self._report("task.failed", task, status="error", error=str(exc))
|
| 339 |
+
raise
|
| 340 |
+
finally:
|
| 341 |
+
for path in temp_paths:
|
| 342 |
+
try:
|
| 343 |
+
if path and os.path.isfile(path):
|
| 344 |
+
os.remove(path)
|
| 345 |
+
except Exception:
|
| 346 |
+
pass
|
| 347 |
+
|
| 348 |
+
# =========================================================
|
| 349 |
+
# تنفيذ كتاب محضر مسبقاً
|
| 350 |
+
# =========================================================
|
| 351 |
+
|
| 352 |
+
def create_book_conversion_task(
|
| 353 |
+
self,
|
| 354 |
+
book_id: str,
|
| 355 |
+
engine: str,
|
| 356 |
+
text_repo_key: str,
|
| 357 |
+
audio_repo_key: str,
|
| 358 |
+
segmentation_manifest: Dict[str, Any],
|
| 359 |
+
voice_name: str = "",
|
| 360 |
+
speaker_name: str = "",
|
| 361 |
+
provider_options: Optional[Dict[str, Any]] = None,
|
| 362 |
+
trace_id: str = "",
|
| 363 |
+
) -> BookConversionTask:
|
| 364 |
+
if not (book_id or "").strip():
|
| 365 |
+
raise ValueError("book_id مطلوب.")
|
| 366 |
+
if not segmentation_manifest.get("segments"):
|
| 367 |
+
raise ValueError("لا توجد مقاطع نصية محضرة للمهمة.")
|
| 368 |
+
task = BookConversionTask(
|
| 369 |
+
book_id=book_id.strip(),
|
| 370 |
+
engine=(engine or "").strip().lower(),
|
| 371 |
+
text_repo_key=(text_repo_key or "").strip(),
|
| 372 |
+
audio_repo_key=(audio_repo_key or "").strip(),
|
| 373 |
+
segmentation_manifest=dict(segmentation_manifest),
|
| 374 |
+
voice_name=(voice_name or "").strip(),
|
| 375 |
+
speaker_name=(speaker_name or "").strip(),
|
| 376 |
+
provider_options=dict(provider_options or {}),
|
| 377 |
+
trace_id=(trace_id or "").strip(),
|
| 378 |
+
)
|
| 379 |
+
with self._task_lock:
|
| 380 |
+
self._tasks[task.task_id] = task
|
| 381 |
+
return task
|
| 382 |
+
|
| 383 |
+
@staticmethod
|
| 384 |
+
def _provider_for_engine(engine: str) -> str:
|
| 385 |
+
key = (engine or "").strip().lower()
|
| 386 |
+
if key in {"coqui", "coqui_voice_clone", "live_teacher"}:
|
| 387 |
+
return "coqui_voice_clone"
|
| 388 |
+
if key in {"bark", "bark_tts", "suno_bark", "bark_small"}:
|
| 389 |
+
return "bark"
|
| 390 |
+
if key in {"chatbox", "florate", "chatbox_florate", "chatbox/florate", "chatbox_florate_tts"}:
|
| 391 |
+
return "chatbox_florate"
|
| 392 |
+
if key in {"visco", "visco_tts"}:
|
| 393 |
+
return "visco_tts"
|
| 394 |
+
if key in {"openai", "openai_tts"}:
|
| 395 |
+
return "openai_tts"
|
| 396 |
+
if key in {"microsoft_normal", "microsoft_arabic", "edge_arabic"}:
|
| 397 |
+
return "microsoft_normal"
|
| 398 |
+
return "microsoft_multilingual"
|
| 399 |
+
|
| 400 |
+
@staticmethod
|
| 401 |
+
def _conversion_fingerprint(task: BookConversionTask) -> str:
|
| 402 |
+
payload = {
|
| 403 |
+
"engine": task.engine,
|
| 404 |
+
"voice_name": task.voice_name,
|
| 405 |
+
"speaker_name": task.speaker_name,
|
| 406 |
+
"provider_options": task.provider_options,
|
| 407 |
+
}
|
| 408 |
+
raw = json.dumps(payload, ensure_ascii=False, sort_keys=True, default=str).encode("utf-8")
|
| 409 |
+
return hashlib.sha256(raw).hexdigest()
|
| 410 |
+
|
| 411 |
+
@staticmethod
|
| 412 |
+
def _segment_execution_context(task: BookConversionTask, segment: Dict[str, Any]) -> Dict[str, Any]:
|
| 413 |
+
"""ادمج إعداد المهمة مع إعداد المقطع؛ المقطع هو المرجع الأعلى عند وجود قيمة صريحة."""
|
| 414 |
+
options = dict(task.provider_options or {})
|
| 415 |
+
generation_settings = dict(segment.get("generation_settings") or {})
|
| 416 |
+
options.update(generation_settings)
|
| 417 |
+
|
| 418 |
+
mapping = {
|
| 419 |
+
"language": "language",
|
| 420 |
+
"emotion": "emotion",
|
| 421 |
+
"emotion_source": "emotion_source",
|
| 422 |
+
"style": "style",
|
| 423 |
+
"speed": "speed",
|
| 424 |
+
"pitch": "pitch",
|
| 425 |
+
"volume": "volume",
|
| 426 |
+
"voice_profile": "voice_profile",
|
| 427 |
+
"character_id": "character_id",
|
| 428 |
+
"speaker_id": "speaker_id",
|
| 429 |
+
}
|
| 430 |
+
for source_key, option_key in mapping.items():
|
| 431 |
+
value = segment.get(source_key)
|
| 432 |
+
if value not in (None, ""):
|
| 433 |
+
options[option_key] = value
|
| 434 |
+
|
| 435 |
+
voice_name = str(segment.get("voice_id") or task.voice_name or "").strip()
|
| 436 |
+
speaker_name = str(
|
| 437 |
+
segment.get("voice_profile")
|
| 438 |
+
or segment.get("speaker_id")
|
| 439 |
+
or task.speaker_name
|
| 440 |
+
or ""
|
| 441 |
+
).strip()
|
| 442 |
+
return {
|
| 443 |
+
"voice_name": voice_name,
|
| 444 |
+
"speaker_name": speaker_name,
|
| 445 |
+
"provider_options": options,
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
@classmethod
|
| 449 |
+
def _segment_synthesis_fingerprint(cls, task: BookConversionTask, segment: Dict[str, Any]) -> str:
|
| 450 |
+
context = cls._segment_execution_context(task, segment)
|
| 451 |
+
payload = {
|
| 452 |
+
"engine": task.engine,
|
| 453 |
+
"voice_name": context["voice_name"],
|
| 454 |
+
"speaker_name": context["speaker_name"],
|
| 455 |
+
"provider_options": context["provider_options"],
|
| 456 |
+
"emotion": segment.get("emotion"),
|
| 457 |
+
"voice_profile": segment.get("voice_profile"),
|
| 458 |
+
"revision": int(segment.get("revision", 1) or 1),
|
| 459 |
+
}
|
| 460 |
+
raw = json.dumps(payload, ensure_ascii=False, sort_keys=True, default=str).encode("utf-8")
|
| 461 |
+
return hashlib.sha256(raw).hexdigest()
|
| 462 |
+
|
| 463 |
+
def _to_mp3(self, source_path: str, target_path: str, add_pause: bool = False) -> Dict[str, Any]:
|
| 464 |
+
self.validate_callback(source_path)
|
| 465 |
+
audio = AudioSegment.from_file(source_path)
|
| 466 |
+
if add_pause and self.book_pause_ms > 0:
|
| 467 |
+
audio += AudioSegment.silent(duration=self.book_pause_ms)
|
| 468 |
+
audio.export(target_path, format="mp3", bitrate=self.book_mp3_bitrate)
|
| 469 |
+
return self.validate_callback(target_path)
|
| 470 |
+
|
| 471 |
+
def _merge_book_mp3(self, paths: List[str], final_path: str) -> str:
|
| 472 |
+
if not paths:
|
| 473 |
+
raise ValueError("لا توجد ملفات صوت للدمج.")
|
| 474 |
+
concat_path = final_path + ".concat.txt"
|
| 475 |
+
try:
|
| 476 |
+
with open(concat_path, "w", encoding="utf-8") as handle:
|
| 477 |
+
for path in paths:
|
| 478 |
+
escaped = os.path.abspath(path).replace("'", "'\\''")
|
| 479 |
+
handle.write(f"file '{escaped}'\n")
|
| 480 |
+
command = [
|
| 481 |
+
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
|
| 482 |
+
"-f", "concat", "-safe", "0", "-i", concat_path,
|
| 483 |
+
"-c:a", "libmp3lame", "-b:a", self.book_mp3_bitrate,
|
| 484 |
+
final_path,
|
| 485 |
+
]
|
| 486 |
+
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 487 |
+
if result.returncode != 0:
|
| 488 |
+
raise RuntimeError(f"فشل دمج الكتاب عبر ffmpeg: {result.stderr.strip()}")
|
| 489 |
+
self.validate_callback(final_path)
|
| 490 |
+
return final_path
|
| 491 |
+
finally:
|
| 492 |
+
try:
|
| 493 |
+
if os.path.isfile(concat_path):
|
| 494 |
+
os.remove(concat_path)
|
| 495 |
+
except Exception:
|
| 496 |
+
pass
|
| 497 |
+
|
| 498 |
+
def _book_language_units(
|
| 499 |
+
self,
|
| 500 |
+
task: BookConversionTask,
|
| 501 |
+
text: str,
|
| 502 |
+
base_provider: str,
|
| 503 |
+
segment: Optional[Dict[str, Any]] = None,
|
| 504 |
+
) -> List[Dict[str, Any]]:
|
| 505 |
+
"""
|
| 506 |
+
يحدد وحدات اللغة داخل المقطع من دون تغيير segment_id الأساسي.
|
| 507 |
+
مسار execute() المباشر لا يستخدم هذه الدالة؛ هي خاصة بتحويل الكتب.
|
| 508 |
+
"""
|
| 509 |
+
segment_context = self._segment_execution_context(task, dict(segment or {}))
|
| 510 |
+
options = dict(segment_context["provider_options"] or {})
|
| 511 |
+
mode = str(options.get("language_mode") or "auto").strip().lower()
|
| 512 |
+
clean = (text or "").strip()
|
| 513 |
+
if not clean:
|
| 514 |
+
return []
|
| 515 |
+
|
| 516 |
+
if mode in {"ar", "arabic", "arabic_only"}:
|
| 517 |
+
routed = [RoutedSegment(index=0, text=clean, language="ar", provider="")]
|
| 518 |
+
elif mode in {"en", "english", "english_only"}:
|
| 519 |
+
routed = [RoutedSegment(index=0, text=clean, language="en", provider="")]
|
| 520 |
+
elif base_provider in {"microsoft_normal", "microsoft_multilingual", "microsoft_english", "coqui_voice_clone"}:
|
| 521 |
+
routed = self.router.split_language_runs(clean)
|
| 522 |
+
if not routed:
|
| 523 |
+
routed = [RoutedSegment(index=0, text=clean, language=self.router.detect_language(clean), provider="")]
|
| 524 |
+
else:
|
| 525 |
+
# لا نغير API المحركات الأخرى؛ نرسل النص كما كان.
|
| 526 |
+
routed = [RoutedSegment(index=0, text=clean, language=self.router.detect_language(clean), provider="")]
|
| 527 |
+
|
| 528 |
+
units: List[Dict[str, Any]] = []
|
| 529 |
+
default_language = str(options.get("default_language") or "ar").lower()
|
| 530 |
+
for routed_part in routed:
|
| 531 |
+
language = routed_part.language
|
| 532 |
+
if language not in {"ar", "en"}:
|
| 533 |
+
language = "en" if default_language.startswith("en") else "ar"
|
| 534 |
+
|
| 535 |
+
provider = base_provider
|
| 536 |
+
voice_name = str(segment_context["voice_name"] or task.voice_name or "")
|
| 537 |
+
speaker_name = str(segment_context["speaker_name"] or task.speaker_name or "")
|
| 538 |
+
unit_options = dict(options)
|
| 539 |
+
unit_options["language"] = language
|
| 540 |
+
|
| 541 |
+
if base_provider in {"microsoft_normal", "microsoft_multilingual", "microsoft_english"}:
|
| 542 |
+
if language == "ar":
|
| 543 |
+
provider = "microsoft_normal"
|
| 544 |
+
voice_name = str(options.get("arabic_voice_name") or task.voice_name or "")
|
| 545 |
+
else:
|
| 546 |
+
provider = "microsoft_english"
|
| 547 |
+
voice_name = str(options.get("english_voice_name") or options.get("multilingual_voice_name") or task.voice_name or "")
|
| 548 |
+
elif base_provider == "coqui_voice_clone":
|
| 549 |
+
if language == "ar":
|
| 550 |
+
speaker_name = str(options.get("arabic_speaker_name") or task.speaker_name or "")
|
| 551 |
+
else:
|
| 552 |
+
speaker_name = str(options.get("english_speaker_name") or task.speaker_name or "")
|
| 553 |
+
|
| 554 |
+
units.append({
|
| 555 |
+
"text": routed_part.text,
|
| 556 |
+
"language": language,
|
| 557 |
+
"provider": provider,
|
| 558 |
+
"voice_name": voice_name,
|
| 559 |
+
"speaker_name": speaker_name,
|
| 560 |
+
"provider_options": unit_options,
|
| 561 |
+
})
|
| 562 |
+
return units
|
| 563 |
+
|
| 564 |
+
def _synthesize_stable_segment(
|
| 565 |
+
self,
|
| 566 |
+
task: BookConversionTask,
|
| 567 |
+
text: str,
|
| 568 |
+
max_chars: int,
|
| 569 |
+
segment_id: str,
|
| 570 |
+
work_dir: str,
|
| 571 |
+
segment: Optional[Dict[str, Any]] = None,
|
| 572 |
+
) -> Tuple[str, int]:
|
| 573 |
+
"""
|
| 574 |
+
التقسيم الأساسي لا يتغير. إذا تجاوز النص الحد بصورة غير متوقعة،
|
| 575 |
+
تقسم المهمة داخلياً إلى subchunks ثم تعيد ملفاً واحداً لنفس segment_id.
|
| 576 |
+
"""
|
| 577 |
+
provider = self._provider_for_engine(task.engine)
|
| 578 |
+
subchunks = self._split_hard_limit(text, max_chars)
|
| 579 |
+
if not subchunks:
|
| 580 |
+
raise ValueError(f"المقطع {segment_id} فارغ.")
|
| 581 |
+
|
| 582 |
+
temp_paths: List[str] = []
|
| 583 |
+
synthesis_units = 0
|
| 584 |
+
try:
|
| 585 |
+
for sub_index, subtext in enumerate(subchunks, start=1):
|
| 586 |
+
language_units = self._book_language_units(task, subtext, provider, segment=segment)
|
| 587 |
+
if not language_units:
|
| 588 |
+
raise RuntimeError(f"لم ينتج الراوتر وحدات لغة لـ {segment_id}/sub_{sub_index:02d}.")
|
| 589 |
+
|
| 590 |
+
for language_index, unit in enumerate(language_units, start=1):
|
| 591 |
+
synthesis_units += 1
|
| 592 |
+
last_error: Optional[Exception] = None
|
| 593 |
+
produced = ""
|
| 594 |
+
for attempt in range(self.book_chunk_retries + 1):
|
| 595 |
+
try:
|
| 596 |
+
produced = self.synthesize_callback(
|
| 597 |
+
provider=unit["provider"],
|
| 598 |
+
text=unit["text"],
|
| 599 |
+
voice_name=unit["voice_name"],
|
| 600 |
+
speaker_name=unit["speaker_name"],
|
| 601 |
+
source_type="book",
|
| 602 |
+
provider_options=unit["provider_options"],
|
| 603 |
+
trace_id=task.trace_id,
|
| 604 |
+
segment_id=f"{segment_id}_u{synthesis_units:02d}",
|
| 605 |
+
)
|
| 606 |
+
self.validate_callback(produced)
|
| 607 |
+
break
|
| 608 |
+
except Exception as exc:
|
| 609 |
+
last_error = exc
|
| 610 |
+
produced = ""
|
| 611 |
+
self._report(
|
| 612 |
+
"voice.segment.retry", task, segment_id=segment_id, status="retry", error=str(exc),
|
| 613 |
+
metadata={
|
| 614 |
+
"sub_index": sub_index,
|
| 615 |
+
"language_index": language_index,
|
| 616 |
+
"language": unit["language"],
|
| 617 |
+
"provider": unit["provider"],
|
| 618 |
+
"attempt": attempt + 1,
|
| 619 |
+
},
|
| 620 |
+
)
|
| 621 |
+
if attempt >= self.book_chunk_retries:
|
| 622 |
+
raise RuntimeError(
|
| 623 |
+
f"فشل {segment_id}/sub_{sub_index:02d}/{unit['language']} بعد {attempt + 1} محاولة: {exc}"
|
| 624 |
+
) from exc
|
| 625 |
+
if not produced:
|
| 626 |
+
raise RuntimeError(
|
| 627 |
+
f"لم ينتج {segment_id}/sub_{sub_index:02d}/{unit['language']}: {last_error}"
|
| 628 |
+
)
|
| 629 |
+
temp_paths.append(produced)
|
| 630 |
+
|
| 631 |
+
segment_mp3 = os.path.join(work_dir, f"{segment_id}.mp3")
|
| 632 |
+
if len(temp_paths) == 1:
|
| 633 |
+
self._to_mp3(temp_paths[0], segment_mp3, add_pause=False)
|
| 634 |
+
else:
|
| 635 |
+
merged = self.merge_callback(temp_paths, prefix=f"{segment_id}_submerge")
|
| 636 |
+
try:
|
| 637 |
+
self._to_mp3(merged, segment_mp3, add_pause=False)
|
| 638 |
+
finally:
|
| 639 |
+
try:
|
| 640 |
+
if os.path.isfile(merged):
|
| 641 |
+
os.remove(merged)
|
| 642 |
+
except Exception:
|
| 643 |
+
pass
|
| 644 |
+
return segment_mp3, synthesis_units
|
| 645 |
+
finally:
|
| 646 |
+
for path in temp_paths:
|
| 647 |
+
try:
|
| 648 |
+
if path and os.path.isfile(path):
|
| 649 |
+
os.remove(path)
|
| 650 |
+
except Exception:
|
| 651 |
+
pass
|
| 652 |
+
|
| 653 |
+
def execute_prepared_book(
|
| 654 |
+
self,
|
| 655 |
+
task: BookConversionTask,
|
| 656 |
+
storage_manager: StorageManager,
|
| 657 |
+
) -> Tuple[str, Dict[str, Any]]:
|
| 658 |
+
segments = sorted(
|
| 659 |
+
list(task.segmentation_manifest.get("segments") or []),
|
| 660 |
+
key=lambda item: int(item.get("index", 0)),
|
| 661 |
+
)
|
| 662 |
+
if not segments:
|
| 663 |
+
raise ValueError("لا توجد مقاطع في segmentation manifest.")
|
| 664 |
+
|
| 665 |
+
capability = dict(task.segmentation_manifest.get("engine_capability") or {})
|
| 666 |
+
max_chars = max(60, int(capability.get("max_chunk_chars", 1200)))
|
| 667 |
+
batches_count = int(task.segmentation_manifest.get("batches_count", 1) or 1)
|
| 668 |
+
task_root = os.path.abspath(os.path.join(self.book_output_dir, f"book_{task.book_id}_{task.task_id}"))
|
| 669 |
+
os.makedirs(task_root, exist_ok=True)
|
| 670 |
+
|
| 671 |
+
local_audio_paths: List[str] = []
|
| 672 |
+
segment_results: List[Dict[str, Any]] = []
|
| 673 |
+
self._report("book.conversion.started", task, metadata={"segments_count": len(segments), "batches_count": batches_count})
|
| 674 |
+
try:
|
| 675 |
+
task.status = TaskStatus.READING
|
| 676 |
+
synthesis_fingerprint = self._conversion_fingerprint(task)
|
| 677 |
+
try:
|
| 678 |
+
existing_audio = set(storage_manager.list_repo_files(task.audio_repo_key))
|
| 679 |
+
except Exception:
|
| 680 |
+
existing_audio = set()
|
| 681 |
+
|
| 682 |
+
previous_manifest: Dict[str, Any] = {}
|
| 683 |
+
try:
|
| 684 |
+
previous_manifest = storage_manager.read_conversion_manifest(
|
| 685 |
+
task.audio_repo_key,
|
| 686 |
+
task.book_id,
|
| 687 |
+
task.engine,
|
| 688 |
+
)
|
| 689 |
+
except Exception:
|
| 690 |
+
previous_manifest = {}
|
| 691 |
+
previous_segments = {
|
| 692 |
+
str(item.get("segment_id")): item
|
| 693 |
+
for item in (previous_manifest.get("segments") or [])
|
| 694 |
+
if item.get("segment_id")
|
| 695 |
+
}
|
| 696 |
+
previous_fingerprint = str(previous_manifest.get("synthesis_fingerprint") or "")
|
| 697 |
+
|
| 698 |
+
task.metadata = {
|
| 699 |
+
"task_id": task.task_id,
|
| 700 |
+
"book_id": task.book_id,
|
| 701 |
+
"engine": task.engine,
|
| 702 |
+
"text_repo_key": task.text_repo_key,
|
| 703 |
+
"audio_repo_key": task.audio_repo_key,
|
| 704 |
+
"segments_count": len(segments),
|
| 705 |
+
"batches_count": batches_count,
|
| 706 |
+
"completed_segments": 0,
|
| 707 |
+
"current_segment": 0,
|
| 708 |
+
"current_batch": 0,
|
| 709 |
+
"synthesis_fingerprint": synthesis_fingerprint,
|
| 710 |
+
"status": task.status.value,
|
| 711 |
+
}
|
| 712 |
+
|
| 713 |
+
for position, segment in enumerate(segments, start=1):
|
| 714 |
+
segment_id = str(segment.get("segment_id") or f"segment_{position:04d}")
|
| 715 |
+
text_path = str(segment.get("text_path") or "")
|
| 716 |
+
audio_path = str(segment.get("audio_path") or storage_manager.book_audio_segment_path(
|
| 717 |
+
task.book_id, task.engine, segment_id
|
| 718 |
+
))
|
| 719 |
+
batch_index = int(segment.get("batch_index", 1) or 1)
|
| 720 |
+
task.metadata["current_segment"] = position
|
| 721 |
+
task.metadata["current_batch"] = batch_index
|
| 722 |
+
|
| 723 |
+
segment_text_sha = str(segment.get("text_sha256") or "")
|
| 724 |
+
text = ""
|
| 725 |
+
if not segment_text_sha:
|
| 726 |
+
task.status = TaskStatus.READING
|
| 727 |
+
text = storage_manager.read_text_file(task.text_repo_key, text_path).strip()
|
| 728 |
+
if not text:
|
| 729 |
+
raise ValueError(f"المقطع النصي فارغ: {segment_id}")
|
| 730 |
+
segment_text_sha = hashlib.sha256(text.encode("utf-8")).hexdigest()
|
| 731 |
+
|
| 732 |
+
previous = previous_segments.get(segment_id) or {}
|
| 733 |
+
segment_fingerprint = self._segment_synthesis_fingerprint(task, segment)
|
| 734 |
+
reusable = (
|
| 735 |
+
audio_path in existing_audio
|
| 736 |
+
and str(previous.get("segment_synthesis_fingerprint") or previous.get("synthesis_fingerprint") or "")
|
| 737 |
+
in {segment_fingerprint, synthesis_fingerprint}
|
| 738 |
+
and str(previous.get("text_sha256") or "") == segment_text_sha
|
| 739 |
+
)
|
| 740 |
+
|
| 741 |
+
# الاستئناف آمن فقط إذا لم يتغير النص ولا إعداد الصوت.
|
| 742 |
+
if reusable:
|
| 743 |
+
local_path = storage_manager.download_file(task.audio_repo_key, audio_path)
|
| 744 |
+
self.validate_callback(local_path)
|
| 745 |
+
local_audio_paths.append(local_path)
|
| 746 |
+
audio_obj = AudioSegment.from_file(local_path)
|
| 747 |
+
duration_seconds = len(audio_obj) / 1000.0
|
| 748 |
+
result = dict(segment)
|
| 749 |
+
result.update({
|
| 750 |
+
"segment_id": segment_id,
|
| 751 |
+
"index": position,
|
| 752 |
+
"sequence_index": int(segment.get("sequence_index", position) or position),
|
| 753 |
+
"batch_index": batch_index,
|
| 754 |
+
"text_path": text_path,
|
| 755 |
+
"audio_path": audio_path,
|
| 756 |
+
"text_sha256": segment_text_sha,
|
| 757 |
+
"synthesis_fingerprint": synthesis_fingerprint,
|
| 758 |
+
"segment_synthesis_fingerprint": segment_fingerprint,
|
| 759 |
+
"duration_seconds": duration_seconds,
|
| 760 |
+
"status": "EXISTING",
|
| 761 |
+
"subchunks": 0,
|
| 762 |
+
})
|
| 763 |
+
segment_results.append(storage_manager.normalize_segment_metadata(result, engine=task.engine))
|
| 764 |
+
else:
|
| 765 |
+
if not text:
|
| 766 |
+
task.status = TaskStatus.READING
|
| 767 |
+
text = storage_manager.read_text_file(task.text_repo_key, text_path).strip()
|
| 768 |
+
if not text:
|
| 769 |
+
raise ValueError(f"المقطع النصي فارغ: {segment_id}")
|
| 770 |
+
|
| 771 |
+
task.status = TaskStatus.SYNTHESIZING
|
| 772 |
+
local_mp3, subchunks_count = self._synthesize_stable_segment(
|
| 773 |
+
task=task,
|
| 774 |
+
text=text,
|
| 775 |
+
max_chars=max_chars,
|
| 776 |
+
segment_id=segment_id,
|
| 777 |
+
work_dir=task_root,
|
| 778 |
+
segment=segment,
|
| 779 |
+
)
|
| 780 |
+
task.status = TaskStatus.SAVING
|
| 781 |
+
storage_manager.save_audio_segment(
|
| 782 |
+
repo_key=task.audio_repo_key,
|
| 783 |
+
book_id=task.book_id,
|
| 784 |
+
engine=task.engine,
|
| 785 |
+
segment_id=segment_id,
|
| 786 |
+
local_mp3_path=local_mp3,
|
| 787 |
+
)
|
| 788 |
+
existing_audio.add(audio_path)
|
| 789 |
+
local_audio_paths.append(local_mp3)
|
| 790 |
+
audio_obj = AudioSegment.from_file(local_mp3)
|
| 791 |
+
duration_seconds = len(audio_obj) / 1000.0
|
| 792 |
+
result = dict(segment)
|
| 793 |
+
result.update({
|
| 794 |
+
"segment_id": segment_id,
|
| 795 |
+
"index": position,
|
| 796 |
+
"sequence_index": int(segment.get("sequence_index", position) or position),
|
| 797 |
+
"batch_index": batch_index,
|
| 798 |
+
"text_path": text_path,
|
| 799 |
+
"audio_path": audio_path,
|
| 800 |
+
"text_sha256": segment_text_sha,
|
| 801 |
+
"synthesis_fingerprint": synthesis_fingerprint,
|
| 802 |
+
"segment_synthesis_fingerprint": segment_fingerprint,
|
| 803 |
+
"duration_seconds": duration_seconds,
|
| 804 |
+
"generation_version": VERSION,
|
| 805 |
+
"status": "GENERATED",
|
| 806 |
+
"subchunks": subchunks_count,
|
| 807 |
+
})
|
| 808 |
+
segment_results.append(storage_manager.normalize_segment_metadata(result, engine=task.engine))
|
| 809 |
+
|
| 810 |
+
task.metadata["completed_segments"] = position
|
| 811 |
+
task.metadata["status"] = task.status.value
|
| 812 |
+
|
| 813 |
+
# نحفظ تقدم المهمة عند نهاية كل Batch أو آخر مقطع.
|
| 814 |
+
next_batch = None
|
| 815 |
+
if position < len(segments):
|
| 816 |
+
next_batch = int(segments[position].get("batch_index", batch_index) or batch_index)
|
| 817 |
+
if position == len(segments) or next_batch != batch_index:
|
| 818 |
+
cursor_seconds = 0.0
|
| 819 |
+
for timeline_item in segment_results:
|
| 820 |
+
before_ms = int(timeline_item.get("pause_before_ms", 0) or 0)
|
| 821 |
+
after_ms = int(timeline_item.get("pause_after_ms", self.book_pause_ms) or 0)
|
| 822 |
+
cursor_seconds += before_ms / 1000.0
|
| 823 |
+
timeline_item["start_time"] = round(cursor_seconds, 6)
|
| 824 |
+
cursor_seconds += float(timeline_item.get("duration_seconds", 0.0) or 0.0)
|
| 825 |
+
timeline_item["end_time"] = round(cursor_seconds, 6)
|
| 826 |
+
cursor_seconds += after_ms / 1000.0
|
| 827 |
+
|
| 828 |
+
checkpoint = {
|
| 829 |
+
"schema_version": CONVERSION_MANIFEST_SCHEMA,
|
| 830 |
+
**task.metadata,
|
| 831 |
+
"voice_name": task.voice_name,
|
| 832 |
+
"provider_options": task.provider_options,
|
| 833 |
+
"segments": segment_results,
|
| 834 |
+
"status": "IN_PROGRESS" if position < len(segments) else "AUDIO_SEGMENTS_COMPLETE",
|
| 835 |
+
"updated_at": datetime.now(timezone.utc).isoformat(),
|
| 836 |
+
}
|
| 837 |
+
storage_manager.save_conversion_manifest(
|
| 838 |
+
task.audio_repo_key,
|
| 839 |
+
task.book_id,
|
| 840 |
+
task.engine,
|
| 841 |
+
checkpoint,
|
| 842 |
+
)
|
| 843 |
+
|
| 844 |
+
task.status = TaskStatus.MERGING
|
| 845 |
+
final_local = os.path.join(task_root, "book_full.mp3")
|
| 846 |
+
timeline_audio = AudioSegment.empty()
|
| 847 |
+
cursor_seconds = 0.0
|
| 848 |
+
for item, local_path in zip(segment_results, local_audio_paths):
|
| 849 |
+
before_ms = int(item.get("pause_before_ms", 0) or 0)
|
| 850 |
+
after_ms = int(item.get("pause_after_ms", self.book_pause_ms) or 0)
|
| 851 |
+
if before_ms > 0:
|
| 852 |
+
timeline_audio += AudioSegment.silent(duration=before_ms)
|
| 853 |
+
cursor_seconds += before_ms / 1000.0
|
| 854 |
+
item["start_time"] = round(cursor_seconds, 6)
|
| 855 |
+
audio_piece = AudioSegment.from_file(local_path)
|
| 856 |
+
item["duration_seconds"] = len(audio_piece) / 1000.0
|
| 857 |
+
timeline_audio += audio_piece
|
| 858 |
+
cursor_seconds += item["duration_seconds"]
|
| 859 |
+
item["end_time"] = round(cursor_seconds, 6)
|
| 860 |
+
if after_ms > 0:
|
| 861 |
+
timeline_audio += AudioSegment.silent(duration=after_ms)
|
| 862 |
+
cursor_seconds += after_ms / 1000.0
|
| 863 |
+
timeline_audio.export(final_local, format="mp3", bitrate=self.book_mp3_bitrate)
|
| 864 |
+
final_audio = self.validate_callback(final_local)
|
| 865 |
+
|
| 866 |
+
task.status = TaskStatus.SAVING
|
| 867 |
+
full_audio_repo_path = storage_manager.save_full_book_audio(
|
| 868 |
+
task.audio_repo_key,
|
| 869 |
+
task.book_id,
|
| 870 |
+
task.engine,
|
| 871 |
+
final_local,
|
| 872 |
+
)
|
| 873 |
+
final_manifest = {
|
| 874 |
+
"schema_version": CONVERSION_MANIFEST_SCHEMA,
|
| 875 |
+
**task.metadata,
|
| 876 |
+
"characters": list(task.segmentation_manifest.get("characters") or []),
|
| 877 |
+
"dialogue_config": dict(task.segmentation_manifest.get("dialogue_config") or {}),
|
| 878 |
+
"default_voice_settings": dict(task.segmentation_manifest.get("default_voice_settings") or {}),
|
| 879 |
+
"emotion_config": dict(task.segmentation_manifest.get("emotion_config") or {}),
|
| 880 |
+
"correction_config": dict(task.segmentation_manifest.get("correction_config") or {}),
|
| 881 |
+
"voice_name": task.voice_name,
|
| 882 |
+
"speaker_name": task.speaker_name,
|
| 883 |
+
"provider_options": task.provider_options,
|
| 884 |
+
"segmentation_manifest_path": task.segmentation_manifest.get("segmentation_manifest_path", ""),
|
| 885 |
+
"segments": segment_results,
|
| 886 |
+
"full_audio_path": full_audio_repo_path,
|
| 887 |
+
"local_full_audio_path": final_local,
|
| 888 |
+
"audio": final_audio,
|
| 889 |
+
"status": TaskStatus.DONE.value,
|
| 890 |
+
"completed_at": datetime.now(timezone.utc).isoformat(),
|
| 891 |
+
}
|
| 892 |
+
storage_manager.save_conversion_manifest(
|
| 893 |
+
task.audio_repo_key,
|
| 894 |
+
task.book_id,
|
| 895 |
+
task.engine,
|
| 896 |
+
final_manifest,
|
| 897 |
+
)
|
| 898 |
+
task.status = TaskStatus.DONE
|
| 899 |
+
task.metadata = final_manifest
|
| 900 |
+
self._report("book.conversion.completed", task, metadata={"segments_count": len(segment_results), "full_audio_path": full_audio_repo_path})
|
| 901 |
+
return final_local, final_manifest
|
| 902 |
+
|
| 903 |
+
except Exception as exc:
|
| 904 |
+
task.status = TaskStatus.FAILED
|
| 905 |
+
task.error = str(exc)
|
| 906 |
+
failed_manifest = {
|
| 907 |
+
"schema_version": CONVERSION_MANIFEST_SCHEMA,
|
| 908 |
+
**task.metadata,
|
| 909 |
+
"task_id": task.task_id,
|
| 910 |
+
"book_id": task.book_id,
|
| 911 |
+
"engine": task.engine,
|
| 912 |
+
"segments": segment_results,
|
| 913 |
+
"status": TaskStatus.FAILED.value,
|
| 914 |
+
"error": task.error,
|
| 915 |
+
"updated_at": datetime.now(timezone.utc).isoformat(),
|
| 916 |
+
}
|
| 917 |
+
try:
|
| 918 |
+
storage_manager.save_conversion_manifest(
|
| 919 |
+
task.audio_repo_key,
|
| 920 |
+
task.book_id,
|
| 921 |
+
task.engine,
|
| 922 |
+
failed_manifest,
|
| 923 |
+
)
|
| 924 |
+
except Exception:
|
| 925 |
+
pass
|
| 926 |
+
task.metadata = failed_manifest
|
| 927 |
+
self._report("book.conversion.failed", task, status="error", error=str(exc), metadata={"completed_segments": len(segment_results)})
|
| 928 |
+
raise
|
| 929 |
+
|
| 930 |
+
def get_task(self, task_id: str) -> Optional[Any]:
|
| 931 |
+
return self._tasks.get(task_id)
|
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# app.py
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
|
| 4 |
-
# الإصدار:
|
| 5 |
|
| 6 |
import os
|
| 7 |
import re
|
|
@@ -26,8 +26,8 @@ from voice_variants_registry import list_voice_profiles
|
|
| 26 |
from voice_manager import (
|
| 27 |
VoiceManager,
|
| 28 |
ENGINE_MULTILINGUAL, ENGINE_MICROSOFT_NORMAL, ENGINE_COQUI,
|
| 29 |
-
ENGINE_VISCO, ENGINE_OPENAI_TEST, ENGINE_CHATBOX,
|
| 30 |
-
VISCO_VOICES, VISCO_EFFECTS, OPENAI_TTS_VOICES, CHATBOX_VOICES,
|
| 31 |
)
|
| 32 |
|
| 33 |
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
|
@@ -106,6 +106,13 @@ def generate_florate_tts(text: str, voice: str = "alloy", instructions: str = ""
|
|
| 106 |
)
|
| 107 |
return path
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def generate_edge_audio(text: str, voice: str, prefix: str = "microsoft") -> str:
|
| 110 |
provider = prefix if prefix in {"microsoft_multilingual", "microsoft_normal", "microsoft_english"} else "microsoft_multilingual"
|
| 111 |
path, _ = system_manager.synthesize_segment(
|
|
@@ -170,6 +177,7 @@ def synth_arabic_ui(
|
|
| 170 |
openai_instructions: Optional[str],
|
| 171 |
chatbox_voice: Optional[str],
|
| 172 |
chatbox_instructions: Optional[str],
|
|
|
|
| 173 |
voice_clone_mode: str,
|
| 174 |
local_speaker: Optional[str],
|
| 175 |
uploaded_speaker: Optional[str],
|
|
@@ -183,7 +191,24 @@ def synth_arabic_ui(
|
|
| 183 |
selected_voice = ""
|
| 184 |
manager_message = ""
|
| 185 |
|
| 186 |
-
if engine ==
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
selected_speaker = (
|
| 188 |
local_speaker
|
| 189 |
if voice_clone_mode
|
|
@@ -412,6 +437,8 @@ AUDIO_BOOKS_REPO_KEY = getattr(storage_manager, "AUDIO_BOOKS_REPO_KEY", _DEFAULT
|
|
| 412 |
|
| 413 |
|
| 414 |
def _book_engine_key_from_ui(engine: str) -> str:
|
|
|
|
|
|
|
| 415 |
if engine == ENGINE_COQUI:
|
| 416 |
return "coqui_voice_clone"
|
| 417 |
if engine == ENGINE_VISCO:
|
|
@@ -437,6 +464,7 @@ def _book_engine_settings(
|
|
| 437 |
openai_instructions: Optional[str],
|
| 438 |
chatbox_voice: Optional[str],
|
| 439 |
chatbox_instructions: Optional[str],
|
|
|
|
| 440 |
voice_clone_mode: str,
|
| 441 |
local_speaker: Optional[str],
|
| 442 |
uploaded_speaker: Optional[str],
|
|
@@ -452,6 +480,8 @@ def _book_engine_settings(
|
|
| 452 |
}
|
| 453 |
language_key = mode_map.get((language_mode or "").strip(), "auto")
|
| 454 |
common_options: Dict[str, Any] = {"language_mode": language_key}
|
|
|
|
|
|
|
| 455 |
if engine_key == "coqui_voice_clone":
|
| 456 |
speaker_ar = local_speaker if voice_clone_mode == "اختر صوتاً مرجعياً موجوداً" else uploaded_speaker
|
| 457 |
speaker_en = local_speaker_en if voice_clone_mode == "اختر صوتاً مرجعياً موجوداً" else uploaded_speaker_en
|
|
@@ -704,6 +734,7 @@ def book_convert_saved_ui(
|
|
| 704 |
openai_instructions: Optional[str],
|
| 705 |
chatbox_voice: Optional[str],
|
| 706 |
chatbox_instructions: Optional[str],
|
|
|
|
| 707 |
voice_clone_mode: str,
|
| 708 |
local_speaker: Optional[str],
|
| 709 |
uploaded_speaker: Optional[str],
|
|
@@ -716,7 +747,7 @@ def book_convert_saved_ui(
|
|
| 716 |
engine_key, voice_name, speaker_name, provider_options = _book_engine_settings(
|
| 717 |
engine, language_mode, normal_voice, english_voice, multilingual_voice, visco_voice, visco_effects,
|
| 718 |
openai_voice, openai_instructions, chatbox_voice,
|
| 719 |
-
chatbox_instructions, voice_clone_mode, local_speaker, uploaded_speaker,
|
| 720 |
local_speaker_en, uploaded_speaker_en,
|
| 721 |
)
|
| 722 |
segmentation = book_preparation_manager.load_or_prepare_segmentation(
|
|
@@ -1098,6 +1129,7 @@ DIALOGUE_ENGINE_PROVIDER = {
|
|
| 1098 |
ENGINE_VISCO: "visco_tts",
|
| 1099 |
ENGINE_OPENAI_TEST: "openai_tts",
|
| 1100 |
ENGINE_CHATBOX: "chatbox_florate",
|
|
|
|
| 1101 |
}
|
| 1102 |
|
| 1103 |
|
|
@@ -1150,6 +1182,13 @@ def dialogue_engine_ui(engine_label: str):
|
|
| 1150 |
en_value = "alloy"
|
| 1151 |
ar_label = "صوت Florate للعربية"
|
| 1152 |
en_label = "صوت Florate للإنجليزية"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1153 |
elif provider == "visco_tts":
|
| 1154 |
ar_choices = VISCO_VOICES
|
| 1155 |
en_choices = VISCO_VOICES
|
|
@@ -1591,6 +1630,7 @@ with gr.Blocks(
|
|
| 1591 |
ENGINE_MULTILINGUAL,
|
| 1592 |
ENGINE_MICROSOFT_NORMAL,
|
| 1593 |
ENGINE_COQUI,
|
|
|
|
| 1594 |
ENGINE_VISCO,
|
| 1595 |
ENGINE_OPENAI_TEST,
|
| 1596 |
ENGINE_CHATBOX,
|
|
@@ -1697,6 +1737,14 @@ with gr.Blocks(
|
|
| 1697 |
)
|
| 1698 |
)
|
| 1699 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1700 |
voice_clone_mode_ar = gr.Radio(
|
| 1701 |
choices=[
|
| 1702 |
"اختر صوتاً مرجعياً موجوداً",
|
|
@@ -1810,6 +1858,7 @@ with gr.Blocks(
|
|
| 1810 |
openai_instructions_ar,
|
| 1811 |
chatbox_voice_ar,
|
| 1812 |
chatbox_instructions_ar,
|
|
|
|
| 1813 |
voice_clone_mode_ar,
|
| 1814 |
local_speaker_ar,
|
| 1815 |
uploaded_speaker_ar,
|
|
@@ -1918,6 +1967,7 @@ with gr.Blocks(
|
|
| 1918 |
ENGINE_MULTILINGUAL,
|
| 1919 |
ENGINE_MICROSOFT_NORMAL,
|
| 1920 |
ENGINE_COQUI,
|
|
|
|
| 1921 |
ENGINE_VISCO,
|
| 1922 |
ENGINE_OPENAI_TEST,
|
| 1923 |
ENGINE_CHATBOX,
|
|
@@ -2206,6 +2256,7 @@ with gr.Blocks(
|
|
| 2206 |
ENGINE_MULTILINGUAL,
|
| 2207 |
ENGINE_MICROSOFT_NORMAL,
|
| 2208 |
ENGINE_COQUI,
|
|
|
|
| 2209 |
ENGINE_VISCO,
|
| 2210 |
ENGINE_OPENAI_TEST,
|
| 2211 |
ENGINE_CHATBOX,
|
|
@@ -2360,6 +2411,12 @@ with gr.Blocks(
|
|
| 2360 |
),
|
| 2361 |
lines=2,
|
| 2362 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2363 |
book_voice_clone_mode = gr.Radio(
|
| 2364 |
choices=[
|
| 2365 |
"اختر صوتاً مرجعياً موجوداً",
|
|
@@ -2448,6 +2505,7 @@ with gr.Blocks(
|
|
| 2448 |
ENGINE_MULTILINGUAL,
|
| 2449 |
ENGINE_MICROSOFT_NORMAL,
|
| 2450 |
ENGINE_COQUI,
|
|
|
|
| 2451 |
ENGINE_VISCO,
|
| 2452 |
ENGINE_OPENAI_TEST,
|
| 2453 |
ENGINE_CHATBOX,
|
|
@@ -2648,6 +2706,7 @@ with gr.Blocks(
|
|
| 2648 |
book_openai_instructions,
|
| 2649 |
book_chatbox_voice,
|
| 2650 |
book_chatbox_instructions,
|
|
|
|
| 2651 |
book_voice_clone_mode,
|
| 2652 |
book_local_speaker,
|
| 2653 |
book_uploaded_speaker,
|
|
|
|
| 1 |
# app.py
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
|
| 4 |
+
# الإصدار: aiocr_radar_ui_v33_bark_small_2026_09_14
|
| 5 |
|
| 6 |
import os
|
| 7 |
import re
|
|
|
|
| 26 |
from voice_manager import (
|
| 27 |
VoiceManager,
|
| 28 |
ENGINE_MULTILINGUAL, ENGINE_MICROSOFT_NORMAL, ENGINE_COQUI,
|
| 29 |
+
ENGINE_VISCO, ENGINE_OPENAI_TEST, ENGINE_CHATBOX, ENGINE_BARK,
|
| 30 |
+
VISCO_VOICES, VISCO_EFFECTS, OPENAI_TTS_VOICES, CHATBOX_VOICES, BARK_VOICE_PRESETS,
|
| 31 |
)
|
| 32 |
|
| 33 |
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
|
|
|
| 106 |
)
|
| 107 |
return path
|
| 108 |
|
| 109 |
+
def generate_bark_audio(text: str, voice_preset: str = "", emotion: str = "neutral") -> str:
|
| 110 |
+
path, _ = system_manager.synthesize_segment(
|
| 111 |
+
provider="bark", text=text, voice_name=(voice_preset or ""), source_type="ui",
|
| 112 |
+
provider_options={"emotion": emotion or "neutral"},
|
| 113 |
+
)
|
| 114 |
+
return path
|
| 115 |
+
|
| 116 |
def generate_edge_audio(text: str, voice: str, prefix: str = "microsoft") -> str:
|
| 117 |
provider = prefix if prefix in {"microsoft_multilingual", "microsoft_normal", "microsoft_english"} else "microsoft_multilingual"
|
| 118 |
path, _ = system_manager.synthesize_segment(
|
|
|
|
| 177 |
openai_instructions: Optional[str],
|
| 178 |
chatbox_voice: Optional[str],
|
| 179 |
chatbox_instructions: Optional[str],
|
| 180 |
+
bark_voice: Optional[str],
|
| 181 |
voice_clone_mode: str,
|
| 182 |
local_speaker: Optional[str],
|
| 183 |
uploaded_speaker: Optional[str],
|
|
|
|
| 191 |
selected_voice = ""
|
| 192 |
manager_message = ""
|
| 193 |
|
| 194 |
+
if engine == ENGINE_BARK:
|
| 195 |
+
selected_bark_voice = (bark_voice or "").strip()
|
| 196 |
+
path, task_metadata = system_manager.process_api(
|
| 197 |
+
text=clean_text,
|
| 198 |
+
engine="bark",
|
| 199 |
+
voice_name=selected_bark_voice,
|
| 200 |
+
provider_options={"emotion": "neutral"},
|
| 201 |
+
)
|
| 202 |
+
provider = "bark"
|
| 203 |
+
selected_voice = selected_bark_voice or "auto"
|
| 204 |
+
manager_message = (
|
| 205 |
+
"\nالمقاطع عبر AITaskManager: "
|
| 206 |
+
f"{task_metadata.get('segments_count', 0)}"
|
| 207 |
+
"\nحالة المهمة: "
|
| 208 |
+
f"{task_metadata.get('status', '')}"
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
elif engine == ENGINE_COQUI:
|
| 212 |
selected_speaker = (
|
| 213 |
local_speaker
|
| 214 |
if voice_clone_mode
|
|
|
|
| 437 |
|
| 438 |
|
| 439 |
def _book_engine_key_from_ui(engine: str) -> str:
|
| 440 |
+
if engine == ENGINE_BARK:
|
| 441 |
+
return "bark"
|
| 442 |
if engine == ENGINE_COQUI:
|
| 443 |
return "coqui_voice_clone"
|
| 444 |
if engine == ENGINE_VISCO:
|
|
|
|
| 464 |
openai_instructions: Optional[str],
|
| 465 |
chatbox_voice: Optional[str],
|
| 466 |
chatbox_instructions: Optional[str],
|
| 467 |
+
bark_voice: Optional[str],
|
| 468 |
voice_clone_mode: str,
|
| 469 |
local_speaker: Optional[str],
|
| 470 |
uploaded_speaker: Optional[str],
|
|
|
|
| 480 |
}
|
| 481 |
language_key = mode_map.get((language_mode or "").strip(), "auto")
|
| 482 |
common_options: Dict[str, Any] = {"language_mode": language_key}
|
| 483 |
+
if engine_key == "bark":
|
| 484 |
+
return engine_key, (bark_voice or "").strip(), "", common_options
|
| 485 |
if engine_key == "coqui_voice_clone":
|
| 486 |
speaker_ar = local_speaker if voice_clone_mode == "اختر صوتاً مرجعياً موجوداً" else uploaded_speaker
|
| 487 |
speaker_en = local_speaker_en if voice_clone_mode == "اختر صوتاً مرجعياً موجوداً" else uploaded_speaker_en
|
|
|
|
| 734 |
openai_instructions: Optional[str],
|
| 735 |
chatbox_voice: Optional[str],
|
| 736 |
chatbox_instructions: Optional[str],
|
| 737 |
+
bark_voice: Optional[str],
|
| 738 |
voice_clone_mode: str,
|
| 739 |
local_speaker: Optional[str],
|
| 740 |
uploaded_speaker: Optional[str],
|
|
|
|
| 747 |
engine_key, voice_name, speaker_name, provider_options = _book_engine_settings(
|
| 748 |
engine, language_mode, normal_voice, english_voice, multilingual_voice, visco_voice, visco_effects,
|
| 749 |
openai_voice, openai_instructions, chatbox_voice,
|
| 750 |
+
chatbox_instructions, bark_voice, voice_clone_mode, local_speaker, uploaded_speaker,
|
| 751 |
local_speaker_en, uploaded_speaker_en,
|
| 752 |
)
|
| 753 |
segmentation = book_preparation_manager.load_or_prepare_segmentation(
|
|
|
|
| 1129 |
ENGINE_VISCO: "visco_tts",
|
| 1130 |
ENGINE_OPENAI_TEST: "openai_tts",
|
| 1131 |
ENGINE_CHATBOX: "chatbox_florate",
|
| 1132 |
+
ENGINE_BARK: "bark",
|
| 1133 |
}
|
| 1134 |
|
| 1135 |
|
|
|
|
| 1182 |
en_value = "alloy"
|
| 1183 |
ar_label = "صوت Florate للعربية"
|
| 1184 |
en_label = "صوت Florate للإنجليزية"
|
| 1185 |
+
elif provider == "bark":
|
| 1186 |
+
ar_choices = BARK_VOICE_PRESETS
|
| 1187 |
+
en_choices = BARK_VOICE_PRESETS
|
| 1188 |
+
ar_value = ""
|
| 1189 |
+
en_value = ""
|
| 1190 |
+
ar_label = "Bark preset للعربية — يفضّل تلقائي"
|
| 1191 |
+
en_label = "Bark preset للإنجليزية"
|
| 1192 |
elif provider == "visco_tts":
|
| 1193 |
ar_choices = VISCO_VOICES
|
| 1194 |
en_choices = VISCO_VOICES
|
|
|
|
| 1630 |
ENGINE_MULTILINGUAL,
|
| 1631 |
ENGINE_MICROSOFT_NORMAL,
|
| 1632 |
ENGINE_COQUI,
|
| 1633 |
+
ENGINE_BARK,
|
| 1634 |
ENGINE_VISCO,
|
| 1635 |
ENGINE_OPENAI_TEST,
|
| 1636 |
ENGINE_CHATBOX,
|
|
|
|
| 1737 |
)
|
| 1738 |
)
|
| 1739 |
|
| 1740 |
+
bark_voice_ar = gr.Dropdown(
|
| 1741 |
+
choices=BARK_VOICE_PRESETS,
|
| 1742 |
+
value="",
|
| 1743 |
+
label="Bark voice preset — تلقائي أفضل للعربية",
|
| 1744 |
+
visible=True,
|
| 1745 |
+
allow_custom_value=True,
|
| 1746 |
+
)
|
| 1747 |
+
|
| 1748 |
voice_clone_mode_ar = gr.Radio(
|
| 1749 |
choices=[
|
| 1750 |
"اختر صوتاً مرجعياً موجوداً",
|
|
|
|
| 1858 |
openai_instructions_ar,
|
| 1859 |
chatbox_voice_ar,
|
| 1860 |
chatbox_instructions_ar,
|
| 1861 |
+
bark_voice_ar,
|
| 1862 |
voice_clone_mode_ar,
|
| 1863 |
local_speaker_ar,
|
| 1864 |
uploaded_speaker_ar,
|
|
|
|
| 1967 |
ENGINE_MULTILINGUAL,
|
| 1968 |
ENGINE_MICROSOFT_NORMAL,
|
| 1969 |
ENGINE_COQUI,
|
| 1970 |
+
ENGINE_BARK,
|
| 1971 |
ENGINE_VISCO,
|
| 1972 |
ENGINE_OPENAI_TEST,
|
| 1973 |
ENGINE_CHATBOX,
|
|
|
|
| 2256 |
ENGINE_MULTILINGUAL,
|
| 2257 |
ENGINE_MICROSOFT_NORMAL,
|
| 2258 |
ENGINE_COQUI,
|
| 2259 |
+
ENGINE_BARK,
|
| 2260 |
ENGINE_VISCO,
|
| 2261 |
ENGINE_OPENAI_TEST,
|
| 2262 |
ENGINE_CHATBOX,
|
|
|
|
| 2411 |
),
|
| 2412 |
lines=2,
|
| 2413 |
)
|
| 2414 |
+
book_bark_voice = gr.Dropdown(
|
| 2415 |
+
choices=BARK_VOICE_PRESETS,
|
| 2416 |
+
value="",
|
| 2417 |
+
label="Bark voice preset — تلقائي أفضل للعربية",
|
| 2418 |
+
allow_custom_value=True,
|
| 2419 |
+
)
|
| 2420 |
book_voice_clone_mode = gr.Radio(
|
| 2421 |
choices=[
|
| 2422 |
"اختر صوتاً مرجعياً موجوداً",
|
|
|
|
| 2505 |
ENGINE_MULTILINGUAL,
|
| 2506 |
ENGINE_MICROSOFT_NORMAL,
|
| 2507 |
ENGINE_COQUI,
|
| 2508 |
+
ENGINE_BARK,
|
| 2509 |
ENGINE_VISCO,
|
| 2510 |
ENGINE_OPENAI_TEST,
|
| 2511 |
ENGINE_CHATBOX,
|
|
|
|
| 2706 |
book_openai_instructions,
|
| 2707 |
book_chatbox_voice,
|
| 2708 |
book_chatbox_instructions,
|
| 2709 |
+
book_bark_voice,
|
| 2710 |
book_voice_clone_mode,
|
| 2711 |
book_local_speaker,
|
| 2712 |
book_uploaded_speaker,
|
engine_capabilities.json
CHANGED
|
@@ -56,8 +56,8 @@
|
|
| 56 |
"sentence_only": true,
|
| 57 |
"batch_size_segments": 25,
|
| 58 |
"supports_expression_tags": true,
|
| 59 |
-
"enabled":
|
| 60 |
-
"note": "
|
| 61 |
}
|
| 62 |
}
|
| 63 |
}
|
|
|
|
| 56 |
"sentence_only": true,
|
| 57 |
"batch_size_segments": 25,
|
| 58 |
"supports_expression_tags": true,
|
| 59 |
+
"enabled": true,
|
| 60 |
+
"note": "Bark Small مفعّل عبر Transformers مع مقاطع قصيرة وحماية ذاكرة GPU."
|
| 61 |
}
|
| 62 |
}
|
| 63 |
}
|
tts_engines_registry.py
CHANGED
|
@@ -181,6 +181,30 @@ TTS_ENGINES_REGISTRY: Dict[str, TTSEngineProfile] = {
|
|
| 181 |
},
|
| 182 |
),
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
# الاسم canonical أصبح coqui_voice_clone للتوافق مع AITaskManager.
|
| 185 |
# VoiceManager يقبل أيضاً coqui_xtts كـalias للخلف.
|
| 186 |
"coqui_voice_clone": TTSEngineProfile(
|
|
@@ -206,6 +230,9 @@ ALIASES: Dict[str, str] = {
|
|
| 206 |
"chatbox_florate_tts": "chatbox_florate",
|
| 207 |
"visco": "visco_tts",
|
| 208 |
"openai": "openai_tts",
|
|
|
|
|
|
|
|
|
|
| 209 |
"microsoft_arabic": "microsoft_normal",
|
| 210 |
"edge_arabic": "microsoft_normal",
|
| 211 |
}
|
|
|
|
| 181 |
},
|
| 182 |
),
|
| 183 |
|
| 184 |
+
|
| 185 |
+
"bark": TTSEngineProfile(
|
| 186 |
+
provider="bark",
|
| 187 |
+
display_name="Bark Small (Suno)",
|
| 188 |
+
capabilities=EngineCapabilities(
|
| 189 |
+
default_format="wav",
|
| 190 |
+
max_concurrency=1,
|
| 191 |
+
supports_effects=True,
|
| 192 |
+
supports_multilingual=True,
|
| 193 |
+
),
|
| 194 |
+
emotion_delivery="effects",
|
| 195 |
+
provider_emotion_map={
|
| 196 |
+
"neutral": [],
|
| 197 |
+
"happy": ["[laughs]"],
|
| 198 |
+
"sad": ["[sighs]"],
|
| 199 |
+
"angry": [],
|
| 200 |
+
"excited": [],
|
| 201 |
+
"calm": [],
|
| 202 |
+
"soft": [],
|
| 203 |
+
"whispering": ["[whispers]"],
|
| 204 |
+
"fearful": ["[gasps]"],
|
| 205 |
+
},
|
| 206 |
+
),
|
| 207 |
+
|
| 208 |
# الاسم canonical أصبح coqui_voice_clone للتوافق مع AITaskManager.
|
| 209 |
# VoiceManager يقبل أيضاً coqui_xtts كـalias للخلف.
|
| 210 |
"coqui_voice_clone": TTSEngineProfile(
|
|
|
|
| 230 |
"chatbox_florate_tts": "chatbox_florate",
|
| 231 |
"visco": "visco_tts",
|
| 232 |
"openai": "openai_tts",
|
| 233 |
+
"bark_tts": "bark",
|
| 234 |
+
"suno_bark": "bark",
|
| 235 |
+
"bark_small": "bark",
|
| 236 |
"microsoft_arabic": "microsoft_normal",
|
| 237 |
"edge_arabic": "microsoft_normal",
|
| 238 |
}
|
voice_manager.py
CHANGED
|
@@ -29,7 +29,7 @@ from tts_engines_registry import get_tts_engine
|
|
| 29 |
from voice_variants_registry import resolve_reference_audio
|
| 30 |
|
| 31 |
|
| 32 |
-
VERSION = "
|
| 33 |
|
| 34 |
# توافق Coqui checkpoints القديمة
|
| 35 |
_original_torch_load = torch.load
|
|
@@ -49,6 +49,7 @@ ENGINE_COQUI = "Coqui XTTS-v2 — استنساخ الصوت (CPU / ZeroGPU)"
|
|
| 49 |
ENGINE_VISCO = "ViSCO TTS — أصوات بشرية + مؤثرات (تجريبي)"
|
| 50 |
ENGINE_OPENAI_TEST = "OpenAI TTS — تجربة أصوات يدوية داخل الرادار"
|
| 51 |
ENGINE_CHATBOX = "ChatBox / Florate TTS — Experimental"
|
|
|
|
| 52 |
|
| 53 |
VISCO_VOICES: List[Tuple[str, str]] = [
|
| 54 |
("سارة - Sarah", "sarah"), ("أدريان - Adrian", "adrian"),
|
|
@@ -74,6 +75,12 @@ OPENAI_TTS_VOICES: List[Tuple[str, str]] = [
|
|
| 74 |
]
|
| 75 |
CHATBOX_VOICES = list(OPENAI_TTS_VOICES)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
class VoiceManager:
|
| 79 |
"""مالك محركات الصوت. لا يقرر منطق الكتاب أو الخزنة أو سير المهمة."""
|
|
@@ -108,6 +115,13 @@ class VoiceManager:
|
|
| 108 |
self.min_audio_duration_ms = int(os.getenv("MIN_AUDIO_DURATION_MS", "500"))
|
| 109 |
self.chatbox_device_id_file = os.getenv("CHATBOX_DEVICE_ID_FILE", "chatbox_device_id.txt").strip()
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
self._edge_loop: Optional[asyncio.AbstractEventLoop] = None
|
| 112 |
self._edge_thread: Optional[threading.Thread] = None
|
| 113 |
self._edge_loop_lock = threading.Lock()
|
|
@@ -478,6 +492,107 @@ class VoiceManager:
|
|
| 478 |
duration_ms=(time.perf_counter()-start)*1000, status="error", error=str(exc))
|
| 479 |
raise
|
| 480 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
def split_text(self, text: str) -> List[str]:
|
| 482 |
clean = (text or "").strip()
|
| 483 |
if not clean: return []
|
|
@@ -589,6 +704,9 @@ class VoiceManager:
|
|
| 589 |
"chatbox": "chatbox_florate",
|
| 590 |
"florate": "chatbox_florate",
|
| 591 |
"chatbox_florate_tts": "chatbox_florate",
|
|
|
|
|
|
|
|
|
|
| 592 |
}
|
| 593 |
key = aliases.get(key, key)
|
| 594 |
options = dict(provider_options or {})
|
|
@@ -601,6 +719,15 @@ class VoiceManager:
|
|
| 601 |
except KeyError as exc:
|
| 602 |
raise RuntimeError(f"مزود صوت غير مسجل في TTS Engine Registry: {key}") from exc
|
| 603 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
if key == "coqui_voice_clone":
|
| 605 |
reference = self.resolve_coqui_reference(
|
| 606 |
voice_id=voice_name,
|
|
|
|
| 29 |
from voice_variants_registry import resolve_reference_audio
|
| 30 |
|
| 31 |
|
| 32 |
+
VERSION = "voice_manager_v3_bark_small_registry_integration"
|
| 33 |
|
| 34 |
# توافق Coqui checkpoints القديمة
|
| 35 |
_original_torch_load = torch.load
|
|
|
|
| 49 |
ENGINE_VISCO = "ViSCO TTS — أصوات بشرية + مؤثرات (تجريبي)"
|
| 50 |
ENGINE_OPENAI_TEST = "OpenAI TTS — تجربة أصوات يدوية داخل الرادار"
|
| 51 |
ENGINE_CHATBOX = "ChatBox / Florate TTS — Experimental"
|
| 52 |
+
ENGINE_BARK = "Bark Small — تعبيرات وصوت توليدي (CPU / ZeroGPU)"
|
| 53 |
|
| 54 |
VISCO_VOICES: List[Tuple[str, str]] = [
|
| 55 |
("سارة - Sarah", "sarah"), ("أدريان - Adrian", "adrian"),
|
|
|
|
| 75 |
]
|
| 76 |
CHATBOX_VOICES = list(OPENAI_TTS_VOICES)
|
| 77 |
|
| 78 |
+
# Bark لا يملك أصواتاً عربية رسمية مسبقة. خيار "تلقائي" هو الافتراضي،
|
| 79 |
+
# وتبقى presets الإنجليزية متاحة للتجربة والحوار الإنجليزي.
|
| 80 |
+
BARK_VOICE_PRESETS: List[Tuple[str, str]] = [("تلقائي / بدون preset", "")] + [
|
| 81 |
+
(f"Bark English Speaker {i}", f"v2/en_speaker_{i}") for i in range(10)
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
|
| 85 |
class VoiceManager:
|
| 86 |
"""مالك محركات الصوت. لا يقرر منطق الكتاب أو الخزنة أو سير المهمة."""
|
|
|
|
| 115 |
self.min_audio_duration_ms = int(os.getenv("MIN_AUDIO_DURATION_MS", "500"))
|
| 116 |
self.chatbox_device_id_file = os.getenv("CHATBOX_DEVICE_ID_FILE", "chatbox_device_id.txt").strip()
|
| 117 |
|
| 118 |
+
# Bark Small عبر Transformers الموجود أصلاً في البيئة. لا نثبت حزمة bark مستقلة.
|
| 119 |
+
self.bark_model_id = os.getenv("BARK_MODEL_ID", "suno/bark-small").strip() or "suno/bark-small"
|
| 120 |
+
self.bark_gpu_duration = int(os.getenv("BARK_GPU_DURATION_SECONDS", "180"))
|
| 121 |
+
self._bark_processor = None
|
| 122 |
+
self._bark_model = None
|
| 123 |
+
self._bark_lock = threading.RLock()
|
| 124 |
+
|
| 125 |
self._edge_loop: Optional[asyncio.AbstractEventLoop] = None
|
| 126 |
self._edge_thread: Optional[threading.Thread] = None
|
| 127 |
self._edge_loop_lock = threading.Lock()
|
|
|
|
| 492 |
duration_ms=(time.perf_counter()-start)*1000, status="error", error=str(exc))
|
| 493 |
raise
|
| 494 |
|
| 495 |
+
def _load_bark(self):
|
| 496 |
+
if self._bark_model is not None and self._bark_processor is not None:
|
| 497 |
+
return self._bark_processor, self._bark_model
|
| 498 |
+
with self._bark_lock:
|
| 499 |
+
if self._bark_model is None or self._bark_processor is None:
|
| 500 |
+
try:
|
| 501 |
+
from transformers import AutoProcessor, BarkModel
|
| 502 |
+
except Exception as exc:
|
| 503 |
+
raise RuntimeError("Transformers الحالي لا يوفّر BarkModel/AutoProcessor.") from exc
|
| 504 |
+
self._bark_processor = AutoProcessor.from_pretrained(self.bark_model_id)
|
| 505 |
+
self._bark_model = BarkModel.from_pretrained(self.bark_model_id)
|
| 506 |
+
self._bark_model.eval()
|
| 507 |
+
return self._bark_processor, self._bark_model
|
| 508 |
+
|
| 509 |
+
@staticmethod
|
| 510 |
+
def _bark_expression_prefix(emotion: Any, effects: Optional[Any] = None) -> str:
|
| 511 |
+
key = VoiceManager._normalize_emotion(emotion)
|
| 512 |
+
tags = {
|
| 513 |
+
"happy": "[laughs]",
|
| 514 |
+
"sad": "[sighs]",
|
| 515 |
+
"fearful": "[gasps]",
|
| 516 |
+
"whispering": "[whispers]",
|
| 517 |
+
}
|
| 518 |
+
selected: List[str] = []
|
| 519 |
+
if effects:
|
| 520 |
+
raw = effects if isinstance(effects, (list, tuple, set)) else [effects]
|
| 521 |
+
allowed = {"[laughs]", "[laughter]", "[sighs]", "[gasps]", "[whispers]", "[clears throat]"}
|
| 522 |
+
for value in raw:
|
| 523 |
+
token = str(value or "").strip()
|
| 524 |
+
if token in allowed and token not in selected:
|
| 525 |
+
selected.append(token)
|
| 526 |
+
implied = tags.get(key, "")
|
| 527 |
+
if implied and implied not in selected:
|
| 528 |
+
selected.append(implied)
|
| 529 |
+
return " ".join(selected).strip()
|
| 530 |
+
|
| 531 |
+
@spaces.GPU(duration=180)
|
| 532 |
+
def generate_bark_audio(
|
| 533 |
+
self,
|
| 534 |
+
text: str,
|
| 535 |
+
voice_preset: str = "",
|
| 536 |
+
*,
|
| 537 |
+
provider_options: Optional[Dict[str, Any]] = None,
|
| 538 |
+
trace_id: str = "",
|
| 539 |
+
segment_id: str = "",
|
| 540 |
+
) -> str:
|
| 541 |
+
clean_text = (text or "").strip()
|
| 542 |
+
if not clean_text:
|
| 543 |
+
raise ValueError("النص فارغ.")
|
| 544 |
+
options = dict(provider_options or {})
|
| 545 |
+
prefix = self._bark_expression_prefix(options.get("emotion"), options.get("effects"))
|
| 546 |
+
prompt_text = f"{prefix} {clean_text}".strip() if prefix else clean_text
|
| 547 |
+
preset = (voice_preset or options.get("voice_preset") or "").strip()
|
| 548 |
+
path = os.path.join(self.voices_dir, f"bark_{uuid.uuid4().hex}.wav")
|
| 549 |
+
start = time.perf_counter()
|
| 550 |
+
with self._bark_lock:
|
| 551 |
+
processor = model = None
|
| 552 |
+
try:
|
| 553 |
+
processor, model = self._load_bark()
|
| 554 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 555 |
+
model.to(device)
|
| 556 |
+
processor_kwargs: Dict[str, Any] = {"text": prompt_text, "return_tensors": "pt"}
|
| 557 |
+
if preset:
|
| 558 |
+
processor_kwargs["voice_preset"] = preset
|
| 559 |
+
inputs = processor(**processor_kwargs)
|
| 560 |
+
moved = {}
|
| 561 |
+
for key, value in dict(inputs).items():
|
| 562 |
+
moved[key] = value.to(device) if hasattr(value, "to") else value
|
| 563 |
+
with torch.inference_mode():
|
| 564 |
+
generated = model.generate(**moved)
|
| 565 |
+
audio = generated[0].detach().float().cpu().numpy()
|
| 566 |
+
sample_rate = int(getattr(model.generation_config, "sample_rate", 24000) or 24000)
|
| 567 |
+
from scipy.io.wavfile import write as wav_write
|
| 568 |
+
wav_write(path, sample_rate, audio)
|
| 569 |
+
info = self.validate_audio_file(path)
|
| 570 |
+
self._report(
|
| 571 |
+
"voice.segment.completed", trace_id=trace_id, engine="bark", segment_id=segment_id,
|
| 572 |
+
duration_ms=(time.perf_counter()-start)*1000, input_size_bytes=len(clean_text.encode("utf-8")),
|
| 573 |
+
output_size_bytes=info["size_bytes"],
|
| 574 |
+
metadata={"audio_duration_seconds": info["duration_seconds"], "model_id": self.bark_model_id, "voice_preset": preset, "emotion": self._normalize_emotion(options.get("emotion"))},
|
| 575 |
+
)
|
| 576 |
+
return path
|
| 577 |
+
except Exception as exc:
|
| 578 |
+
if os.path.exists(path):
|
| 579 |
+
try: os.remove(path)
|
| 580 |
+
except OSError: pass
|
| 581 |
+
self._report(
|
| 582 |
+
"voice.segment.failed", trace_id=trace_id, engine="bark", segment_id=segment_id,
|
| 583 |
+
duration_ms=(time.perf_counter()-start)*1000, status="error", error=str(exc),
|
| 584 |
+
)
|
| 585 |
+
raise
|
| 586 |
+
finally:
|
| 587 |
+
# حماية Coqui/ZeroGPU: لا نترك Bark ممسكاً بذاكرة GPU بين الطلبات.
|
| 588 |
+
if model is not None:
|
| 589 |
+
try: model.to("cpu")
|
| 590 |
+
except Exception: pass
|
| 591 |
+
gc.collect()
|
| 592 |
+
if torch.cuda.is_available():
|
| 593 |
+
try: torch.cuda.empty_cache()
|
| 594 |
+
except Exception: pass
|
| 595 |
+
|
| 596 |
def split_text(self, text: str) -> List[str]:
|
| 597 |
clean = (text or "").strip()
|
| 598 |
if not clean: return []
|
|
|
|
| 704 |
"chatbox": "chatbox_florate",
|
| 705 |
"florate": "chatbox_florate",
|
| 706 |
"chatbox_florate_tts": "chatbox_florate",
|
| 707 |
+
"bark_tts": "bark",
|
| 708 |
+
"suno_bark": "bark",
|
| 709 |
+
"bark_small": "bark",
|
| 710 |
}
|
| 711 |
key = aliases.get(key, key)
|
| 712 |
options = dict(provider_options or {})
|
|
|
|
| 719 |
except KeyError as exc:
|
| 720 |
raise RuntimeError(f"مزود صوت غير مسجل في TTS Engine Registry: {key}") from exc
|
| 721 |
|
| 722 |
+
if key == "bark":
|
| 723 |
+
return self.generate_bark_audio(
|
| 724 |
+
text,
|
| 725 |
+
voice_name,
|
| 726 |
+
provider_options=options,
|
| 727 |
+
trace_id=trace_id,
|
| 728 |
+
segment_id=segment_id,
|
| 729 |
+
)
|
| 730 |
+
|
| 731 |
if key == "coqui_voice_clone":
|
| 732 |
reference = self.resolve_coqui_reference(
|
| 733 |
voice_id=voice_name,
|