Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,135 @@
|
|
| 1 |
-
# Versión 1.
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import config
|
| 4 |
from core import PDFEngine
|
| 5 |
|
| 6 |
-
# Instanciamos el motor (Inyección simple)
|
| 7 |
engine = PDFEngine()
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if not files:
|
| 11 |
return None
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
if not file:
|
| 18 |
-
return None
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
if not file or not
|
| 23 |
return None
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
with gr.Blocks(title=config.APP_TITLE, theme=gr.themes.Soft()) as demo:
|
| 28 |
gr.Markdown(f"# {config.APP_TITLE}")
|
| 29 |
-
gr.Markdown(config.APP_DESCRIPTION)
|
| 30 |
|
| 31 |
with gr.Tabs():
|
| 32 |
-
#
|
| 33 |
with gr.TabItem("Unir PDF"):
|
| 34 |
-
gr.Markdown(
|
|
|
|
| 35 |
with gr.Row():
|
| 36 |
-
with gr.Column():
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
)
|
| 42 |
-
merge_btn = gr.Button("Unir Archivos", variant="primary")
|
| 43 |
-
with gr.Column():
|
| 44 |
-
merge_output = gr.File(label="PDF Unido")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
with gr.TabItem("Dividir PDF"):
|
| 54 |
-
gr.Markdown(
|
|
|
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
with gr.Column():
|
| 64 |
-
|
| 65 |
-
split_output = gr.File(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
split_btn.click(
|
| 68 |
-
fn=
|
| 69 |
-
inputs=
|
| 70 |
outputs=split_output
|
| 71 |
)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
with gr.TabItem("Proteger PDF"):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
with gr.Column():
|
| 78 |
-
protect_input = gr.File(label="PDF a proteger")
|
| 79 |
-
pass_input = gr.Textbox(label="Contraseña", type="password")
|
| 80 |
-
protect_btn = gr.Button("Encriptar", variant="primary")
|
| 81 |
-
with gr.Column():
|
| 82 |
-
protect_output = gr.File(label="PDF Encriptado")
|
| 83 |
-
|
| 84 |
-
protect_btn.click(
|
| 85 |
-
fn=interface_protect,
|
| 86 |
-
inputs=[protect_input, pass_input],
|
| 87 |
-
outputs=protect_output
|
| 88 |
-
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|
|
|
|
| 1 |
+
# Versión 1.1: UI Avanzada con gestión de estado
|
| 2 |
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
import config
|
| 5 |
from core import PDFEngine
|
| 6 |
|
|
|
|
| 7 |
engine = PDFEngine()
|
| 8 |
|
| 9 |
+
# --- FUNCIONES AUXILIARES UNIR ---
|
| 10 |
+
def update_file_list(files):
|
| 11 |
+
"""Crea un Dataframe para mostrar los archivos subidos y sus índices."""
|
| 12 |
+
if not files:
|
| 13 |
+
return pd.DataFrame(), ""
|
| 14 |
+
|
| 15 |
+
# files es una lista de objetos o rutas dependiendo de la config.
|
| 16 |
+
# En Gradio 4, si file_count="multiple", suele ser lista de rutas.
|
| 17 |
+
data = []
|
| 18 |
+
for idx, f_path in enumerate(files):
|
| 19 |
+
name = f_path.split("/")[-1] # Obtener nombre simple
|
| 20 |
+
data.append([idx, name])
|
| 21 |
+
|
| 22 |
+
default_order = ",".join([str(i) for i in range(len(files))])
|
| 23 |
+
return pd.DataFrame(data, columns=["ID", "Nombre del Archivo"]), default_order
|
| 24 |
+
|
| 25 |
+
def process_merge_ordered(files, order_str):
|
| 26 |
if not files:
|
| 27 |
return None
|
| 28 |
+
try:
|
| 29 |
+
# Convertir string "0, 2, 1" a lista de enteros [0, 2, 1]
|
| 30 |
+
indices = [int(x.strip()) for x in order_str.split(",") if x.strip().isdigit()]
|
| 31 |
+
return engine.merge_pdfs(files, indices)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
raise gr.Error(f"Error en el orden: {str(e)}")
|
| 34 |
|
| 35 |
+
# --- FUNCIONES AUXILIARES DIVIDIR ---
|
| 36 |
+
def load_pdf_info(file):
|
| 37 |
if not file:
|
| 38 |
+
return None, None, gr.update(visible=False)
|
| 39 |
+
|
| 40 |
+
info = engine.get_pdf_info(file)
|
| 41 |
+
msg = f"Documento cargado: {info['name']} ({info['pages']} páginas)."
|
| 42 |
+
return msg, info['pages'], gr.update(visible=True)
|
| 43 |
|
| 44 |
+
def update_preview(file, range_str, total_pages):
|
| 45 |
+
if not file or not range_str:
|
| 46 |
return None
|
| 47 |
+
|
| 48 |
+
# Parseamos para ver qué páginas quiere el usuario
|
| 49 |
+
# Solo previsualizamos la primera y última del rango para no saturar memoria
|
| 50 |
+
indices = engine.parse_range_string(range_str, total_pages)
|
| 51 |
+
if not indices:
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
first_idx = indices[0] + 1 # Convertir a 1-based para visualización
|
| 55 |
+
last_idx = indices[-1] + 1
|
| 56 |
+
|
| 57 |
+
images = []
|
| 58 |
+
# Generar imagen de inicio
|
| 59 |
+
img1 = engine.generate_preview(file, first_idx)
|
| 60 |
+
if img1: images.append((img1, f"Página {first_idx}"))
|
| 61 |
+
|
| 62 |
+
# Generar imagen de fin si es distinta
|
| 63 |
+
if last_idx != first_idx:
|
| 64 |
+
img2 = engine.generate_preview(file, last_idx)
|
| 65 |
+
if img2: images.append((img2, f"Página {last_idx}"))
|
| 66 |
+
|
| 67 |
+
return images
|
| 68 |
+
|
| 69 |
+
def process_split_range(file, range_str):
|
| 70 |
+
return engine.split_pdf_custom(file, range_str)
|
| 71 |
|
| 72 |
+
# --- UI PRINCIPAL ---
|
| 73 |
with gr.Blocks(title=config.APP_TITLE, theme=gr.themes.Soft()) as demo:
|
| 74 |
gr.Markdown(f"# {config.APP_TITLE}")
|
|
|
|
| 75 |
|
| 76 |
with gr.Tabs():
|
| 77 |
+
# TAB 1: UNIR (Con reordenamiento manual)
|
| 78 |
with gr.TabItem("Unir PDF"):
|
| 79 |
+
gr.Markdown("Sube los archivos, revisa sus IDs en la tabla y escribe el orden deseado (ej: 2,0,1).")
|
| 80 |
+
|
| 81 |
with gr.Row():
|
| 82 |
+
with gr.Column(scale=1):
|
| 83 |
+
merge_files = gr.File(file_count="multiple", label="Archivos", file_types=[".pdf"])
|
| 84 |
+
with gr.Column(scale=2):
|
| 85 |
+
file_table = gr.Dataframe(headers=["ID", "Nombre del Archivo"], interactive=False)
|
| 86 |
+
order_input = gr.Textbox(label="Orden de unión (IDs separados por coma)")
|
| 87 |
+
merge_btn = gr.Button("Unir en este orden", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
merge_output = gr.File(label="Resultado")
|
| 90 |
+
|
| 91 |
+
# Eventos
|
| 92 |
+
merge_files.change(fn=update_file_list, inputs=merge_files, outputs=[file_table, order_input])
|
| 93 |
+
merge_btn.click(fn=process_merge_ordered, inputs=[merge_files, order_input], outputs=merge_output)
|
| 94 |
|
| 95 |
+
# TAB 2: DIVIDIR (Con Rangos y Preview)
|
| 96 |
+
with gr.TabItem("Extraer/Dividir PDF"):
|
| 97 |
+
gr.Markdown("Extrae páginas específicas. Ejemplos de rango: '1-5', '1,3,5', '1-3, 8'.")
|
| 98 |
+
|
| 99 |
with gr.Row():
|
| 100 |
with gr.Column():
|
| 101 |
+
split_file = gr.File(label="PDF Origen", file_types=[".pdf"])
|
| 102 |
+
info_text = gr.Markdown("Esperando archivo...")
|
| 103 |
+
# Estado oculto para guardar número total de páginas
|
| 104 |
+
page_count_state = gr.State(0)
|
| 105 |
+
|
| 106 |
+
range_input = gr.Textbox(label="Rango de páginas", placeholder="Ej: 1-3, 5")
|
| 107 |
+
preview_btn = gr.Button("Previsualizar Selección")
|
| 108 |
+
split_btn = gr.Button("Extraer Páginas", variant="primary")
|
| 109 |
+
|
| 110 |
with gr.Column():
|
| 111 |
+
preview_gallery = gr.Gallery(label="Vista previa (Inicio/Fin)", columns=2, height=300, object_fit="contain")
|
| 112 |
+
split_output = gr.File(label="PDF Extraído")
|
| 113 |
+
|
| 114 |
+
# Eventos
|
| 115 |
+
split_file.change(fn=load_pdf_info, inputs=split_file, outputs=[info_text, page_count_state, split_output])
|
| 116 |
+
|
| 117 |
+
preview_btn.click(
|
| 118 |
+
fn=update_preview,
|
| 119 |
+
inputs=[split_file, range_input, page_count_state],
|
| 120 |
+
outputs=preview_gallery
|
| 121 |
+
)
|
| 122 |
|
| 123 |
split_btn.click(
|
| 124 |
+
fn=process_split_range,
|
| 125 |
+
inputs=[split_file, range_input],
|
| 126 |
outputs=split_output
|
| 127 |
)
|
| 128 |
|
| 129 |
+
# TAB 3: PROTEGER (Igual que antes)
|
| 130 |
with gr.TabItem("Proteger PDF"):
|
| 131 |
+
# (Mantener el código de la UI de proteger de la versión 1.0)
|
| 132 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
demo.launch()
|