File size: 4,108 Bytes
95c5b84 15c8645 2dac9bd e7104f3 01c4102 f805301 e7104f3 d87bf4a a98c9aa d87bf4a 02756c5 a98c9aa 2e86218 2dac9bd e7104f3 2dac9bd e7104f3 2dac9bd a98c9aa f14054c a0ca30c 15c8645 a6ce0c1 15c8645 a0ca30c e7104f3 2dac9bd a0ca30c a6ce0c1 a0ca30c a6ce0c1 a0ca30c f14054c a0ca30c f14054c d1f277d a98c9aa 9bd8424 01c4102 a98c9aa e7104f3 a0ca30c 3c3ebfd 01c4102 e7104f3 01c4102 9bd8424 3c3ebfd d1f277d 01c4102 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
import gradio as gr
from mtranslate import translate
from bs4 import BeautifulSoup, NavigableString
import re
import uuid
import time # Simulación de carga
# Diccionario de idiomas
lang_dict = {
'Auto': 'auto',
'Español': 'es',
'English': 'en',
}
lang_list = [k for k in lang_dict.keys() if k != 'Auto']
source_lang_list = ['Auto'] + lang_list
def split_text(text, limit=4000):
sentences = re.split(r'([;.])', text)
chunks = []
chunk = ''
for i in range(0, len(sentences), 2):
sentence = sentences[i] + (sentences[i+1] if i+1 < len(sentences) else '')
if len(chunk) + len(sentence) > limit:
chunks.append(chunk)
chunk = sentence
else:
chunk += sentence
if chunk:
chunks.append(chunk)
return chunks
def translate_html_content(text, source_lang, target_lang):
soup = BeautifulSoup(text, 'html.parser')
# Traducir nodos de texto
for node in soup.find_all(string=True): # Cambiado de text=True a string=True
if isinstance(node, NavigableString) and node.strip():
original_text = node.strip()
chunks = split_text(original_text)
translated_chunks = [translate(chunk, target_lang, source_lang) for chunk in chunks]
translated_text = ''.join(translated_chunks)
# Verificar espacios antes y después del nodo
if node.previous_sibling and isinstance(node.previous_sibling, NavigableString):
translated_text = ' ' + translated_text
if node.next_sibling and isinstance(node.next_sibling, NavigableString):
if not node.next_sibling.startswith(' '): # Verificación adicional
translated_text += ' '
node.replace_with(translated_text)
return str(soup)
def main(Texto, source_lang, target_lang):
source_code = lang_dict[source_lang]
target_code = lang_dict[target_lang]
# Simulación de tiempo de procesamiento para UX mejorada
time.sleep(2)
translated_text = translate_html_content(Texto, source_code, target_code)
element_id = f"text-{uuid.uuid4()}"
html_output = f"""
<div style="position: relative; min-height: 100px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
<div id="translated-content" style="font-family: Arial, sans-serif; font-size: 16px;">{translated_text}</div>
<textarea id="{element_id}" style="display: none;">{translated_text}</textarea>
<button onclick="navigator.clipboard.writeText(document.getElementById('{element_id}').value).then(() => alert('Texto copiado'))"
style="margin-top: 10px; padding: 5px 10px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">
📋 Copiar Traducción
</button>
</div>
"""
return html_output
# --- INTERFAZ MEJORADA ---
title = """<h1 align="center">🌍 Traductor Inteligente de HTML 🌍</h1>"""
description = """<h3 align="center">💬 Traduce contenido HTML sin perder el formato. 🚀</h3>"""
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
gr.HTML(title)
gr.HTML(description)
with gr.Row():
with gr.Column():
Texto = gr.Textbox(label="Introduce tu HTML aquí", placeholder="Escribe o pega un texto con HTML...", lines=10)
with gr.Column():
output = gr.HTML(label="Texto traducido")
with gr.Row():
source_lang = gr.Dropdown(source_lang_list, value="Auto", label="Idioma de origen")
target_lang = gr.Dropdown(lang_list, value="Español", label="Idioma de destino")
with gr.Row():
translate_btn = gr.Button("🌎 Traducir")
clear_btn = gr.Button("🧹 Limpiar")
# Indicador de carga
status = gr.Textbox(label="Estado", interactive=False, visible=False)
# Conectar eventos
translate_btn.click(main, [Texto, source_lang, target_lang], output, show_progress=True)
clear_btn.click(lambda: "", [], Texto)
# Activa el modo streaming para mejor experiencia
demo.queue().launch(debug=True)
|