Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import re
|
|
| 4 |
|
| 5 |
# Diccionario de idiomas y sus códigos
|
| 6 |
lang_dict = {
|
|
|
|
| 7 |
'Español': 'es',
|
| 8 |
'English': 'en',
|
| 9 |
'Mandarín': 'zh',
|
|
@@ -41,7 +42,34 @@ lang_dict = {
|
|
| 41 |
|
| 42 |
lang_list = list(lang_dict.keys())
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def split_text(text, limit=4000):
|
|
|
|
| 45 |
sentences = re.split(r'([;.])', text)
|
| 46 |
chunks = []
|
| 47 |
chunk = ''
|
|
@@ -55,39 +83,62 @@ def split_text(text, limit=4000):
|
|
| 55 |
chunks.append(chunk)
|
| 56 |
return chunks
|
| 57 |
|
| 58 |
-
def translate_text(
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
return
|
| 69 |
|
| 70 |
-
def main(Texto,
|
| 71 |
# Realizar la traducción
|
| 72 |
-
translated_text = translate_text(
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
html_output = f"""
|
| 77 |
-
<div>
|
| 78 |
{translated_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
</div>
|
| 80 |
"""
|
| 81 |
|
| 82 |
return html_output
|
| 83 |
|
| 84 |
-
|
| 85 |
iface = gr.Interface(
|
| 86 |
fn=main,
|
| 87 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
outputs="html",
|
| 89 |
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>",
|
| 90 |
-
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
|
| 91 |
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>"
|
| 92 |
)
|
| 93 |
|
|
|
|
| 4 |
|
| 5 |
# Diccionario de idiomas y sus códigos
|
| 6 |
lang_dict = {
|
| 7 |
+
'Automático': 'auto',
|
| 8 |
'Español': 'es',
|
| 9 |
'English': 'en',
|
| 10 |
'Mandarín': 'zh',
|
|
|
|
| 42 |
|
| 43 |
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 encontrar elementos HTML completos
|
| 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 |
+
# Extraer contenido entre etiquetas
|
| 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 |
+
parts.append(('text', content))
|
| 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 |
+
|
| 71 |
def split_text(text, limit=4000):
|
| 72 |
+
"""Divide texto en fragmentos respetando el límite"""
|
| 73 |
sentences = re.split(r'([;.])', text)
|
| 74 |
chunks = []
|
| 75 |
chunk = ''
|
|
|
|
| 83 |
chunks.append(chunk)
|
| 84 |
return chunks
|
| 85 |
|
| 86 |
+
def translate_text(source_lang, target_lang, text):
|
| 87 |
+
"""Traduce contenido manteniendo etiquetas HTML"""
|
| 88 |
+
source_code = lang_dict[source_lang]
|
| 89 |
+
target_code = lang_dict[target_lang]
|
| 90 |
|
| 91 |
+
# Separar HTML y contenido
|
| 92 |
+
parts = split_html_content(text)
|
| 93 |
|
| 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)
|
| 102 |
+
for chunk in chunks
|
| 103 |
+
]
|
| 104 |
+
translated_parts.append(''.join(translated_chunks))
|
| 105 |
+
else:
|
| 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 |
+
# Escapar comillas para evitar errores en el JavaScript
|
| 116 |
+
translated_text_escaped = translated_text.replace('"', '"')
|
| 117 |
+
|
| 118 |
+
# Generar salida HTML con botón de copia
|
| 119 |
html_output = f"""
|
| 120 |
+
<div style="white-space: pre-wrap;">
|
| 121 |
{translated_text}
|
| 122 |
+
<button class="lg secondary svelte-cmf5ev"
|
| 123 |
+
style="font-size: small; padding: 2px; color: #808080ba; border: none; margin-left: 5px;"
|
| 124 |
+
onclick='navigator.clipboard.writeText("{translated_text_escaped}").then(() => alert("Texto copiado al portapapeles"))'>
|
| 125 |
+
✂
|
| 126 |
+
</button>
|
| 127 |
</div>
|
| 128 |
"""
|
| 129 |
|
| 130 |
return html_output
|
| 131 |
|
|
|
|
| 132 |
iface = gr.Interface(
|
| 133 |
fn=main,
|
| 134 |
+
inputs=[
|
| 135 |
+
gr.Textbox(label="Texto a traducir", lines=5),
|
| 136 |
+
gr.Dropdown(lang_list, label="Idioma origen", value="Automático"),
|
| 137 |
+
gr.Dropdown(lang_list, label="Idioma destino", value="Español")
|
| 138 |
+
],
|
| 139 |
outputs="html",
|
| 140 |
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>",
|
| 141 |
+
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>",
|
| 142 |
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>"
|
| 143 |
)
|
| 144 |
|