Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from mtranslate import translate
|
|
| 3 |
from bs4 import BeautifulSoup, NavigableString
|
| 4 |
import re
|
| 5 |
import uuid
|
|
|
|
| 6 |
|
| 7 |
# Diccionario de idiomas
|
| 8 |
lang_dict = {
|
|
@@ -48,46 +49,58 @@ def translate_html_content(text, source_lang, target_lang):
|
|
| 48 |
|
| 49 |
node.replace_with(translated_text)
|
| 50 |
|
| 51 |
-
# Asegurar espacios alrededor de etiquetas de bloque
|
| 52 |
-
for element in soup.find_all(['p', 'span', 'b', 'strong']):
|
| 53 |
-
if element.previous_sibling and isinstance(element.previous_sibling, NavigableString):
|
| 54 |
-
element.insert_before(NavigableString(' ')) # Añadir espacio antes si no hay
|
| 55 |
-
if element.next_sibling and isinstance(element.next_sibling, NavigableString):
|
| 56 |
-
element.insert_after(NavigableString(' ')) # Añadir espacio después si no hay
|
| 57 |
-
|
| 58 |
return str(soup)
|
| 59 |
|
| 60 |
def main(Texto, source_lang, target_lang):
|
| 61 |
source_code = lang_dict[source_lang]
|
| 62 |
target_code = lang_dict[target_lang]
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
translated_text = translate_html_content(Texto, source_code, target_code)
|
| 65 |
element_id = f"text-{uuid.uuid4()}"
|
| 66 |
|
| 67 |
html_output = f"""
|
| 68 |
-
<div style="position: relative; min-height: 100px;">
|
| 69 |
-
<div id="translated-content">{translated_text}</div>
|
| 70 |
<textarea id="{element_id}" style="display: none;">{translated_text}</textarea>
|
| 71 |
<button onclick="navigator.clipboard.writeText(document.getElementById('{element_id}').value).then(() => alert('Texto copiado'))"
|
| 72 |
-
style="
|
| 73 |
-
📋 Copiar
|
| 74 |
</button>
|
| 75 |
</div>
|
| 76 |
"""
|
| 77 |
return html_output
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from bs4 import BeautifulSoup, NavigableString
|
| 4 |
import re
|
| 5 |
import uuid
|
| 6 |
+
import time # Simulación de carga
|
| 7 |
|
| 8 |
# Diccionario de idiomas
|
| 9 |
lang_dict = {
|
|
|
|
| 49 |
|
| 50 |
node.replace_with(translated_text)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return str(soup)
|
| 53 |
|
| 54 |
def main(Texto, source_lang, target_lang):
|
| 55 |
source_code = lang_dict[source_lang]
|
| 56 |
target_code = lang_dict[target_lang]
|
| 57 |
+
|
| 58 |
+
# Simulación de tiempo de procesamiento para UX mejorada
|
| 59 |
+
time.sleep(2)
|
| 60 |
+
|
| 61 |
translated_text = translate_html_content(Texto, source_code, target_code)
|
| 62 |
element_id = f"text-{uuid.uuid4()}"
|
| 63 |
|
| 64 |
html_output = f"""
|
| 65 |
+
<div style="position: relative; min-height: 100px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
|
| 66 |
+
<div id="translated-content" style="font-family: Arial, sans-serif; font-size: 16px;">{translated_text}</div>
|
| 67 |
<textarea id="{element_id}" style="display: none;">{translated_text}</textarea>
|
| 68 |
<button onclick="navigator.clipboard.writeText(document.getElementById('{element_id}').value).then(() => alert('Texto copiado'))"
|
| 69 |
+
style="margin-top: 10px; padding: 5px 10px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">
|
| 70 |
+
📋 Copiar Traducción
|
| 71 |
</button>
|
| 72 |
</div>
|
| 73 |
"""
|
| 74 |
return html_output
|
| 75 |
|
| 76 |
+
# --- INTERFAZ MEJORADA ---
|
| 77 |
+
title = """<h1 align="center">🌍 Traductor Inteligente de HTML 🌍</h1>"""
|
| 78 |
+
description = """<h3 align="center">💬 Traduce contenido HTML sin perder el formato. 🚀</h3>"""
|
| 79 |
+
|
| 80 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 81 |
+
gr.HTML(title)
|
| 82 |
+
gr.HTML(description)
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column():
|
| 86 |
+
Texto = gr.Textbox(label="Introduce tu HTML aquí", placeholder="Escribe o pega un texto con HTML...", lines=10)
|
| 87 |
+
with gr.Column():
|
| 88 |
+
output = gr.HTML(label="Texto traducido")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
source_lang = gr.Dropdown(source_lang_list, value="Auto", label="Idioma de origen")
|
| 92 |
+
target_lang = gr.Dropdown(lang_list, value="Español", label="Idioma de destino")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
translate_btn = gr.Button("🌎 Traducir")
|
| 96 |
+
clear_btn = gr.Button("🧹 Limpiar")
|
| 97 |
+
|
| 98 |
+
# Indicador de carga
|
| 99 |
+
status = gr.Textbox(label="Estado", interactive=False, visible=False)
|
| 100 |
+
|
| 101 |
+
# Conectar eventos
|
| 102 |
+
translate_btn.click(main, [Texto, source_lang, target_lang], output, show_progress=True)
|
| 103 |
+
clear_btn.click(lambda: "", [], Texto)
|
| 104 |
+
|
| 105 |
+
# Activa el modo streaming para mejor experiencia
|
| 106 |
+
demo.queue().launch(debug=True)
|