Spaces:
Sleeping
Sleeping
Update app.py
Browse files- changed time view
- skip 4 disable OCR
app.py
CHANGED
|
@@ -29,11 +29,11 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 29 |
try:
|
| 30 |
subprocess.run(["marker_single", "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 31 |
except FileNotFoundError:
|
| 32 |
-
yield "Error: el comando 'marker_single' no está disponible.",
|
| 33 |
return
|
| 34 |
|
| 35 |
if not formatos:
|
| 36 |
-
yield "Selecciona al menos un formato de salida.",
|
| 37 |
return
|
| 38 |
|
| 39 |
try:
|
|
@@ -44,7 +44,7 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 44 |
ruta_pdf = obtener_ruta_segura(output_base, nombre_pdf)
|
| 45 |
response = requests.get(url_pdf)
|
| 46 |
if response.status_code != 200:
|
| 47 |
-
yield "No se pudo descargar correctamente el PDF desde la URL.",
|
| 48 |
return
|
| 49 |
with open(ruta_pdf, "wb") as f:
|
| 50 |
f.write(response.content)
|
|
@@ -53,17 +53,17 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 53 |
ruta_pdf = obtener_ruta_segura(output_base, nombre_pdf)
|
| 54 |
shutil.copyfile(pdf_file.name, ruta_pdf)
|
| 55 |
else:
|
| 56 |
-
yield "No se proporcionó archivo ni URL.",
|
| 57 |
return
|
| 58 |
|
| 59 |
# Verificar cabecera PDF
|
| 60 |
with open(ruta_pdf, "rb") as f:
|
| 61 |
if f.read(4) != b"%PDF":
|
| 62 |
-
yield "El archivo no es un PDF válido (no comienza por %PDF).",
|
| 63 |
return
|
| 64 |
|
| 65 |
if os.path.getsize(ruta_pdf) > 3 * 1024 * 1024:
|
| 66 |
-
yield "Aviso: el PDF es grande y puede tardar más de lo normal.",
|
| 67 |
|
| 68 |
nombre_sin_ext = os.path.splitext(os.path.basename(ruta_pdf))[0]
|
| 69 |
carpeta_salida = os.path.join(output_base, nombre_sin_ext)
|
|
@@ -73,15 +73,15 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 73 |
|
| 74 |
# Procesar cada formato
|
| 75 |
for fmt in formatos:
|
| 76 |
-
tiempo_parcial =
|
| 77 |
if fmt == "md":
|
| 78 |
-
cmd = ["marker_single", ruta_pdf, "--output_format", "markdown", "--
|
| 79 |
elif fmt == "md + ocr":
|
| 80 |
cmd = ["marker_single", ruta_pdf, "--output_format", "markdown", "--output_dir", carpeta_salida]
|
| 81 |
else:
|
| 82 |
cmd = ["marker_single", ruta_pdf, "--output_format", fmt, "--output_dir", carpeta_salida]
|
| 83 |
|
| 84 |
-
yield f"Procesando formato '{fmt}'...
|
| 85 |
|
| 86 |
for intento in range(1, 4):
|
| 87 |
try:
|
|
@@ -89,11 +89,11 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 89 |
if result.returncode == 0:
|
| 90 |
break
|
| 91 |
elif intento == 3:
|
| 92 |
-
yield f"Error en marker_single:\n{result.stderr}",
|
| 93 |
return
|
| 94 |
time.sleep(2)
|
| 95 |
except Exception as e:
|
| 96 |
-
yield f"Error inesperado:\n{str(e)}",
|
| 97 |
return
|
| 98 |
|
| 99 |
# Leer contenido Markdown
|
|
@@ -121,14 +121,14 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 121 |
zip_path = os.path.join(output_base, f"{nombre_sin_ext}.zip")
|
| 122 |
shutil.make_archive(base_name=zip_path.replace(".zip", ""), format="zip", root_dir=carpeta_salida)
|
| 123 |
|
| 124 |
-
estado_final = f"Procesamiento completado correctamente (tiempo total: {tiempo_total} segundos)"
|
| 125 |
salida_md = md_content + resumen if md_content else resumen
|
| 126 |
-
yield estado_final,
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
-
yield f"Error general inesperado: {str(e)}",
|
| 130 |
|
| 131 |
-
# Interfaz con
|
| 132 |
demo = gr.Interface(
|
| 133 |
fn=procesar_pdf,
|
| 134 |
inputs=[
|
|
@@ -141,8 +141,7 @@ demo = gr.Interface(
|
|
| 141 |
)
|
| 142 |
],
|
| 143 |
outputs=[
|
| 144 |
-
gr.Textbox(label="Estado del procesamiento", lines=
|
| 145 |
-
gr.Textbox(label="Tiempo parcial", interactive=False),
|
| 146 |
gr.File(label="Descargar Markdown", visible=False),
|
| 147 |
gr.File(label="Descargar ZIP completo", visible=False),
|
| 148 |
gr.Textbox(label="Contenido extraído en Markdown", lines=25, visible=False)
|
|
|
|
| 29 |
try:
|
| 30 |
subprocess.run(["marker_single", "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 31 |
except FileNotFoundError:
|
| 32 |
+
yield "❌ Error: el comando 'marker_single' no está disponible.", None, None, None
|
| 33 |
return
|
| 34 |
|
| 35 |
if not formatos:
|
| 36 |
+
yield "⚠️ Selecciona al menos un formato de salida.", None, None, None
|
| 37 |
return
|
| 38 |
|
| 39 |
try:
|
|
|
|
| 44 |
ruta_pdf = obtener_ruta_segura(output_base, nombre_pdf)
|
| 45 |
response = requests.get(url_pdf)
|
| 46 |
if response.status_code != 200:
|
| 47 |
+
yield "❌ No se pudo descargar correctamente el PDF desde la URL.", None, None, None
|
| 48 |
return
|
| 49 |
with open(ruta_pdf, "wb") as f:
|
| 50 |
f.write(response.content)
|
|
|
|
| 53 |
ruta_pdf = obtener_ruta_segura(output_base, nombre_pdf)
|
| 54 |
shutil.copyfile(pdf_file.name, ruta_pdf)
|
| 55 |
else:
|
| 56 |
+
yield "⚠️ No se proporcionó archivo ni URL.", None, None, None
|
| 57 |
return
|
| 58 |
|
| 59 |
# Verificar cabecera PDF
|
| 60 |
with open(ruta_pdf, "rb") as f:
|
| 61 |
if f.read(4) != b"%PDF":
|
| 62 |
+
yield "❌ El archivo no es un PDF válido (no comienza por %PDF).", None, None, None
|
| 63 |
return
|
| 64 |
|
| 65 |
if os.path.getsize(ruta_pdf) > 3 * 1024 * 1024:
|
| 66 |
+
yield "⚠️ Aviso: el PDF es grande y puede tardar más de lo normal.", None, None, None
|
| 67 |
|
| 68 |
nombre_sin_ext = os.path.splitext(os.path.basename(ruta_pdf))[0]
|
| 69 |
carpeta_salida = os.path.join(output_base, nombre_sin_ext)
|
|
|
|
| 73 |
|
| 74 |
# Procesar cada formato
|
| 75 |
for fmt in formatos:
|
| 76 |
+
tiempo_parcial = round(time.time() - start_time, 1)
|
| 77 |
if fmt == "md":
|
| 78 |
+
cmd = ["marker_single", ruta_pdf, "--output_format", "markdown", "--disable_ocr", "--output_dir", carpeta_salida]
|
| 79 |
elif fmt == "md + ocr":
|
| 80 |
cmd = ["marker_single", ruta_pdf, "--output_format", "markdown", "--output_dir", carpeta_salida]
|
| 81 |
else:
|
| 82 |
cmd = ["marker_single", ruta_pdf, "--output_format", fmt, "--output_dir", carpeta_salida]
|
| 83 |
|
| 84 |
+
yield f"⏳ Procesando formato '{fmt}'... (tiempo: {tiempo_parcial} s)", None, None, None
|
| 85 |
|
| 86 |
for intento in range(1, 4):
|
| 87 |
try:
|
|
|
|
| 89 |
if result.returncode == 0:
|
| 90 |
break
|
| 91 |
elif intento == 3:
|
| 92 |
+
yield f"❌ Error en marker_single:\n{result.stderr}", None, None, None
|
| 93 |
return
|
| 94 |
time.sleep(2)
|
| 95 |
except Exception as e:
|
| 96 |
+
yield f"❌ Error inesperado:\n{str(e)}", None, None, None
|
| 97 |
return
|
| 98 |
|
| 99 |
# Leer contenido Markdown
|
|
|
|
| 121 |
zip_path = os.path.join(output_base, f"{nombre_sin_ext}.zip")
|
| 122 |
shutil.make_archive(base_name=zip_path.replace(".zip", ""), format="zip", root_dir=carpeta_salida)
|
| 123 |
|
| 124 |
+
estado_final = f"✅ Procesamiento completado correctamente (tiempo total: {tiempo_total} segundos)"
|
| 125 |
salida_md = md_content + resumen if md_content else resumen
|
| 126 |
+
yield estado_final, gr.update(value=md_path, visible=True), gr.update(value=zip_path, visible=True), salida_md
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
+
yield f"❌ Error general inesperado: {str(e)}", None, None, None
|
| 130 |
|
| 131 |
+
# Interfaz con 4 salidas
|
| 132 |
demo = gr.Interface(
|
| 133 |
fn=procesar_pdf,
|
| 134 |
inputs=[
|
|
|
|
| 141 |
)
|
| 142 |
],
|
| 143 |
outputs=[
|
| 144 |
+
gr.Textbox(label="Estado del procesamiento", lines=5),
|
|
|
|
| 145 |
gr.File(label="Descargar Markdown", visible=False),
|
| 146 |
gr.File(label="Descargar ZIP completo", visible=False),
|
| 147 |
gr.Textbox(label="Contenido extraído en Markdown", lines=25, visible=False)
|