Spaces:
Sleeping
Sleeping
Update app.py
Browse filesfixed show time error
app.py
CHANGED
|
@@ -25,11 +25,11 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 25 |
os.makedirs(output_base, exist_ok=True)
|
| 26 |
start_time = time.time()
|
| 27 |
|
| 28 |
-
# Verificar
|
| 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:
|
|
@@ -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.", "", None, None, None
|
| 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.", "", None, None, None
|
| 57 |
return
|
| 58 |
|
| 59 |
-
# Verificar cabecera
|
| 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)
|
|
@@ -71,7 +71,7 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 71 |
shutil.rmtree(carpeta_salida)
|
| 72 |
os.makedirs(carpeta_salida, exist_ok=True)
|
| 73 |
|
| 74 |
-
# Procesar
|
| 75 |
for fmt in formatos:
|
| 76 |
tiempo_parcial = f"{round(time.time() - start_time, 1)} s"
|
| 77 |
if fmt == "md":
|
|
@@ -81,7 +81,7 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 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,14 +89,14 @@ 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
|
| 93 |
return
|
| 94 |
time.sleep(2)
|
| 95 |
except Exception as e:
|
| 96 |
-
yield f"Error inesperado
|
| 97 |
return
|
| 98 |
|
| 99 |
-
# Leer contenido
|
| 100 |
md_path = os.path.join(carpeta_salida, f"{nombre_sin_ext}.md")
|
| 101 |
md_content = ""
|
| 102 |
if os.path.exists(md_path):
|
|
@@ -123,12 +123,12 @@ def procesar_pdf(pdf_file=None, url_pdf=None, formatos=[], verbose="auto"):
|
|
| 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
|
| 132 |
demo = gr.Interface(
|
| 133 |
fn=procesar_pdf,
|
| 134 |
inputs=[
|
|
@@ -141,13 +141,14 @@ demo = gr.Interface(
|
|
| 141 |
)
|
| 142 |
],
|
| 143 |
outputs=[
|
| 144 |
-
gr.Textbox(label="Estado del procesamiento
|
|
|
|
| 145 |
gr.File(label="Descargar Markdown", visible=False),
|
| 146 |
gr.File(label="Descargar ZIP completo", visible=False),
|
| 147 |
-
gr.Textbox(label="Contenido Markdown
|
| 148 |
],
|
| 149 |
title="Marker PDF",
|
| 150 |
-
description="Convierte PDFs científicos en Markdown, HTML o JSON. Incluye OCR opcional y resumen
|
| 151 |
flagging_mode="never"
|
| 152 |
)
|
| 153 |
|
|
|
|
| 25 |
os.makedirs(output_base, exist_ok=True)
|
| 26 |
start_time = time.time()
|
| 27 |
|
| 28 |
+
# Verificar marker_single
|
| 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:
|
|
|
|
| 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, 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, 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, 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, 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)
|
|
|
|
| 71 |
shutil.rmtree(carpeta_salida)
|
| 72 |
os.makedirs(carpeta_salida, exist_ok=True)
|
| 73 |
|
| 74 |
+
# Procesar cada formato
|
| 75 |
for fmt in formatos:
|
| 76 |
tiempo_parcial = f"{round(time.time() - start_time, 1)} s"
|
| 77 |
if fmt == "md":
|
|
|
|
| 81 |
else:
|
| 82 |
cmd = ["marker_single", ruta_pdf, "--output_format", fmt, "--output_dir", carpeta_salida]
|
| 83 |
|
| 84 |
+
yield f"Procesando formato '{fmt}'...", tiempo_parcial, 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}", tiempo_parcial, None, None, None
|
| 93 |
return
|
| 94 |
time.sleep(2)
|
| 95 |
except Exception as e:
|
| 96 |
+
yield f"Error inesperado:\n{str(e)}", tiempo_parcial, None, None, None
|
| 97 |
return
|
| 98 |
|
| 99 |
+
# Leer contenido Markdown
|
| 100 |
md_path = os.path.join(carpeta_salida, f"{nombre_sin_ext}.md")
|
| 101 |
md_content = ""
|
| 102 |
if os.path.exists(md_path):
|
|
|
|
| 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, f"{tiempo_total} s", 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 5 salidas
|
| 132 |
demo = gr.Interface(
|
| 133 |
fn=procesar_pdf,
|
| 134 |
inputs=[
|
|
|
|
| 141 |
)
|
| 142 |
],
|
| 143 |
outputs=[
|
| 144 |
+
gr.Textbox(label="Estado del procesamiento", lines=2),
|
| 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)
|
| 149 |
],
|
| 150 |
title="Marker PDF",
|
| 151 |
+
description="Convierte PDFs científicos en Markdown, HTML o JSON. Incluye OCR opcional y resumen completo.",
|
| 152 |
flagging_mode="never"
|
| 153 |
)
|
| 154 |
|