eberhenriquez94 commited on
Commit
45b9d3b
verified
1 Parent(s): caebb19
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  import tempfile
6
  import shlex
7
  from gradio_pdf import PDF
 
8
 
9
  # Configuraci贸n de logs
10
  logger = logging.getLogger(__name__)
@@ -44,6 +45,7 @@ def flujo_principal(pdf_file, idioma="spa"):
44
  reparado_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
45
  simplificado_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
46
  output_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
 
47
 
48
  try:
49
  # Reparar el PDF
@@ -55,11 +57,19 @@ def flujo_principal(pdf_file, idioma="spa"):
55
  # Procesar con OCR
56
  crear_pdf_con_texto_incrustado(simplificado_pdf, output_pdf, idioma)
57
 
58
- return output_pdf # Devolver el PDF final con OCR
 
 
 
59
 
60
  except Exception as e:
61
  logger.error(f"Error durante el procesamiento del PDF: {str(e)}")
62
  raise gr.Error(f"Error al procesar el PDF: {str(e)}")
 
 
 
 
 
63
 
64
  # Interfaz Gradio
65
  with gr.Blocks() as interfaz:
@@ -72,20 +82,19 @@ with gr.Blocks() as interfaz:
72
 
73
  with gr.Row():
74
  pdf_vista = PDF(label="Visor PDF procesado", interactive=False) # Salida usando PDF para visualizaci贸n
75
- boton_procesar.click(
76
- fn=flujo_principal,
77
- inputs=[archivo_pdf, idioma_ocr],
78
- outputs=[pdf_vista],
79
- )
80
-
81
- with gr.Row():
82
- gr.Markdown("### Descargar PDF procesado con OCR")
83
- pdf_descarga = gr.File(label="Descargar PDF con OCR", interactive=False)
84
- boton_procesar.click(
85
- fn=lambda x: x,
86
- inputs=[pdf_vista],
87
- outputs=[pdf_descarga]
88
- )
89
 
90
  if __name__ == "__main__":
91
  interfaz.launch()
 
5
  import tempfile
6
  import shlex
7
  from gradio_pdf import PDF
8
+ import shutil
9
 
10
  # Configuraci贸n de logs
11
  logger = logging.getLogger(__name__)
 
45
  reparado_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
46
  simplificado_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
47
  output_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
48
+ final_output_path = "/mnt/data/pdf_con_ocr.pdf" # Ubicaci贸n fija para descarga en Hugging Face Space
49
 
50
  try:
51
  # Reparar el PDF
 
57
  # Procesar con OCR
58
  crear_pdf_con_texto_incrustado(simplificado_pdf, output_pdf, idioma)
59
 
60
+ # Mover el archivo final a una ubicaci贸n persistente
61
+ shutil.move(output_pdf, final_output_path)
62
+
63
+ return final_output_path # Devolver el archivo para visualizaci贸n y descarga
64
 
65
  except Exception as e:
66
  logger.error(f"Error durante el procesamiento del PDF: {str(e)}")
67
  raise gr.Error(f"Error al procesar el PDF: {str(e)}")
68
+ finally:
69
+ # Limpiar archivos temporales no necesarios
70
+ for temp_file in [reparado_pdf, simplificado_pdf]:
71
+ if os.path.exists(temp_file):
72
+ os.remove(temp_file)
73
 
74
  # Interfaz Gradio
75
  with gr.Blocks() as interfaz:
 
82
 
83
  with gr.Row():
84
  pdf_vista = PDF(label="Visor PDF procesado", interactive=False) # Salida usando PDF para visualizaci贸n
85
+ pdf_descarga = gr.File(label="Descargar PDF procesado con OCR", interactive=False)
86
+
87
+ boton_procesar.click(
88
+ fn=flujo_principal,
89
+ inputs=[archivo_pdf, idioma_ocr],
90
+ outputs=[pdf_vista],
91
+ )
92
+
93
+ boton_procesar.click(
94
+ fn=lambda x: x,
95
+ inputs=[pdf_vista],
96
+ outputs=[pdf_descarga]
97
+ )
 
98
 
99
  if __name__ == "__main__":
100
  interfaz.launch()