Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
Centralita Torá — HuggingFace Space (Gradio)
|
| 4 |
Leer hebreo real + español fluido con exégesis de Groq y audio optimizado
|
| 5 |
"""
|
| 6 |
-
import os, glob, json, tempfile, html, asyncio, subprocess, shutil, re
|
| 7 |
import urllib.request
|
| 8 |
import urllib.error
|
| 9 |
import gradio as gr
|
|
@@ -141,10 +141,23 @@ def _traducir_lote_groq(pares):
|
|
| 141 |
TAMANO_LOTE_GROQ = 6
|
| 142 |
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
def traducir_capitulo_con_groq(pares):
|
| 145 |
"""
|
| 146 |
pares: lista de (n, hebreo, es_literal) de TODO el capítulo.
|
| 147 |
Trocea en lotes pequeños, traduce cada uno con Groq y une los resultados.
|
|
|
|
|
|
|
| 148 |
Devuelve (dict {n: español_con_sentido}, estado_general).
|
| 149 |
"""
|
| 150 |
if not pares:
|
|
@@ -158,7 +171,15 @@ def traducir_capitulo_con_groq(pares):
|
|
| 158 |
for i in range(0, len(pares), TAMANO_LOTE_GROQ):
|
| 159 |
lote = pares[i:i + TAMANO_LOTE_GROQ]
|
| 160 |
lotes_total += 1
|
|
|
|
| 161 |
parcial, estado_lote = _traducir_lote_groq(lote)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
resultado.update(parcial)
|
| 163 |
if estado_lote == "ok" or estado_lote.startswith("ok ("):
|
| 164 |
lotes_ok += 1
|
|
|
|
| 3 |
Centralita Torá — HuggingFace Space (Gradio)
|
| 4 |
Leer hebreo real + español fluido con exégesis de Groq y audio optimizado
|
| 5 |
"""
|
| 6 |
+
import os, glob, json, tempfile, html, asyncio, subprocess, shutil, re, time
|
| 7 |
import urllib.request
|
| 8 |
import urllib.error
|
| 9 |
import gradio as gr
|
|
|
|
| 141 |
TAMANO_LOTE_GROQ = 6
|
| 142 |
|
| 143 |
|
| 144 |
+
def _segundos_de_espera(motivo_error):
|
| 145 |
+
"""Si el error es un 429 con 'Please try again in X s', devuelve X (+1s de margen)."""
|
| 146 |
+
m = re.search(r"try again in ([\d.]+)s", motivo_error)
|
| 147 |
+
if m:
|
| 148 |
+
try:
|
| 149 |
+
return float(m.group(1)) + 1.0
|
| 150 |
+
except ValueError:
|
| 151 |
+
pass
|
| 152 |
+
return None
|
| 153 |
+
|
| 154 |
+
|
| 155 |
def traducir_capitulo_con_groq(pares):
|
| 156 |
"""
|
| 157 |
pares: lista de (n, hebreo, es_literal) de TODO el capítulo.
|
| 158 |
Trocea en lotes pequeños, traduce cada uno con Groq y une los resultados.
|
| 159 |
+
Si Groq responde 429 (límite de tokens por minuto), espera el tiempo que
|
| 160 |
+
Groq indica y reintenta ese lote (hasta 3 intentos) antes de rendirse.
|
| 161 |
Devuelve (dict {n: español_con_sentido}, estado_general).
|
| 162 |
"""
|
| 163 |
if not pares:
|
|
|
|
| 171 |
for i in range(0, len(pares), TAMANO_LOTE_GROQ):
|
| 172 |
lote = pares[i:i + TAMANO_LOTE_GROQ]
|
| 173 |
lotes_total += 1
|
| 174 |
+
|
| 175 |
parcial, estado_lote = _traducir_lote_groq(lote)
|
| 176 |
+
intentos = 1
|
| 177 |
+
while "error HTTP 429" in estado_lote and intentos < 3:
|
| 178 |
+
espera = _segundos_de_espera(estado_lote) or 8.0
|
| 179 |
+
time.sleep(espera)
|
| 180 |
+
parcial, estado_lote = _traducir_lote_groq(lote)
|
| 181 |
+
intentos += 1
|
| 182 |
+
|
| 183 |
resultado.update(parcial)
|
| 184 |
if estado_lote == "ok" or estado_lote.startswith("ok ("):
|
| 185 |
lotes_ok += 1
|