Spaces:
Build error
Build error
app.py
CHANGED
@@ -4,10 +4,11 @@ import logging
|
|
4 |
from PyPDF2 import PdfReader
|
5 |
import tempfile
|
6 |
import os
|
|
|
7 |
|
8 |
-
# Configuraci贸n de logs
|
9 |
logger = logging.getLogger(__name__)
|
10 |
-
logging.basicConfig(level=logging.INFO, format=
|
11 |
|
12 |
def ejecutar_comando(comando):
|
13 |
"""Ejecuta un comando de shell y maneja errores."""
|
@@ -23,13 +24,12 @@ def ejecutar_comando(comando):
|
|
23 |
def crear_pdf_con_texto_incrustado(pdf_original, archivo_salida, idioma="spa"):
|
24 |
"""Procesa un PDF con OCR usando OCRmyPDF."""
|
25 |
try:
|
26 |
-
|
27 |
-
|
28 |
-
)
|
29 |
-
except RuntimeError as e:
|
30 |
raise gr.Error(str(e)) # Mostrar el error en la interfaz de Gradio
|
31 |
|
32 |
-
|
33 |
def leer_pdf(pdf_path):
|
34 |
"""Extrae texto de un archivo PDF."""
|
35 |
try:
|
@@ -46,25 +46,27 @@ def flujo_principal(pdf_file, idioma="spa"):
|
|
46 |
if not pdf_file:
|
47 |
raise gr.Error("No se subi贸 ning煤n archivo.")
|
48 |
|
49 |
-
#
|
50 |
input_pdf = pdf_file.name
|
|
|
|
|
51 |
|
52 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_output:
|
53 |
output_pdf = temp_output.name
|
54 |
|
55 |
-
texto_original = leer_pdf(
|
56 |
|
57 |
try:
|
58 |
-
crear_pdf_con_texto_incrustado(
|
59 |
texto_ocr = leer_pdf(output_pdf)
|
60 |
-
return gr.File(
|
61 |
except gr.Error as e:
|
62 |
-
# Limpieza en caso de error
|
63 |
if os.path.exists(output_pdf):
|
64 |
os.remove(output_pdf)
|
65 |
-
raise e
|
66 |
finally:
|
67 |
-
|
|
|
68 |
if os.path.exists(output_pdf):
|
69 |
os.remove(output_pdf)
|
70 |
|
@@ -91,4 +93,4 @@ with gr.Blocks() as interfaz:
|
|
91 |
outputs=[pdf_original_vista, texto_original, pdf_ocr_vista, texto_ocr]
|
92 |
)
|
93 |
|
94 |
-
interfaz.launch(share=True)
|
|
|
4 |
from PyPDF2 import PdfReader
|
5 |
import tempfile
|
6 |
import os
|
7 |
+
import shlex # Importa para manejar nombres de archivo con espacios
|
8 |
|
9 |
+
# Configuraci贸n de logs
|
10 |
logger = logging.getLogger(__name__)
|
11 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
12 |
|
13 |
def ejecutar_comando(comando):
|
14 |
"""Ejecuta un comando de shell y maneja errores."""
|
|
|
24 |
def crear_pdf_con_texto_incrustado(pdf_original, archivo_salida, idioma="spa"):
|
25 |
"""Procesa un PDF con OCR usando OCRmyPDF."""
|
26 |
try:
|
27 |
+
# Usa shlex.quote para manejar espacios en los nombres de archivo
|
28 |
+
comando = f"ocrmypdf -l {idioma} --force-ocr --deskew --clean --output-type pdf {shlex.quote(pdf_original)} {shlex.quote(archivo_salida)}"
|
29 |
+
ejecutar_comando(comando)
|
30 |
+
except RuntimeError as e:
|
31 |
raise gr.Error(str(e)) # Mostrar el error en la interfaz de Gradio
|
32 |
|
|
|
33 |
def leer_pdf(pdf_path):
|
34 |
"""Extrae texto de un archivo PDF."""
|
35 |
try:
|
|
|
46 |
if not pdf_file:
|
47 |
raise gr.Error("No se subi贸 ning煤n archivo.")
|
48 |
|
49 |
+
# Aseg煤rate de que los nombres de archivo sean seguros
|
50 |
input_pdf = pdf_file.name
|
51 |
+
input_pdf_fixed = input_pdf.replace(" ", "_") # Renombra temporalmente para evitar problemas con espacios
|
52 |
+
os.rename(input_pdf, input_pdf_fixed)
|
53 |
|
54 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_output:
|
55 |
output_pdf = temp_output.name
|
56 |
|
57 |
+
texto_original = leer_pdf(input_pdf_fixed)
|
58 |
|
59 |
try:
|
60 |
+
crear_pdf_con_texto_incrustado(input_pdf_fixed, output_pdf, idioma)
|
61 |
texto_ocr = leer_pdf(output_pdf)
|
62 |
+
return gr.File(input_pdf_fixed, label="PDF Original"), texto_original, gr.File(output_pdf, label="PDF con OCR"), texto_ocr
|
63 |
except gr.Error as e:
|
|
|
64 |
if os.path.exists(output_pdf):
|
65 |
os.remove(output_pdf)
|
66 |
+
raise e
|
67 |
finally:
|
68 |
+
if os.path.exists(input_pdf_fixed):
|
69 |
+
os.remove(input_pdf_fixed)
|
70 |
if os.path.exists(output_pdf):
|
71 |
os.remove(output_pdf)
|
72 |
|
|
|
93 |
outputs=[pdf_original_vista, texto_original, pdf_ocr_vista, texto_ocr]
|
94 |
)
|
95 |
|
96 |
+
interfaz.launch(share=True)
|