tx3bas commited on
Commit
82ada27
·
verified ·
1 Parent(s): ff7deb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from mtranslate import translate
3
  import re
4
  import json
 
5
 
6
  # Diccionario de idiomas (sin cambios)
7
  lang_dict = {
@@ -29,32 +30,35 @@ def split_text(text, limit=4000):
29
  return chunks
30
 
31
  def translate_html_content(text, source_lang, target_lang):
32
- # Patrón para identificar etiquetas HTML
33
  html_tag_pattern = re.compile(r'<[^>]+>')
34
 
35
- # Separar texto y etiquetas
36
- parts = html_tag_pattern.split(text)
37
  tags = html_tag_pattern.findall(text)
38
 
39
- # Traducir solo las partes de texto
40
- translated_parts = []
41
- for part in parts:
42
- if part.strip(): # Solo traducir si hay texto no vacío
43
- chunks = split_text(part)
44
- translated_chunks = [translate(chunk, target_lang, source_lang) for chunk in chunks]
45
- translated_part = ''.join(translated_chunks)
46
- # Limpiar caracteres escapados no deseados
47
- translated_part = translated_part.replace('\\"', '"').replace('\\n', '\n').replace('\\\\', '\\')
48
- translated_parts.append(translated_part)
49
- else:
50
- translated_parts.append(part) # Mantener partes vacías o espacios
51
 
52
- # Reconstruir el texto
53
- translated_text = ''
54
- for i in range(len(parts)):
55
- translated_text += translated_parts[i]
56
- if i < len(tags):
57
- translated_text += tags[i]
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  return translated_text
60
 
 
2
  from mtranslate import translate
3
  import re
4
  import json
5
+ import uuid
6
 
7
  # Diccionario de idiomas (sin cambios)
8
  lang_dict = {
 
30
  return chunks
31
 
32
  def translate_html_content(text, source_lang, target_lang):
33
+ # Patrón para identificar etiquetas HTML completas
34
  html_tag_pattern = re.compile(r'<[^>]+>')
35
 
36
+ # Encontrar todas las etiquetas HTML
 
37
  tags = html_tag_pattern.findall(text)
38
 
39
+ # Reemplazar cada etiqueta con un marcador único
40
+ markers = {f"{{{uuid.uuid4()}}}": tag for tag in tags}
41
+ for marker, tag in markers.items():
42
+ text = text.replace(tag, marker)
 
 
 
 
 
 
 
 
43
 
44
+ # Dividir el texto en chunks respetando los marcadores
45
+ chunks = split_text(text)
46
+
47
+ # Traducir cada chunk
48
+ translated_chunks = []
49
+ for chunk in chunks:
50
+ # Traducir el chunk
51
+ translated_chunk = translate(chunk, target_lang, source_lang)
52
+ # Limpiar caracteres escapados no deseados
53
+ translated_chunk = translated_chunk.replace('\\"', '"').replace('\\n', '\n').replace('\\\\', '\\')
54
+ translated_chunks.append(translated_chunk)
55
+
56
+ # Unir los chunks traducidos
57
+ translated_text = ''.join(translated_chunks)
58
+
59
+ # Restaurar las etiquetas originales
60
+ for marker, tag in markers.items():
61
+ translated_text = translated_text.replace(marker, tag)
62
 
63
  return translated_text
64