Spaces:
Build error
Build error
app.py
CHANGED
@@ -46,51 +46,48 @@ 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 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_output:
|
55 |
-
|
56 |
|
57 |
-
texto_original = leer_pdf(
|
58 |
|
59 |
try:
|
60 |
-
crear_pdf_con_texto_incrustado(
|
61 |
-
texto_ocr = leer_pdf(
|
62 |
-
return gr.File(
|
63 |
except gr.Error as e:
|
64 |
-
if os.path.exists(output_pdf):
|
65 |
-
os.remove(output_pdf)
|
66 |
raise e
|
67 |
finally:
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
|
73 |
# Interfaz Gradio
|
74 |
with gr.Blocks() as interfaz:
|
75 |
gr.Markdown("## Procesador OCR para PDFs")
|
76 |
-
|
77 |
with gr.Row():
|
78 |
archivo_pdf = gr.File(label="Sube tu archivo PDF")
|
79 |
idioma_ocr = gr.Dropdown(["spa", "eng", "fra", "deu"], label="Idioma OCR", value="spa")
|
80 |
boton_procesar = gr.Button("Procesar OCR")
|
81 |
-
|
82 |
with gr.Row():
|
83 |
texto_original = gr.Textbox(label="Texto Original", lines=10, interactive=False)
|
84 |
texto_ocr = gr.Textbox(label="Texto con OCR", lines=10, interactive=False)
|
85 |
-
|
86 |
with gr.Row():
|
87 |
pdf_original_vista = gr.File(label="Descargar PDF Original", interactive=False)
|
88 |
pdf_ocr_vista = gr.File(label="Descargar PDF con OCR", interactive=False)
|
89 |
-
|
90 |
boton_procesar.click(
|
91 |
fn=flujo_principal,
|
92 |
inputs=[archivo_pdf, idioma_ocr],
|
93 |
outputs=[pdf_original_vista, texto_original, pdf_ocr_vista, texto_ocr]
|
94 |
)
|
95 |
|
96 |
-
interfaz.launch(
|
|
|
46 |
if not pdf_file:
|
47 |
raise gr.Error("No se subi贸 ning煤n archivo.")
|
48 |
|
49 |
+
# Guardar el archivo subido en el directorio /tmp
|
50 |
+
temp_dir = tempfile.mkdtemp()
|
51 |
+
input_pdf_path = os.path.join(temp_dir, pdf_file.name)
|
52 |
+
with open(input_pdf_path, "wb") as f:
|
53 |
+
f.write(pdf_file.read())
|
54 |
|
55 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf", dir=temp_dir) as temp_output:
|
56 |
+
output_pdf_path = temp_output.name
|
57 |
|
58 |
+
texto_original = leer_pdf(input_pdf_path)
|
59 |
|
60 |
try:
|
61 |
+
crear_pdf_con_texto_incrustado(input_pdf_path, output_pdf_path, idioma)
|
62 |
+
texto_ocr = leer_pdf(output_pdf_path)
|
63 |
+
return gr.File(input_pdf_path, label="PDF Original"), texto_original, gr.File(output_pdf_path, label="PDF con OCR"), texto_ocr
|
64 |
except gr.Error as e:
|
|
|
|
|
65 |
raise e
|
66 |
finally:
|
67 |
+
# Limpiar archivos temporales
|
68 |
+
if os.path.exists(input_pdf_path):
|
69 |
+
os.remove(input_pdf_path)
|
70 |
+
if os.path.exists(output_pdf_path):
|
71 |
+
os.remove(output_pdf_path)
|
72 |
+
os.rmdir(temp_dir)
|
73 |
|
74 |
# Interfaz Gradio
|
75 |
with gr.Blocks() as interfaz:
|
76 |
gr.Markdown("## Procesador OCR para PDFs")
|
|
|
77 |
with gr.Row():
|
78 |
archivo_pdf = gr.File(label="Sube tu archivo PDF")
|
79 |
idioma_ocr = gr.Dropdown(["spa", "eng", "fra", "deu"], label="Idioma OCR", value="spa")
|
80 |
boton_procesar = gr.Button("Procesar OCR")
|
|
|
81 |
with gr.Row():
|
82 |
texto_original = gr.Textbox(label="Texto Original", lines=10, interactive=False)
|
83 |
texto_ocr = gr.Textbox(label="Texto con OCR", lines=10, interactive=False)
|
|
|
84 |
with gr.Row():
|
85 |
pdf_original_vista = gr.File(label="Descargar PDF Original", interactive=False)
|
86 |
pdf_ocr_vista = gr.File(label="Descargar PDF con OCR", interactive=False)
|
|
|
87 |
boton_procesar.click(
|
88 |
fn=flujo_principal,
|
89 |
inputs=[archivo_pdf, idioma_ocr],
|
90 |
outputs=[pdf_original_vista, texto_original, pdf_ocr_vista, texto_ocr]
|
91 |
)
|
92 |
|
93 |
+
interfaz.launch()
|