tx3bas commited on
Commit
a3ff0af
·
verified ·
1 Parent(s): 901e549

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -76
app.py CHANGED
@@ -1,11 +1,6 @@
1
  import streamlit as st
2
  from mtranslate import translate
3
  import re
4
- import fitz # PyMuPDF
5
- import docx
6
- from io import BytesIO
7
- from reportlab.pdfgen import canvas
8
- from reportlab.lib.pagesizes import letter
9
 
10
  # Diccionario de idiomas y sus códigos
11
  lang_dict = {
@@ -72,87 +67,24 @@ def translate_text(lang, text):
72
 
73
  return translated_text
74
 
75
- def read_pdf(file):
76
- doc = fitz.open(stream=file.read(), filetype="pdf")
77
- text = ""
78
- for page in doc:
79
- text += page.get_text()
80
- return text
81
-
82
- def read_docx(file):
83
- doc = docx.Document(file)
84
- text = "\n".join([para.text for para in doc.paragraphs])
85
- return text
86
-
87
- def save_pdf(original_file, translated_text):
88
- buffer = BytesIO()
89
- original_doc = fitz.open(stream=original_file.read(), filetype="pdf")
90
-
91
- # Crear un nuevo documento PDF
92
- translated_doc = fitz.open()
93
- for page_num in range(len(original_doc)):
94
- original_page = original_doc.load_page(page_num)
95
- translated_page = translated_doc.new_page(width=original_page.rect.width, height=original_page.rect.height)
96
-
97
- # Insertar el texto traducido
98
- translated_page.insert_text((72, 72), translated_text, fontsize=12, fontname="helv")
99
-
100
- translated_doc.save(buffer)
101
- buffer.seek(0)
102
- return buffer
103
-
104
- def save_docx(text):
105
- buffer = BytesIO()
106
- doc = docx.Document()
107
- doc.add_paragraph(text)
108
- doc.save(buffer)
109
- buffer.seek(0)
110
- return buffer
111
-
112
  def main():
113
  st.title("Traducción sin límites")
114
 
115
  text = st.text_area("Texto a traducir", height=200)
116
  lang = st.selectbox("Idioma", lang_list)
117
-
118
- uploaded_file = st.file_uploader("Sube un documento (PDF o DOCX)", type=["pdf", "docx"])
119
-
120
- if uploaded_file:
121
- if uploaded_file.type == "application/pdf":
122
- text = read_pdf(uploaded_file)
123
- elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
124
- text = read_docx(uploaded_file)
125
- st.text_area("Contenido del documento", text, height=200)
126
 
127
  if st.button("Traducir"):
128
  # Realizar la traducción
129
  translated_text = translate_text(lang, text)
130
 
131
- # Mostrar el resultado
132
- st.write("Texto traducido:")
133
- st.write(translated_text)
134
-
135
- # Botón para copiar el texto traducido
136
- st.markdown(f'<button onclick="navigator.clipboard.writeText(`{translated_text}`)">Copiar al portapapeles</button>', unsafe_allow_html=True)
137
-
138
- if uploaded_file:
139
- if uploaded_file.type == "application/pdf":
140
- uploaded_file.seek(0) # Reiniciar el puntero del archivo
141
- translated_file = save_pdf(uploaded_file, translated_text)
142
- st.download_button(
143
- label="Descargar PDF traducido",
144
- data=translated_file,
145
- file_name="documento_traducido.pdf",
146
- mime="application/pdf"
147
- )
148
- elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
149
- translated_file = save_docx(translated_text)
150
- st.download_button(
151
- label="Descargar DOCX traducido",
152
- data=translated_file,
153
- file_name="documento_traducido.docx",
154
- mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
155
- )
156
 
157
  if __name__ == "__main__":
158
  main()
 
1
  import streamlit as st
2
  from mtranslate import translate
3
  import re
 
 
 
 
 
4
 
5
  # Diccionario de idiomas y sus códigos
6
  lang_dict = {
 
67
 
68
  return translated_text
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def main():
71
  st.title("Traducción sin límites")
72
 
73
  text = st.text_area("Texto a traducir", height=200)
74
  lang = st.selectbox("Idioma", lang_list)
 
 
 
 
 
 
 
 
 
75
 
76
  if st.button("Traducir"):
77
  # Realizar la traducción
78
  translated_text = translate_text(lang, text)
79
 
80
+ # Mostrar el resultado con el botón de copia
81
+ html_output = f"""
82
+ <div>
83
+ <span id='translated_text'>{translated_text}</span>
84
+ <button class="lg secondary svelte-cmf5ev" style="font-size: small; padding: 2px; color: #808080ba; border: none; margin-left: 5px;" onclick='navigator.clipboard.writeText(document.getElementById("translated_text").innerText).then(() => alert("Texto copiado al portapapeles"))'>&nbsp;✂&nbsp;</button>
85
+ </div>
86
+ """
87
+ st.markdown(html_output, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  if __name__ == "__main__":
90
  main()