File size: 12,808 Bytes
04a4cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# api_client.py  (UI - Space "veureu")
import os
import requests
import base64
import zipfile
import io
from typing import Iterable, Dict, Any

class APIClient:
    """

    Cliente para 'engine':

      POST /jobs                       -> {"job_id": "..."}

      GET  /jobs/{job_id}/status       -> {"status": "queued|processing|done|failed", ...}

      GET  /jobs/{job_id}/result       -> JobResult {"book": {...}, "une": {...}, ...}

    """
    def __init__(self, base_url: str, use_mock: bool = False, data_dir: str | None = None, token: str | None = None, timeout: int = 180):
        self.base_url = base_url.rstrip("/")
        # La URL para el servicio TTS es la misma que la base_url para los Spaces de HF
        self.tts_url = self.base_url
        self.use_mock = use_mock
        self.data_dir = data_dir
        self.timeout = timeout
        self.session = requests.Session()
        # Permite inyectar el token del engine via secret/var en el Space UI
        token = token or os.getenv("API_SHARED_TOKEN")
        if token:
            self.session.headers.update({"Authorization": f"Bearer {token}"})

    # ---- modo real (engine) ----
    def _post_jobs(self, video_path: str, modes: Iterable[str]) -> Dict[str, Any]:
        url = f"{self.base_url}/jobs"
        files = {"file": (os.path.basename(video_path), open(video_path, "rb"), "application/octet-stream")}
        data = {"modes": ",".join(modes)}
        r = self.session.post(url, files=files, data=data, timeout=self.timeout)
        r.raise_for_status()
        return r.json()  # {"job_id": ...}

    def _get_status(self, job_id: str) -> Dict[str, Any]:
        url = f"{self.base_url}/jobs/{job_id}/status"
        r = self.session.get(url, timeout=self.timeout)
        r.raise_for_status()
        return r.json()

    def _get_result(self, job_id: str) -> Dict[str, Any]:
        url = f"{self.base_url}/jobs/{job_id}/result"
        r = self.session.get(url, timeout=self.timeout)
        r.raise_for_status()
        return r.json()  # JobResult (book/une/... según engine)

    # ---- API que usa streamlit_app.py ----
    def process_video(self, video_path: str, modes: Iterable[str]) -> Dict[str, Any]:
        """Devuelve {"job_id": "..."}"""
        if self.use_mock:
            return {"job_id": "mock-123"}
        return self._post_jobs(video_path, modes)

    def get_job(self, job_id: str) -> Dict[str, Any]:
        """

        La UI espera algo del estilo:

          {"status":"done","results":{"book":{...},"une":{...}}}

        Adaptamos la respuesta de /result del engine a ese contrato.

        """
        if self.use_mock:
            # resultado inmediato de prueba
            return {
                "status": "done",
                "results": {
                    "book": {"text": "Text d'exemple (book)", "mp3_bytes": b""},
                    "une":  {"srt": "1\n00:00:00,000 --> 00:00:01,000\nExemple UNE\n", "mp3_bytes": b""},
                }
            }

        # Opción 1: si quieres chequear estado primero
        st = self._get_status(job_id)
        if st.get("status") in {"queued", "processing"}:
            return {"status": st.get("status", "queued")}

        res = self._get_result(job_id)
        # 'res' viene como JobResult del engine: {"book": {...}, "une": {...}, ...}
        # La UI consume 'results' con claves "book"/"une"; si tus claves ya son iguales, pasa directo:
        results = {}
        if "book" in res:
            results["book"] = {
                "text": res["book"].get("text"),
                # si sirves URLs en el engine, podrías mapear "book_mp3_url" a descarga directa;
                # la UI actual espera "mp3_bytes" sólo en mock, así que lo dejamos fuera.
            }
        if "une" in res:
            results["une"] = {
                "srt": res["une"].get("srt"),
            }
        # Si res incluye "characters"/"metrics", la UI también los guarda:
        for k in ("book", "une"):
            if k in res:
                if "characters" in res[k]:
                    results[k]["characters"] = res[k]["characters"]
                if "metrics" in res[k]:
                    results[k]["metrics"] = res[k]["metrics"]

        status = "done" if results else st.get("status", "unknown")
        return {"status": status, "results": results}


    def tts_matxa(self, text: str, voice: str = "central/grau") -> dict:
        """

        Llama al space 'tts' para sintetizar audio.



        Args:

            text (str): Texto a sintetizar.

            voice (str): Voz de Matxa a usar (p.ej. 'central/alvocat').



        Returns:

            dict: {'mp3_data_url': 'data:audio/mpeg;base64,...'}

        """
        if not self.tts_url:
            raise ValueError("La URL del servei TTS no està configurada (API_TTS_URL)")

        url = f"{self.tts_url.rstrip('/')}/tts/text"
        data = {
            "texto": text,
            "voice": voice,
            "formato": "mp3"
        }
        
        try:
            r = requests.post(url, data=data, timeout=self.timeout)
            r.raise_for_status()
            
            # Devolver los bytes directamente para que el cliente los pueda concatenar
            return {"mp3_bytes": r.content}

        except requests.exceptions.RequestException as e:
            print(f"Error cridant a TTS: {e}")
            # Devolvemos un diccionario con error para que la UI lo muestre
            return {"error": str(e)}


    def rebuild_video_with_ad(self, video_path: str, srt_path: str) -> dict:
        """

        Llama al space 'tts' para reconstruir un vídeo con audiodescripció a partir de un SRT.

        El servidor devuelve un ZIP, y de ahí extraemos el MP4 final.

        """
        if not self.tts_url:
            raise ValueError("La URL del servei TTS no està configurada (API_TTS_URL)")

        url = f"{self.tts_url.rstrip('/')}/tts/srt"
        
        try:
            files = {
                'video': (os.path.basename(video_path), open(video_path, 'rb'), 'video/mp4'),
                'srt': (os.path.basename(srt_path), open(srt_path, 'rb'), 'application/x-subrip')
            }
            data = {"include_final_mp4": 1}
            
            r = requests.post(url, files=files, data=data, timeout=self.timeout * 5)
            r.raise_for_status()
            
            # El servidor devuelve un ZIP, lo procesamos en memoria
            with zipfile.ZipFile(io.BytesIO(r.content)) as z:
                # Buscamos el archivo .mp4 dentro del ZIP
                for filename in z.namelist():
                    if filename.endswith('.mp4'):
                        video_bytes = z.read(filename)
                        return {"video_bytes": video_bytes}
            
            # Si no se encuentra el MP4 en el ZIP
            return {"error": "No se encontró el archivo de vídeo MP4 en la respuesta del servidor."}

        except requests.exceptions.RequestException as e:
            print(f"Error cridant a la reconstrucció de vídeo: {e}")
            return {"error": str(e)}
        except zipfile.BadZipFile:
            return {"error": "La respuesta del servidor no fue un archivo ZIP válido."}


    def create_initial_casting(self, video_path: str = None, video_bytes: bytes = None, video_name: str = None, epsilon: float = 0.5, min_cluster_size: int = 2) -> dict:
        """

        Llama al endpoint del space 'engine' para crear el 'initial casting'.



        Envía el vídeo recién importado como archivo y los parámetros de clustering.

        

        Args:

            video_path: Path to video file (if reading from disk)

            video_bytes: Video file bytes (if already in memory)

            video_name: Name for the video file

            epsilon: Clustering epsilon parameter

            min_cluster_size: Minimum cluster size parameter

        """
        url = f"{self.base_url}/create_initial_casting"
        try:
            # Prepare file data
            if video_bytes:
                filename = video_name or "video.mp4"
                files = {
                    "video": (filename, video_bytes, "video/mp4"),
                }
            elif video_path:
                with open(video_path, "rb") as f:
                    files = {
                        "video": (os.path.basename(video_path), f.read(), "video/mp4"),
                    }
            else:
                return {"error": "Either video_path or video_bytes must be provided"}
            
            data = {
                "epsilon": str(epsilon),
                "min_cluster_size": str(min_cluster_size),
            }
            r = self.session.post(url, files=files, data=data, timeout=self.timeout * 5)
            r.raise_for_status()
            return r.json() if r.headers.get("content-type", "").startswith("application/json") else {"ok": True}
        except requests.exceptions.RequestException as e:
            return {"error": str(e)}
        except Exception as e:
            return {"error": f"Unexpected error: {str(e)}"}

    def generate_audio_from_text_file(self, text_content: str, voice: str = "central/grau") -> dict:
        """

        Genera un único MP3 a partir de un texto largo, usando el endpoint de SRT.

        1. Convierte el texto en un SRT falso.

        2. Llama a /tts/srt con el SRT.

        3. Extrae el 'ad_master.mp3' del ZIP resultante.

        """
        if not self.tts_url:
            raise ValueError("La URL del servei TTS no està configurada (API_TTS_URL)")

        # 1. Crear un SRT falso en memoria
        srt_content = ""
        start_time = 0
        for i, line in enumerate(text_content.strip().split('\n')):
            line = line.strip()
            if not line:
                continue
            # Asignar 5 segundos por línea, un valor simple
            end_time = start_time + 5
            
            def format_time(seconds):
                h = int(seconds / 3600)
                m = int((seconds % 3600) / 60)
                s = int(seconds % 60)
                ms = int((seconds - int(seconds)) * 1000)
                return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"

            srt_content += f"{i+1}\n"
            srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
            srt_content += f"{line}\n\n"
            start_time = end_time

        if not srt_content:
            return {"error": "El texto proporcionado estaba vacío o no se pudo procesar."}

        # 2. Llamar al endpoint /tts/srt
        url = f"{self.tts_url.rstrip('/')}/tts/srt"
        try:
            files = {
                'srt': ('fake_ad.srt', srt_content, 'application/x-subrip')
            }
            data = {"voice": voice, "ad_format": "mp3"}
            
            r = requests.post(url, files=files, data=data, timeout=self.timeout * 5)
            r.raise_for_status()

            # 3. Extraer 'ad_master.mp3' del ZIP
            with zipfile.ZipFile(io.BytesIO(r.content)) as z:
                for filename in z.namelist():
                    if filename == 'ad_master.mp3':
                        mp3_bytes = z.read(filename)
                        return {"mp3_bytes": mp3_bytes}
            
            return {"error": "No se encontró 'ad_master.mp3' en la respuesta del servidor."}

        except requests.exceptions.RequestException as e:
            return {"error": f"Error llamando a la API de SRT: {e}"}
        except zipfile.BadZipFile:
            return {"error": "La respuesta del servidor no fue un archivo ZIP válido."}


    def tts_long_text(self, text: str, voice: str = "central/grau") -> dict:
        """

        Llama al endpoint '/tts/text_long' para sintetizar un texto largo.

        La API se encarga de todo el procesamiento.

        """
        if not self.tts_url:
            raise ValueError("La URL del servei TTS no està configurada (API_TTS_URL)")

        url = f"{self.tts_url.rstrip('/')}/tts/text_long"
        data = {
            "texto": text,
            "voice": voice,
            "formato": "mp3"
        }
        
        try:
            # Usamos un timeout más largo por si el texto es muy extenso
            r = requests.post(url, data=data, timeout=self.timeout * 10)
            r.raise_for_status()
            return {"mp3_bytes": r.content}

        except requests.exceptions.RequestException as e:
            print(f"Error cridant a TTS per a text llarg: {e}")
            return {"error": str(e)}