Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,9 @@ import gradio as gr
|
|
| 2 |
from mtranslate import translate
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import re
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
# Diccionario de idiomas
|
| 8 |
lang_dict = {
|
| 9 |
'Auto': 'auto',
|
| 10 |
'Español': 'es',
|
|
@@ -16,17 +16,13 @@ lang_list = [k for k in lang_dict.keys() if k != 'Auto']
|
|
| 16 |
source_lang_list = ['Auto'] + lang_list
|
| 17 |
|
| 18 |
def split_text(text, limit=4000):
|
| 19 |
-
|
| 20 |
-
Divide el texto en fragmentos de hasta 'limit' caracteres usando el punto (.) como delimitador.
|
| 21 |
-
"""
|
| 22 |
-
sentences = re.split(r'(\.)', text) # Separa conservando el punto
|
| 23 |
chunks = []
|
| 24 |
chunk = ''
|
| 25 |
for i in range(0, len(sentences), 2):
|
| 26 |
sentence = sentences[i] + (sentences[i+1] if i+1 < len(sentences) else '')
|
| 27 |
if len(chunk) + len(sentence) > limit:
|
| 28 |
-
|
| 29 |
-
chunks.append(chunk)
|
| 30 |
chunk = sentence
|
| 31 |
else:
|
| 32 |
chunk += sentence
|
|
@@ -35,42 +31,31 @@ def split_text(text, limit=4000):
|
|
| 35 |
return chunks
|
| 36 |
|
| 37 |
def translate_html_content(text, source_lang, target_lang):
|
| 38 |
-
"""
|
| 39 |
-
Traduce el texto dentro de un HTML, preservando las etiquetas y dividiendo en fragmentos de máximo 4000 caracteres.
|
| 40 |
-
"""
|
| 41 |
soup = BeautifulSoup(text, 'html.parser')
|
| 42 |
-
|
| 43 |
for node in soup.find_all(text=True):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
chunks = split_text(text_to_translate)
|
| 48 |
-
translated_chunks = []
|
| 49 |
-
for chunk in chunks:
|
| 50 |
-
# Traduce cada fragmento
|
| 51 |
-
translated_chunk = translate(chunk, target_lang, source_lang)
|
| 52 |
-
# Limpia caracteres escapados no deseados
|
| 53 |
-
translated_chunk = translated_chunk.replace('\\"', '"').replace('\\n', '\n').replace('\\\\', '\\')
|
| 54 |
-
translated_chunks.append(translated_chunk)
|
| 55 |
-
# Une los fragmentos traducidos
|
| 56 |
translated_text = ''.join(translated_chunks)
|
| 57 |
-
# Reemplaza el texto original por el traducido
|
| 58 |
node.replace_with(translated_text)
|
| 59 |
-
|
| 60 |
return str(soup)
|
| 61 |
|
| 62 |
def main(Texto, source_lang, target_lang):
|
| 63 |
source_code = lang_dict[source_lang]
|
| 64 |
target_code = lang_dict[target_lang]
|
| 65 |
|
|
|
|
| 66 |
translated_text = translate_html_content(Texto, source_code, target_code)
|
| 67 |
-
escaped_text = json.dumps(translated_text)[1:-1]
|
| 68 |
|
| 69 |
-
#
|
|
|
|
|
|
|
|
|
|
| 70 |
html_output = f"""
|
| 71 |
<div style="position: relative; min-height: 100px;">
|
| 72 |
<div id="translated-content">{translated_text}</div>
|
| 73 |
-
<
|
|
|
|
| 74 |
style="position: absolute; top: 0; right: 0; padding: 5px; background: #ffffff; border: 1px solid #e0e0e0; border-radius: 4px; cursor: pointer;">
|
| 75 |
📋 Copiar
|
| 76 |
</button>
|
|
@@ -82,11 +67,11 @@ def main(Texto, source_lang, target_lang):
|
|
| 82 |
iface = gr.Interface(
|
| 83 |
fn=main,
|
| 84 |
inputs=[
|
| 85 |
-
|
| 86 |
gr.Dropdown(source_lang_list, value="Auto", label="Idioma origen"),
|
| 87 |
gr.Dropdown(lang_list, value="Español", label="Idioma destino")
|
| 88 |
],
|
| 89 |
-
outputs=
|
| 90 |
title="Traducción sin límites",
|
| 91 |
description="Ingresa el texto que deseas traducir y selecciona los idiomas. ¡No hay límites!",
|
| 92 |
article="Desarrollada por © Artxe Web"
|
|
|
|
| 2 |
from mtranslate import translate
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import re
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
+
# Diccionario de idiomas
|
| 8 |
lang_dict = {
|
| 9 |
'Auto': 'auto',
|
| 10 |
'Español': 'es',
|
|
|
|
| 16 |
source_lang_list = ['Auto'] + lang_list
|
| 17 |
|
| 18 |
def split_text(text, limit=4000):
|
| 19 |
+
sentences = re.split(r'([;.])', text)
|
|
|
|
|
|
|
|
|
|
| 20 |
chunks = []
|
| 21 |
chunk = ''
|
| 22 |
for i in range(0, len(sentences), 2):
|
| 23 |
sentence = sentences[i] + (sentences[i+1] if i+1 < len(sentences) else '')
|
| 24 |
if len(chunk) + len(sentence) > limit:
|
| 25 |
+
chunks.append(chunk)
|
|
|
|
| 26 |
chunk = sentence
|
| 27 |
else:
|
| 28 |
chunk += sentence
|
|
|
|
| 31 |
return chunks
|
| 32 |
|
| 33 |
def translate_html_content(text, source_lang, target_lang):
|
|
|
|
|
|
|
|
|
|
| 34 |
soup = BeautifulSoup(text, 'html.parser')
|
|
|
|
| 35 |
for node in soup.find_all(text=True):
|
| 36 |
+
if node.strip():
|
| 37 |
+
chunks = split_text(node)
|
| 38 |
+
translated_chunks = [translate(chunk, target_lang, source_lang) for chunk in chunks]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
translated_text = ''.join(translated_chunks)
|
|
|
|
| 40 |
node.replace_with(translated_text)
|
|
|
|
| 41 |
return str(soup)
|
| 42 |
|
| 43 |
def main(Texto, source_lang, target_lang):
|
| 44 |
source_code = lang_dict[source_lang]
|
| 45 |
target_code = lang_dict[target_lang]
|
| 46 |
|
| 47 |
+
# Obtener el texto traducido como texto plano
|
| 48 |
translated_text = translate_html_content(Texto, source_code, target_code)
|
|
|
|
| 49 |
|
| 50 |
+
# Generar un ID único para el elemento
|
| 51 |
+
element_id = f"text-{uuid.uuid4()}"
|
| 52 |
+
|
| 53 |
+
# Crear el HTML con un elemento oculto para el texto plano
|
| 54 |
html_output = f"""
|
| 55 |
<div style="position: relative; min-height: 100px;">
|
| 56 |
<div id="translated-content">{translated_text}</div>
|
| 57 |
+
<textarea id="{element_id}" style="display: none;">{translated_text}</textarea>
|
| 58 |
+
<button onclick="navigator.clipboard.writeText(document.getElementById('{element_id}').value).then(() => alert('Texto copiado'))"
|
| 59 |
style="position: absolute; top: 0; right: 0; padding: 5px; background: #ffffff; border: 1px solid #e0e0e0; border-radius: 4px; cursor: pointer;">
|
| 60 |
📋 Copiar
|
| 61 |
</button>
|
|
|
|
| 67 |
iface = gr.Interface(
|
| 68 |
fn=main,
|
| 69 |
inputs=[
|
| 70 |
+
"text",
|
| 71 |
gr.Dropdown(source_lang_list, value="Auto", label="Idioma origen"),
|
| 72 |
gr.Dropdown(lang_list, value="Español", label="Idioma destino")
|
| 73 |
],
|
| 74 |
+
outputs="html",
|
| 75 |
title="Traducción sin límites",
|
| 76 |
description="Ingresa el texto que deseas traducir y selecciona los idiomas. ¡No hay límites!",
|
| 77 |
article="Desarrollada por © Artxe Web"
|