Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# Versión 2.
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import config
|
|
@@ -6,8 +6,7 @@ from core import PDFEngine
|
|
| 6 |
|
| 7 |
engine = PDFEngine()
|
| 8 |
|
| 9 |
-
# ---
|
| 10 |
-
|
| 11 |
def update_file_list(files):
|
| 12 |
if not files: return pd.DataFrame(), ""
|
| 13 |
data = [[i, f.split("/")[-1]] for i, f in enumerate(files)]
|
|
@@ -41,14 +40,14 @@ def process_reorder(f, o):
|
|
| 41 |
try: return engine.reorder_pages(f, o)
|
| 42 |
except Exception as e: raise gr.Error(str(e))
|
| 43 |
|
|
|
|
| 44 |
def process_compare(fa, fb):
|
| 45 |
if not fa or not fb: return None
|
| 46 |
-
try: return engine.
|
| 47 |
except Exception as e: raise gr.Error(str(e))
|
| 48 |
|
| 49 |
def process_compress(f, l):
|
| 50 |
if not f: return None
|
| 51 |
-
# Mapeo de interfaz a niveles de Ghostscript
|
| 52 |
lvls = {"Baja (Máxima calidad)": 1, "Media (Recomendado - eBook)": 3, "Alta (Pantalla - 72dpi)": 4}
|
| 53 |
try: return engine.compress_pdf(f, lvls.get(l, 3))
|
| 54 |
except Exception as e: raise gr.Error(str(e))
|
|
@@ -86,34 +85,30 @@ def process_text(f):
|
|
| 86 |
try: return engine.extract_text(f)
|
| 87 |
except Exception as e: raise gr.Error(str(e))
|
| 88 |
|
| 89 |
-
#
|
| 90 |
def process_word(f):
|
| 91 |
if not f: return None
|
| 92 |
try: return engine.pdf_to_word(f)
|
| 93 |
except Exception as e: raise gr.Error(str(e))
|
| 94 |
-
|
| 95 |
def process_excel(f):
|
| 96 |
if not f: return None
|
| 97 |
try: return engine.pdf_to_excel(f)
|
| 98 |
except Exception as e: raise gr.Error(str(e))
|
| 99 |
-
|
| 100 |
def process_pptx(f):
|
| 101 |
if not f: return None
|
| 102 |
try: return engine.pdf_to_pptx(f)
|
| 103 |
except Exception as e: raise gr.Error(str(e))
|
| 104 |
-
|
| 105 |
def process_p2i(f):
|
| 106 |
if not f: return None
|
| 107 |
try: return engine.pdf_to_images_zip(f)
|
| 108 |
except Exception as e: raise gr.Error(str(e))
|
| 109 |
-
|
| 110 |
def process_i2p(fs):
|
| 111 |
if not fs: return None
|
| 112 |
try: return engine.images_to_pdf(fs)
|
| 113 |
except Exception as e: raise gr.Error(str(e))
|
| 114 |
|
| 115 |
|
| 116 |
-
# --- UI
|
| 117 |
with gr.Blocks(title=config.APP_TITLE, theme=gr.themes.Soft()) as demo:
|
| 118 |
gr.Markdown(f"# {config.APP_TITLE}")
|
| 119 |
gr.Markdown(config.APP_DESCRIPTION)
|
|
@@ -133,7 +128,7 @@ with gr.Blocks(title=config.APP_TITLE, theme=gr.themes.Soft()) as demo:
|
|
| 133 |
m_files.change(update_file_list, m_files, [m_tbl, m_ord])
|
| 134 |
m_btn.click(process_merge, [m_files, m_ord], m_out)
|
| 135 |
|
| 136 |
-
# 2. DIVIDIR
|
| 137 |
with gr.TabItem("Dividir / Reordenar"):
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column():
|
|
@@ -156,98 +151,75 @@ with gr.Blocks(title=config.APP_TITLE, theme=gr.themes.Soft()) as demo:
|
|
| 156 |
r_btn.click(process_reorder, [dr_f, r_ord], r_out)
|
| 157 |
dr_f.change(load_info, dr_f, [dr_inf, dr_pg, s_out])
|
| 158 |
|
| 159 |
-
# 3. COMPRIMIR
|
| 160 |
with gr.TabItem("Comprimir"):
|
| 161 |
with gr.Row():
|
| 162 |
-
# Columna Izquierda: Entrada y Controles
|
| 163 |
with gr.Column():
|
| 164 |
c_f = gr.File(label="PDF Original", file_types=[".pdf"])
|
| 165 |
-
c_l = gr.Radio(
|
| 166 |
-
|
| 167 |
-
label="Nivel de Compresión",
|
| 168 |
-
value="Media (Recomendado - eBook)"
|
| 169 |
-
)
|
| 170 |
-
c_b = gr.Button("Comprimir Documento", variant="primary")
|
| 171 |
-
|
| 172 |
-
# Columna Derecha: Salida
|
| 173 |
with gr.Column():
|
| 174 |
-
c_out = gr.File(label="
|
| 175 |
-
|
| 176 |
c_b.click(process_compress, [c_f, c_l], c_out)
|
| 177 |
|
| 178 |
# 4. CONVERTIR
|
| 179 |
with gr.TabItem("Convertir Formatos"):
|
| 180 |
-
|
| 181 |
-
with gr.Tab("A Documentos (Word/Excel/PPT)"):
|
| 182 |
with gr.Row():
|
| 183 |
-
# PDF -> WORD
|
| 184 |
with gr.Column():
|
| 185 |
-
gr.Markdown("###
|
| 186 |
w_f = gr.File(label="PDF")
|
| 187 |
w_b = gr.Button("Convertir")
|
| 188 |
-
w_o = gr.File(
|
| 189 |
w_b.click(process_word, w_f, w_o)
|
| 190 |
-
|
| 191 |
-
# PDF -> EXCEL
|
| 192 |
with gr.Column():
|
| 193 |
-
gr.Markdown("###
|
| 194 |
x_f = gr.File(label="PDF")
|
| 195 |
x_b = gr.Button("Convertir")
|
| 196 |
-
x_o = gr.File(
|
| 197 |
x_b.click(process_excel, x_f, x_o)
|
| 198 |
-
|
| 199 |
-
# PDF -> PPT
|
| 200 |
with gr.Column():
|
| 201 |
-
gr.Markdown("###
|
| 202 |
p_f = gr.File(label="PDF")
|
| 203 |
p_b = gr.Button("Convertir")
|
| 204 |
-
p_o = gr.File(
|
| 205 |
p_b.click(process_pptx, p_f, p_o)
|
| 206 |
-
|
| 207 |
-
with gr.Tab("A Imágenes (JPG)"):
|
| 208 |
with gr.Row():
|
| 209 |
with gr.Column():
|
| 210 |
p2i_f = gr.File(label="PDF")
|
| 211 |
p2i_b = gr.Button("Extraer ZIP")
|
| 212 |
-
p2i_o = gr.File(
|
| 213 |
p2i_b.click(process_p2i, p2i_f, p2i_o)
|
| 214 |
with gr.Column():
|
| 215 |
i2p_f = gr.File(label="Imágenes", file_count="multiple")
|
| 216 |
i2p_b = gr.Button("Crear PDF")
|
| 217 |
-
i2p_o = gr.File(
|
| 218 |
i2p_b.click(process_i2p, i2p_f, i2p_o)
|
| 219 |
|
| 220 |
-
# 5. COMPARAR
|
| 221 |
with gr.TabItem("Comparar"):
|
|
|
|
| 222 |
with gr.Row():
|
| 223 |
with gr.Column():
|
| 224 |
ca = gr.File(label="Versión A (Original)", file_types=[".pdf"])
|
| 225 |
with gr.Column():
|
| 226 |
cb = gr.File(label="Versión B (Modificada)", file_types=[".pdf"])
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
co = gr.File(label="Informe Visual")
|
| 230 |
cb_btn.click(process_compare, [ca, cb], co)
|
| 231 |
|
| 232 |
-
# 6. EXTRAS
|
| 233 |
with gr.TabItem("Extras"):
|
| 234 |
with gr.Tab("Rotar"):
|
| 235 |
with gr.Row():
|
| 236 |
-
# Columna Izquierda
|
| 237 |
with gr.Column():
|
| 238 |
-
rf = gr.File(label="PDF"
|
| 239 |
-
ra = gr.Radio(
|
| 240 |
-
|
| 241 |
-
label="Ángulo de Rotación",
|
| 242 |
-
value="0° (Original)"
|
| 243 |
-
)
|
| 244 |
-
rb = gr.Button("Rotar Documento", variant="primary")
|
| 245 |
-
|
| 246 |
-
# Columna Derecha
|
| 247 |
with gr.Column():
|
| 248 |
-
rp = gr.Image(label="
|
| 249 |
-
ro = gr.File(
|
| 250 |
-
|
| 251 |
rf.change(update_rot_preview, [rf, ra], rp)
|
| 252 |
ra.change(update_rot_preview, [rf, ra], rp)
|
| 253 |
rb.click(process_rotate, [rf, ra], ro)
|
|
@@ -256,26 +228,26 @@ with gr.Blocks(title=config.APP_TITLE, theme=gr.themes.Soft()) as demo:
|
|
| 256 |
with gr.Row():
|
| 257 |
with gr.Column():
|
| 258 |
pf = gr.File(label="PDF")
|
| 259 |
-
pp = gr.Textbox(type="password", label="
|
| 260 |
pb = gr.Button("Encriptar", variant="primary")
|
| 261 |
with gr.Column():
|
| 262 |
-
po = gr.File(
|
| 263 |
pb.click(process_protect, [pf, pp], po)
|
| 264 |
-
|
| 265 |
with gr.Tab("Info/Texto"):
|
| 266 |
with gr.Row():
|
| 267 |
with gr.Column():
|
| 268 |
tf = gr.File(label="PDF")
|
| 269 |
-
tb = gr.Button("Extraer Texto
|
| 270 |
to = gr.File()
|
| 271 |
tb.click(process_text, tf, to)
|
| 272 |
with gr.Column():
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
|
| 280 |
if __name__ == "__main__":
|
| 281 |
demo.launch()
|
|
|
|
| 1 |
+
# Versión 2.2: Interfaz con Comparador de Texto Mejorado
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import config
|
|
|
|
| 6 |
|
| 7 |
engine = PDFEngine()
|
| 8 |
|
| 9 |
+
# --- WRAPPERS ---
|
|
|
|
| 10 |
def update_file_list(files):
|
| 11 |
if not files: return pd.DataFrame(), ""
|
| 12 |
data = [[i, f.split("/")[-1]] for i, f in enumerate(files)]
|
|
|
|
| 40 |
try: return engine.reorder_pages(f, o)
|
| 41 |
except Exception as e: raise gr.Error(str(e))
|
| 42 |
|
| 43 |
+
# CAMBIO AQUÍ: Llamamos a compare_pdfs_text en lugar de visual
|
| 44 |
def process_compare(fa, fb):
|
| 45 |
if not fa or not fb: return None
|
| 46 |
+
try: return engine.compare_pdfs_text(fa, fb)
|
| 47 |
except Exception as e: raise gr.Error(str(e))
|
| 48 |
|
| 49 |
def process_compress(f, l):
|
| 50 |
if not f: return None
|
|
|
|
| 51 |
lvls = {"Baja (Máxima calidad)": 1, "Media (Recomendado - eBook)": 3, "Alta (Pantalla - 72dpi)": 4}
|
| 52 |
try: return engine.compress_pdf(f, lvls.get(l, 3))
|
| 53 |
except Exception as e: raise gr.Error(str(e))
|
|
|
|
| 85 |
try: return engine.extract_text(f)
|
| 86 |
except Exception as e: raise gr.Error(str(e))
|
| 87 |
|
| 88 |
+
# WRAPPERS OFFICE
|
| 89 |
def process_word(f):
|
| 90 |
if not f: return None
|
| 91 |
try: return engine.pdf_to_word(f)
|
| 92 |
except Exception as e: raise gr.Error(str(e))
|
|
|
|
| 93 |
def process_excel(f):
|
| 94 |
if not f: return None
|
| 95 |
try: return engine.pdf_to_excel(f)
|
| 96 |
except Exception as e: raise gr.Error(str(e))
|
|
|
|
| 97 |
def process_pptx(f):
|
| 98 |
if not f: return None
|
| 99 |
try: return engine.pdf_to_pptx(f)
|
| 100 |
except Exception as e: raise gr.Error(str(e))
|
|
|
|
| 101 |
def process_p2i(f):
|
| 102 |
if not f: return None
|
| 103 |
try: return engine.pdf_to_images_zip(f)
|
| 104 |
except Exception as e: raise gr.Error(str(e))
|
|
|
|
| 105 |
def process_i2p(fs):
|
| 106 |
if not fs: return None
|
| 107 |
try: return engine.images_to_pdf(fs)
|
| 108 |
except Exception as e: raise gr.Error(str(e))
|
| 109 |
|
| 110 |
|
| 111 |
+
# --- UI ---
|
| 112 |
with gr.Blocks(title=config.APP_TITLE, theme=gr.themes.Soft()) as demo:
|
| 113 |
gr.Markdown(f"# {config.APP_TITLE}")
|
| 114 |
gr.Markdown(config.APP_DESCRIPTION)
|
|
|
|
| 128 |
m_files.change(update_file_list, m_files, [m_tbl, m_ord])
|
| 129 |
m_btn.click(process_merge, [m_files, m_ord], m_out)
|
| 130 |
|
| 131 |
+
# 2. DIVIDIR
|
| 132 |
with gr.TabItem("Dividir / Reordenar"):
|
| 133 |
with gr.Row():
|
| 134 |
with gr.Column():
|
|
|
|
| 151 |
r_btn.click(process_reorder, [dr_f, r_ord], r_out)
|
| 152 |
dr_f.change(load_info, dr_f, [dr_inf, dr_pg, s_out])
|
| 153 |
|
| 154 |
+
# 3. COMPRIMIR
|
| 155 |
with gr.TabItem("Comprimir"):
|
| 156 |
with gr.Row():
|
|
|
|
| 157 |
with gr.Column():
|
| 158 |
c_f = gr.File(label="PDF Original", file_types=[".pdf"])
|
| 159 |
+
c_l = gr.Radio(["Baja (Máxima calidad)", "Media (Recomendado - eBook)", "Alta (Pantalla - 72dpi)"], label="Nivel", value="Media (Recomendado - eBook)")
|
| 160 |
+
c_b = gr.Button("Comprimir", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
with gr.Column():
|
| 162 |
+
c_out = gr.File(label="Resultado")
|
|
|
|
| 163 |
c_b.click(process_compress, [c_f, c_l], c_out)
|
| 164 |
|
| 165 |
# 4. CONVERTIR
|
| 166 |
with gr.TabItem("Convertir Formatos"):
|
| 167 |
+
with gr.Tab("A Documentos"):
|
|
|
|
| 168 |
with gr.Row():
|
|
|
|
| 169 |
with gr.Column():
|
| 170 |
+
gr.Markdown("### A Word")
|
| 171 |
w_f = gr.File(label="PDF")
|
| 172 |
w_b = gr.Button("Convertir")
|
| 173 |
+
w_o = gr.File()
|
| 174 |
w_b.click(process_word, w_f, w_o)
|
|
|
|
|
|
|
| 175 |
with gr.Column():
|
| 176 |
+
gr.Markdown("### A Excel")
|
| 177 |
x_f = gr.File(label="PDF")
|
| 178 |
x_b = gr.Button("Convertir")
|
| 179 |
+
x_o = gr.File()
|
| 180 |
x_b.click(process_excel, x_f, x_o)
|
|
|
|
|
|
|
| 181 |
with gr.Column():
|
| 182 |
+
gr.Markdown("### A PowerPoint")
|
| 183 |
p_f = gr.File(label="PDF")
|
| 184 |
p_b = gr.Button("Convertir")
|
| 185 |
+
p_o = gr.File()
|
| 186 |
p_b.click(process_pptx, p_f, p_o)
|
| 187 |
+
with gr.Tab("A Imágenes"):
|
|
|
|
| 188 |
with gr.Row():
|
| 189 |
with gr.Column():
|
| 190 |
p2i_f = gr.File(label="PDF")
|
| 191 |
p2i_b = gr.Button("Extraer ZIP")
|
| 192 |
+
p2i_o = gr.File()
|
| 193 |
p2i_b.click(process_p2i, p2i_f, p2i_o)
|
| 194 |
with gr.Column():
|
| 195 |
i2p_f = gr.File(label="Imágenes", file_count="multiple")
|
| 196 |
i2p_b = gr.Button("Crear PDF")
|
| 197 |
+
i2p_o = gr.File()
|
| 198 |
i2p_b.click(process_i2p, i2p_f, i2p_o)
|
| 199 |
|
| 200 |
+
# 5. COMPARAR (Actualizado)
|
| 201 |
with gr.TabItem("Comparar"):
|
| 202 |
+
gr.Markdown("Compara el **texto** de dos versiones. Descarga un informe con lo añadido (Verde) y borrado (Rojo).")
|
| 203 |
with gr.Row():
|
| 204 |
with gr.Column():
|
| 205 |
ca = gr.File(label="Versión A (Original)", file_types=[".pdf"])
|
| 206 |
with gr.Column():
|
| 207 |
cb = gr.File(label="Versión B (Modificada)", file_types=[".pdf"])
|
| 208 |
+
cb_btn = gr.Button("Generar Informe de Cambios", variant="primary")
|
| 209 |
+
co = gr.File(label="Informe PDF")
|
|
|
|
| 210 |
cb_btn.click(process_compare, [ca, cb], co)
|
| 211 |
|
| 212 |
+
# 6. EXTRAS
|
| 213 |
with gr.TabItem("Extras"):
|
| 214 |
with gr.Tab("Rotar"):
|
| 215 |
with gr.Row():
|
|
|
|
| 216 |
with gr.Column():
|
| 217 |
+
rf = gr.File(label="PDF")
|
| 218 |
+
ra = gr.Radio(["0° (Original)", "90° (Derecha)", "180° (Invertir)", "270° (Izquierda)"], label="Rotación", value="0° (Original)")
|
| 219 |
+
rb = gr.Button("Rotar", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
with gr.Column():
|
| 221 |
+
rp = gr.Image(label="Preview")
|
| 222 |
+
ro = gr.File()
|
|
|
|
| 223 |
rf.change(update_rot_preview, [rf, ra], rp)
|
| 224 |
ra.change(update_rot_preview, [rf, ra], rp)
|
| 225 |
rb.click(process_rotate, [rf, ra], ro)
|
|
|
|
| 228 |
with gr.Row():
|
| 229 |
with gr.Column():
|
| 230 |
pf = gr.File(label="PDF")
|
| 231 |
+
pp = gr.Textbox(type="password", label="Pass")
|
| 232 |
pb = gr.Button("Encriptar", variant="primary")
|
| 233 |
with gr.Column():
|
| 234 |
+
po = gr.File()
|
| 235 |
pb.click(process_protect, [pf, pp], po)
|
| 236 |
+
|
| 237 |
with gr.Tab("Info/Texto"):
|
| 238 |
with gr.Row():
|
| 239 |
with gr.Column():
|
| 240 |
tf = gr.File(label="PDF")
|
| 241 |
+
tb = gr.Button("Extraer Texto")
|
| 242 |
to = gr.File()
|
| 243 |
tb.click(process_text, tf, to)
|
| 244 |
with gr.Column():
|
| 245 |
+
mt = gr.Textbox(label="Título")
|
| 246 |
+
ma = gr.Textbox(label="Autor")
|
| 247 |
+
ms = gr.Textbox(label="Asunto")
|
| 248 |
+
mb = gr.Button("Actualizar Meta")
|
| 249 |
+
mo = gr.File()
|
| 250 |
+
mb.click(process_meta, [tf, mt, ma, ms], mo)
|
| 251 |
|
| 252 |
if __name__ == "__main__":
|
| 253 |
demo.launch()
|