diff --git a/orbisubs.py b/orbisubs.py index 94b74e9..b571d9c 100755 --- a/orbisubs.py +++ b/orbisubs.py @@ -106,6 +106,25 @@ class MotorLocal: return [(w.start, w.end, w.word) for s in segs for w in (s.words or [])] +def post_reintento(sesion, url, **kw): + """POST con un solo reintento si se cortó la conexión. + + Caso real (T13, 2026-11-16 22:58:59): RemoteDisconnected en un pedido a whisper.cpp, + sin ningún error en gpu02. Mandamos un pedido cada ~6 s por una conexión keep-alive y + el server (cpp-httplib) cierra las inactivas a los ~5 s. Si el pedido sale justo + cuando el server cierra, llega a un socket muerto. Reintentar abre una conexión nueva. + Es seguro: /inference y chat/completions no cambian estado en el server. + Un timeout NO se reintenta: ahí el motor está lento y repetir lo carga más.""" + import requests + try: + return sesion.post(url, **kw) + except requests.exceptions.ConnectionError as e: + if isinstance(e, requests.exceptions.Timeout): + raise + log("http_retry", url=url, err=str(e)[:200]) + return sesion.post(url, **kw) + + class MotorHTTP: """whisper.cpp server remoto (gpu02, tarea 076-agregado): solo POST /inference, un pedido a la vez (este proceso es secuencial). Nunca /load: cambia el modelo @@ -129,8 +148,8 @@ class MotorHTTP: data["prompt"] = prompt t = time.time() try: - r = self.s.post(self.url, files={"file": ("a.wav", b.getvalue(), "audio/wav")}, data=data, - timeout=self.timeout) + r = post_reintento(self.s, self.url, files={"file": ("a.wav", b.getvalue(), "audio/wav")}, + data=data, timeout=self.timeout) r.raise_for_status() d = r.json() finally: diff --git a/orbisubs_tr.py b/orbisubs_tr.py index 79d0104..a98f614 100644 --- a/orbisubs_tr.py +++ b/orbisubs_tr.py @@ -18,7 +18,7 @@ import argparse, json, os, statistics, time import requests -from orbisubs import escribir_atomico, log, partir_lineas, ts_vtt +from orbisubs import escribir_atomico, log, partir_lineas, post_reintento, ts_vtt SISTEMA = ("You translate live Chilean Spanish TV captions into natural, concise English. " "Keep proper names, place names and Chilean acronyms (e.g. Carabineros, PDI, SII, AFP, Fonasa) as they are. " @@ -42,9 +42,10 @@ class Traductor: t = time.time() self.error = None try: - r = self.s.post(self.url, json={"model": self.modelo, "temperature": 0, "max_tokens": 160, - "chat_template_kwargs": {"enable_thinking": False}, - "messages": msgs}, timeout=self.timeout) + # Mismo keep-alive que con whisper: un corte de conexión se reintenta una vez. + r = post_reintento(self.s, self.url, json={"model": self.modelo, "temperature": 0, "max_tokens": 160, + "chat_template_kwargs": {"enable_thinking": False}, + "messages": msgs}, timeout=self.timeout) r.raise_for_status() en = r.json()["choices"][0]["message"]["content"].strip().strip('"').strip() except Exception as e: