Update app.py
Browse files
app.py
CHANGED
|
@@ -44,27 +44,26 @@ lang_list = list(lang_dict.keys())
|
|
| 44 |
|
| 45 |
def split_html_content(text):
|
| 46 |
"""Separa etiquetas HTML y su contenido"""
|
| 47 |
-
# Expresión regular para
|
| 48 |
pattern = r'(<[^>]+>.*?</[^>]+>)|([^<]+)'
|
| 49 |
parts = []
|
| 50 |
|
| 51 |
for match in re.finditer(pattern, text, re.DOTALL):
|
| 52 |
if match.group(1): # Elemento HTML completo
|
| 53 |
html_tag = match.group(1)
|
| 54 |
-
|
| 55 |
-
content_match = re.search(r'>((?:[^<]|<(?!/))*)<', html_tag)
|
| 56 |
if content_match:
|
| 57 |
-
content = content_match.group(1)
|
| 58 |
-
# Guardar etiqueta de apertura, contenido y cierre por separado
|
| 59 |
opening = html_tag[:content_match.start(1) + 1]
|
| 60 |
closing = html_tag[content_match.end(1):]
|
| 61 |
parts.append(('html_open', opening))
|
| 62 |
-
|
|
|
|
| 63 |
parts.append(('html_close', closing))
|
| 64 |
else:
|
| 65 |
parts.append(('html', html_tag))
|
| 66 |
elif match.group(2): # Texto fuera de etiquetas
|
| 67 |
-
parts.append(('text', match.group(2)))
|
| 68 |
|
| 69 |
return parts
|
| 70 |
|
|
@@ -94,8 +93,7 @@ def translate_text(source_lang, target_lang, text):
|
|
| 94 |
# Traducir solo las partes de texto
|
| 95 |
translated_parts = []
|
| 96 |
for part_type, content in parts:
|
| 97 |
-
if part_type == 'text':
|
| 98 |
-
# Dividir en fragmentos si es necesario
|
| 99 |
chunks = split_text(content)
|
| 100 |
translated_chunks = [
|
| 101 |
translate(chunk, target_code, source_code)
|
|
@@ -106,13 +104,12 @@ def translate_text(source_lang, target_lang, text):
|
|
| 106 |
# Mantener etiquetas HTML sin cambios
|
| 107 |
translated_parts.append(content)
|
| 108 |
|
|
|
|
| 109 |
return ''.join(translated_parts)
|
| 110 |
|
| 111 |
def main(Texto, source_lang, target_lang):
|
| 112 |
# Realizar la traducción
|
| 113 |
translated_text = translate_text(source_lang, target_lang, Texto)
|
| 114 |
-
|
| 115 |
-
# Devolver el texto plano sin formato HTML renderizado
|
| 116 |
return translated_text
|
| 117 |
|
| 118 |
iface = gr.Interface(
|
|
@@ -122,7 +119,7 @@ iface = gr.Interface(
|
|
| 122 |
gr.Dropdown(lang_list, label="Idioma origen", value="Automático"),
|
| 123 |
gr.Dropdown(lang_list, label="Idioma destino", value="Español")
|
| 124 |
],
|
| 125 |
-
outputs="text", #
|
| 126 |
title="<div style='margin:0 auto;text-align:center'><div style='margin:0 auto;text-align:center'><img style='width:100px;display: inline-table;margin-bottom:-10px' src='https://artxeweb.com/media/files/idioma.jpg'><p>Traducción sin límites</p></div>",
|
| 127 |
description="<p style='margin-bottom:10px;text-align:center;background: #ffffff; padding: 8px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb;'>Ingresa el texto que deseas traducir, selecciona el idioma origen (o deja 'Automático') y el idioma de destino. ¡No hay límites!</p>",
|
| 128 |
article="<div style='margin-top:10px'><p style='text-align: center !important; background: #ffffff; padding: 5px 30px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb; width: fit-content; margin: auto;'>Desarrollada por <a style='text-decoration: none !important; color: #e12a31 !important;' title='Artxe Web' href='https://artxeweb.com'>© Artxe Web</a></p></div>"
|
|
|
|
| 44 |
|
| 45 |
def split_html_content(text):
|
| 46 |
"""Separa etiquetas HTML y su contenido"""
|
| 47 |
+
# Expresión regular mejorada para manejar elementos HTML correctamente
|
| 48 |
pattern = r'(<[^>]+>.*?</[^>]+>)|([^<]+)'
|
| 49 |
parts = []
|
| 50 |
|
| 51 |
for match in re.finditer(pattern, text, re.DOTALL):
|
| 52 |
if match.group(1): # Elemento HTML completo
|
| 53 |
html_tag = match.group(1)
|
| 54 |
+
content_match = re.search(r'>((?:[^<]|<(?!/[^>]+>))*)<', html_tag, re.DOTALL)
|
|
|
|
| 55 |
if content_match:
|
| 56 |
+
content = content_match.group(1).strip()
|
|
|
|
| 57 |
opening = html_tag[:content_match.start(1) + 1]
|
| 58 |
closing = html_tag[content_match.end(1):]
|
| 59 |
parts.append(('html_open', opening))
|
| 60 |
+
if content: # Solo añadir contenido si no está vacío
|
| 61 |
+
parts.append(('text', content))
|
| 62 |
parts.append(('html_close', closing))
|
| 63 |
else:
|
| 64 |
parts.append(('html', html_tag))
|
| 65 |
elif match.group(2): # Texto fuera de etiquetas
|
| 66 |
+
parts.append(('text', match.group(2).strip()))
|
| 67 |
|
| 68 |
return parts
|
| 69 |
|
|
|
|
| 93 |
# Traducir solo las partes de texto
|
| 94 |
translated_parts = []
|
| 95 |
for part_type, content in parts:
|
| 96 |
+
if part_type == 'text' and content: # Solo traducir si hay contenido
|
|
|
|
| 97 |
chunks = split_text(content)
|
| 98 |
translated_chunks = [
|
| 99 |
translate(chunk, target_code, source_code)
|
|
|
|
| 104 |
# Mantener etiquetas HTML sin cambios
|
| 105 |
translated_parts.append(content)
|
| 106 |
|
| 107 |
+
# Unir las partes sin añadir espacios adicionales innecesarios
|
| 108 |
return ''.join(translated_parts)
|
| 109 |
|
| 110 |
def main(Texto, source_lang, target_lang):
|
| 111 |
# Realizar la traducción
|
| 112 |
translated_text = translate_text(source_lang, target_lang, Texto)
|
|
|
|
|
|
|
| 113 |
return translated_text
|
| 114 |
|
| 115 |
iface = gr.Interface(
|
|
|
|
| 119 |
gr.Dropdown(lang_list, label="Idioma origen", value="Automático"),
|
| 120 |
gr.Dropdown(lang_list, label="Idioma destino", value="Español")
|
| 121 |
],
|
| 122 |
+
outputs="text", # Salida como texto plano
|
| 123 |
title="<div style='margin:0 auto;text-align:center'><div style='margin:0 auto;text-align:center'><img style='width:100px;display: inline-table;margin-bottom:-10px' src='https://artxeweb.com/media/files/idioma.jpg'><p>Traducción sin límites</p></div>",
|
| 124 |
description="<p style='margin-bottom:10px;text-align:center;background: #ffffff; padding: 8px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb;'>Ingresa el texto que deseas traducir, selecciona el idioma origen (o deja 'Automático') y el idioma de destino. ¡No hay límites!</p>",
|
| 125 |
article="<div style='margin-top:10px'><p style='text-align: center !important; background: #ffffff; padding: 5px 30px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb; width: fit-content; margin: auto;'>Desarrollada por <a style='text-decoration: none !important; color: #e12a31 !important;' title='Artxe Web' href='https://artxeweb.com'>© Artxe Web</a></p></div>"
|