Spaces:
Build error
Build error
app.py
CHANGED
|
@@ -25,7 +25,7 @@ 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 --
|
| 29 |
ejecutar_comando(comando)
|
| 30 |
except RuntimeError as e:
|
| 31 |
raise gr.Error(str(e)) # Mostrar el error en la interfaz de Gradio
|
|
@@ -46,19 +46,40 @@ 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 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Interfaz Gradio
|
| 58 |
with gr.Blocks() as interfaz:
|
| 59 |
gr.Markdown("## Procesador OCR para PDFs")
|
| 60 |
with gr.Row():
|
| 61 |
-
archivo_pdf = gr.File(label="Sube tu archivo PDF")
|
| 62 |
idioma_ocr = gr.Dropdown(["spa", "eng", "fra", "deu"], label="Idioma OCR", value="spa")
|
| 63 |
boton_procesar = gr.Button("Procesar OCR")
|
| 64 |
with gr.Row():
|
|
@@ -73,4 +94,4 @@ with gr.Blocks() as interfaz:
|
|
| 73 |
outputs=[pdf_original_vista, texto_original, pdf_ocr_vista, texto_ocr]
|
| 74 |
)
|
| 75 |
|
| 76 |
-
interfaz.launch()
|
|
|
|
| 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)) # Mostrar el error en la interfaz de Gradio
|
|
|
|
| 46 |
if not pdf_file:
|
| 47 |
raise gr.Error("No se subi贸 ning煤n archivo.")
|
| 48 |
|
| 49 |
+
# Crear archivos temporales para el procesamiento
|
| 50 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_input:
|
| 51 |
+
temp_input.write(pdf_file.read())
|
| 52 |
+
input_pdf = temp_input.name
|
| 53 |
|
| 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)
|
| 64 |
+
return gr.File(input_pdf, label="PDF Original"), texto_original, gr.File(output_pdf, label="PDF con OCR"), texto_ocr
|
| 65 |
+
except gr.Error as e:
|
| 66 |
+
if os.path.exists(input_pdf):
|
| 67 |
+
os.remove(input_pdf)
|
| 68 |
+
if os.path.exists(output_pdf):
|
| 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):
|
| 76 |
+
os.remove(output_pdf)
|
| 77 |
|
| 78 |
# Interfaz Gradio
|
| 79 |
with gr.Blocks() as interfaz:
|
| 80 |
gr.Markdown("## Procesador OCR para PDFs")
|
| 81 |
with gr.Row():
|
| 82 |
+
archivo_pdf = gr.File(label="Sube tu archivo PDF", file_types=[".pdf"])
|
| 83 |
idioma_ocr = gr.Dropdown(["spa", "eng", "fra", "deu"], label="Idioma OCR", value="spa")
|
| 84 |
boton_procesar = gr.Button("Procesar OCR")
|
| 85 |
with gr.Row():
|
|
|
|
| 94 |
outputs=[pdf_original_vista, texto_original, pdf_ocr_vista, texto_ocr]
|
| 95 |
)
|
| 96 |
|
| 97 |
+
interfaz.launch()
|