Spaces:
Build error
Build error
app.py
CHANGED
|
@@ -41,6 +41,25 @@ def crear_pdf_con_texto_incrustado(pdf_original, archivo_salida, idioma="spa"):
|
|
| 41 |
logger.error(f"Error en ocrmypdf: {e}")
|
| 42 |
raise
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def flujo_principal(pdf_file, idioma="spa"):
|
| 45 |
"""Procesa un PDF subido con reparaci贸n, simplificaci贸n y OCR."""
|
| 46 |
if not pdf_file:
|
|
@@ -52,16 +71,18 @@ def flujo_principal(pdf_file, idioma="spa"):
|
|
| 52 |
simplificar_pdf(temp_pdf.name, temp_pdf.name)
|
| 53 |
crear_pdf_con_texto_incrustado(temp_pdf.name, temp_pdf.name, idioma)
|
| 54 |
ruta_final = temp_pdf.name
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
-
logger.exception(f"Error durante el procesamiento: {e}")
|
| 58 |
raise gr.Error(f"Error al procesar el PDF: {e}")
|
| 59 |
finally:
|
| 60 |
-
# Elimina el archivo temporal al final, incluso si hay errores
|
| 61 |
if os.path.exists(temp_pdf.name):
|
| 62 |
os.remove(temp_pdf.name)
|
| 63 |
|
| 64 |
-
|
| 65 |
# Interfaz Gradio
|
| 66 |
with gr.Blocks() as interfaz:
|
| 67 |
gr.Markdown("## Procesador de PDFs con OCR")
|
|
@@ -72,10 +93,9 @@ with gr.Blocks() as interfaz:
|
|
| 72 |
boton_procesar = gr.Button("Procesar OCR")
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
-
pdf_vista = gr.
|
| 76 |
pdf_descarga = gr.File(label="Descargar PDF procesado", visible=False)
|
| 77 |
|
| 78 |
-
|
| 79 |
boton_procesar.click(
|
| 80 |
fn=flujo_principal,
|
| 81 |
inputs=[archivo_pdf, idioma_ocr],
|
|
@@ -83,4 +103,4 @@ with gr.Blocks() as interfaz:
|
|
| 83 |
)
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
-
interfaz.launch()
|
|
|
|
| 41 |
logger.error(f"Error en ocrmypdf: {e}")
|
| 42 |
raise
|
| 43 |
|
| 44 |
+
def mostrar_pdf(ruta_pdf):
|
| 45 |
+
"""
|
| 46 |
+
Devuelve un HTML con un iframe que usa PDF.js para mostrar un archivo PDF.
|
| 47 |
+
"""
|
| 48 |
+
if ruta_pdf:
|
| 49 |
+
# Aseg煤rate de usar rutas correctas para los archivos procesados
|
| 50 |
+
return f"""
|
| 51 |
+
<html>
|
| 52 |
+
<head>
|
| 53 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.min.js"></script>
|
| 54 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.worker.min.js"></script>
|
| 55 |
+
</head>
|
| 56 |
+
<body>
|
| 57 |
+
<iframe src="{ruta_pdf}" width="100%" height="600px" style="border: none;"></iframe>
|
| 58 |
+
</body>
|
| 59 |
+
</html>
|
| 60 |
+
"""
|
| 61 |
+
return "<p>No se pudo generar la vista previa del PDF.</p>"
|
| 62 |
+
|
| 63 |
def flujo_principal(pdf_file, idioma="spa"):
|
| 64 |
"""Procesa un PDF subido con reparaci贸n, simplificaci贸n y OCR."""
|
| 65 |
if not pdf_file:
|
|
|
|
| 71 |
simplificar_pdf(temp_pdf.name, temp_pdf.name)
|
| 72 |
crear_pdf_con_texto_incrustado(temp_pdf.name, temp_pdf.name, idioma)
|
| 73 |
ruta_final = temp_pdf.name
|
| 74 |
+
|
| 75 |
+
# Crear vista previa para PDF.js
|
| 76 |
+
pdf_vista_html = mostrar_pdf(ruta_final)
|
| 77 |
+
|
| 78 |
+
return pdf_vista_html, gr.File(value=ruta_final, label="Descargar PDF procesado")
|
| 79 |
except Exception as e:
|
| 80 |
+
logger.exception(f"Error durante el procesamiento: {e}")
|
| 81 |
raise gr.Error(f"Error al procesar el PDF: {e}")
|
| 82 |
finally:
|
|
|
|
| 83 |
if os.path.exists(temp_pdf.name):
|
| 84 |
os.remove(temp_pdf.name)
|
| 85 |
|
|
|
|
| 86 |
# Interfaz Gradio
|
| 87 |
with gr.Blocks() as interfaz:
|
| 88 |
gr.Markdown("## Procesador de PDFs con OCR")
|
|
|
|
| 93 |
boton_procesar = gr.Button("Procesar OCR")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
+
pdf_vista = gr.HTML(label="Visor PDF") # Cambiado a gr.HTML
|
| 97 |
pdf_descarga = gr.File(label="Descargar PDF procesado", visible=False)
|
| 98 |
|
|
|
|
| 99 |
boton_procesar.click(
|
| 100 |
fn=flujo_principal,
|
| 101 |
inputs=[archivo_pdf, idioma_ocr],
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
interfaz.launch()
|