Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from mtranslate import translate
|
| 3 |
import re
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
# Diccionario de idiomas (sin cambios)
|
| 7 |
lang_dict = {
|
|
@@ -29,32 +30,35 @@ def split_text(text, limit=4000):
|
|
| 29 |
return chunks
|
| 30 |
|
| 31 |
def translate_html_content(text, source_lang, target_lang):
|
| 32 |
-
# Patrón para identificar etiquetas HTML
|
| 33 |
html_tag_pattern = re.compile(r'<[^>]+>')
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
parts = html_tag_pattern.split(text)
|
| 37 |
tags = html_tag_pattern.findall(text)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
for
|
| 42 |
-
|
| 43 |
-
chunks = split_text(part)
|
| 44 |
-
translated_chunks = [translate(chunk, target_lang, source_lang) for chunk in chunks]
|
| 45 |
-
translated_part = ''.join(translated_chunks)
|
| 46 |
-
# Limpiar caracteres escapados no deseados
|
| 47 |
-
translated_part = translated_part.replace('\\"', '"').replace('\\n', '\n').replace('\\\\', '\\')
|
| 48 |
-
translated_parts.append(translated_part)
|
| 49 |
-
else:
|
| 50 |
-
translated_parts.append(part) # Mantener partes vacías o espacios
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
return translated_text
|
| 60 |
|
|
|
|
| 2 |
from mtranslate import translate
|
| 3 |
import re
|
| 4 |
import json
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
# Diccionario de idiomas (sin cambios)
|
| 8 |
lang_dict = {
|
|
|
|
| 30 |
return chunks
|
| 31 |
|
| 32 |
def translate_html_content(text, source_lang, target_lang):
|
| 33 |
+
# Patrón para identificar etiquetas HTML completas
|
| 34 |
html_tag_pattern = re.compile(r'<[^>]+>')
|
| 35 |
|
| 36 |
+
# Encontrar todas las etiquetas HTML
|
|
|
|
| 37 |
tags = html_tag_pattern.findall(text)
|
| 38 |
|
| 39 |
+
# Reemplazar cada etiqueta con un marcador único
|
| 40 |
+
markers = {f"{{{uuid.uuid4()}}}": tag for tag in tags}
|
| 41 |
+
for marker, tag in markers.items():
|
| 42 |
+
text = text.replace(tag, marker)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Dividir el texto en chunks respetando los marcadores
|
| 45 |
+
chunks = split_text(text)
|
| 46 |
+
|
| 47 |
+
# Traducir cada chunk
|
| 48 |
+
translated_chunks = []
|
| 49 |
+
for chunk in chunks:
|
| 50 |
+
# Traducir el chunk
|
| 51 |
+
translated_chunk = translate(chunk, target_lang, source_lang)
|
| 52 |
+
# Limpiar caracteres escapados no deseados
|
| 53 |
+
translated_chunk = translated_chunk.replace('\\"', '"').replace('\\n', '\n').replace('\\\\', '\\')
|
| 54 |
+
translated_chunks.append(translated_chunk)
|
| 55 |
+
|
| 56 |
+
# Unir los chunks traducidos
|
| 57 |
+
translated_text = ''.join(translated_chunks)
|
| 58 |
+
|
| 59 |
+
# Restaurar las etiquetas originales
|
| 60 |
+
for marker, tag in markers.items():
|
| 61 |
+
translated_text = translated_text.replace(marker, tag)
|
| 62 |
|
| 63 |
return translated_text
|
| 64 |
|