Spaces:
Build error
Build error
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import logging
|
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
-
import shlex
|
| 8 |
|
| 9 |
# Configuraci贸n de logs
|
| 10 |
logger = logging.getLogger(__name__)
|
|
@@ -24,11 +24,10 @@ def ejecutar_comando(comando):
|
|
| 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 --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))
|
| 32 |
|
| 33 |
def leer_pdf(pdf_path):
|
| 34 |
"""Extrae texto de un archivo PDF."""
|
|
@@ -54,10 +53,8 @@ def flujo_principal(pdf_file, idioma="spa"):
|
|
| 54 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_output:
|
| 55 |
output_pdf = temp_output.name
|
| 56 |
|
| 57 |
-
# Extraer texto original del PDF
|
| 58 |
texto_original = leer_pdf(input_pdf)
|
| 59 |
|
| 60 |
-
# Procesar el PDF con OCR
|
| 61 |
try:
|
| 62 |
crear_pdf_con_texto_incrustado(input_pdf, output_pdf, idioma)
|
| 63 |
texto_ocr = leer_pdf(output_pdf)
|
|
@@ -69,7 +66,6 @@ def flujo_principal(pdf_file, idioma="spa"):
|
|
| 69 |
os.remove(output_pdf)
|
| 70 |
raise e
|
| 71 |
finally:
|
| 72 |
-
# Limpieza de archivos temporales
|
| 73 |
if os.path.exists(input_pdf):
|
| 74 |
os.remove(input_pdf)
|
| 75 |
if os.path.exists(output_pdf):
|
|
|
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
+
import shlex
|
| 8 |
|
| 9 |
# Configuraci贸n de logs
|
| 10 |
logger = logging.getLogger(__name__)
|
|
|
|
| 24 |
def crear_pdf_con_texto_incrustado(pdf_original, archivo_salida, idioma="spa"):
|
| 25 |
"""Procesa un PDF con OCR usando OCRmyPDF."""
|
| 26 |
try:
|
|
|
|
| 27 |
comando = f"ocrmypdf -l {idioma} --force-ocr --deskew --output-type pdf {shlex.quote(pdf_original)} {shlex.quote(archivo_salida)}"
|
| 28 |
ejecutar_comando(comando)
|
| 29 |
except RuntimeError as e:
|
| 30 |
+
raise gr.Error(str(e))
|
| 31 |
|
| 32 |
def leer_pdf(pdf_path):
|
| 33 |
"""Extrae texto de un archivo PDF."""
|
|
|
|
| 53 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_output:
|
| 54 |
output_pdf = temp_output.name
|
| 55 |
|
|
|
|
| 56 |
texto_original = leer_pdf(input_pdf)
|
| 57 |
|
|
|
|
| 58 |
try:
|
| 59 |
crear_pdf_con_texto_incrustado(input_pdf, output_pdf, idioma)
|
| 60 |
texto_ocr = leer_pdf(output_pdf)
|
|
|
|
| 66 |
os.remove(output_pdf)
|
| 67 |
raise e
|
| 68 |
finally:
|
|
|
|
| 69 |
if os.path.exists(input_pdf):
|
| 70 |
os.remove(input_pdf)
|
| 71 |
if os.path.exists(output_pdf):
|