Spaces:
Runtime error
Runtime error
AndresIgnacio
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,129 +1,125 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
import
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from docx
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
)
|
127 |
-
|
128 |
-
# Ejecuta la aplicación
|
129 |
-
demo.launch(share=True)
|
|
|
1 |
+
import os
|
2 |
+
from pathlib import Path
|
3 |
+
from typing import Tuple, Union
|
4 |
+
import logging
|
5 |
+
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
6 |
+
from PIL import Image
|
7 |
+
from pdf2image import convert_from_path
|
8 |
+
from docx import Document
|
9 |
+
from docx.shared import Pt
|
10 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
# Configuración avanzada de logging
|
14 |
+
logging.basicConfig(
|
15 |
+
level=logging.DEBUG,
|
16 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
17 |
+
handlers=[
|
18 |
+
logging.FileHandler("app.log", mode="a", encoding="utf-8"),
|
19 |
+
logging.StreamHandler()
|
20 |
+
]
|
21 |
+
)
|
22 |
+
|
23 |
+
class HuggingFaceProcessor:
|
24 |
+
"""Clase para manejar modelos avanzados de Hugging Face para procesamiento de documentos."""
|
25 |
+
def __init__(self, model_name: str = "naver-clova-ix/donut-base-finetuned-docvqa"):
|
26 |
+
self.logger = logging.getLogger("HuggingFaceProcessor")
|
27 |
+
self.logger.info("Cargando modelo de Hugging Face...")
|
28 |
+
try:
|
29 |
+
self.processor = DonutProcessor.from_pretrained(model_name)
|
30 |
+
self.model = VisionEncoderDecoderModel.from_pretrained(model_name)
|
31 |
+
except Exception as e:
|
32 |
+
self.logger.error(f"Error cargando el modelo: {e}")
|
33 |
+
raise
|
34 |
+
|
35 |
+
def process_image(self, image: Image.Image) -> str:
|
36 |
+
"""Procesa una imagen y extrae texto usando el modelo Donut."""
|
37 |
+
try:
|
38 |
+
pixel_values = self.processor(image, return_tensors="pt").pixel_values
|
39 |
+
outputs = self.model.generate(pixel_values, max_length=512)
|
40 |
+
result = self.processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
41 |
+
return result.strip()
|
42 |
+
except Exception as e:
|
43 |
+
self.logger.error(f"Error procesando la imagen con Donut: {e}")
|
44 |
+
return ""
|
45 |
+
|
46 |
+
class PDFToWordProcessor:
|
47 |
+
"""Procesa un PDF escaneado y genera un documento Word."""
|
48 |
+
def __init__(self):
|
49 |
+
self.logger = logging.getLogger("PDFToWordProcessor")
|
50 |
+
self.hf_processor = HuggingFaceProcessor()
|
51 |
+
|
52 |
+
def process_pdf(self, file_path: Path) -> Document:
|
53 |
+
"""Convierte un PDF a un documento Word."""
|
54 |
+
self.logger.info(f"Procesando PDF: {file_path}")
|
55 |
+
doc = Document()
|
56 |
+
|
57 |
+
try:
|
58 |
+
# Convertir cada página del PDF a imagen
|
59 |
+
images = convert_from_path(file_path)
|
60 |
+
|
61 |
+
for page_num, image in enumerate(images, start=1):
|
62 |
+
self.logger.debug(f"Procesando página {page_num}")
|
63 |
+
|
64 |
+
# Extraer texto usando el modelo Donut
|
65 |
+
page_text = self.hf_processor.process_image(image)
|
66 |
+
|
67 |
+
# Agregar encabezado para cada página
|
68 |
+
doc.add_heading(f"Página {page_num}", level=2)
|
69 |
+
|
70 |
+
# Agregar texto extraído al documento Word
|
71 |
+
self._add_text_to_doc(doc, page_text)
|
72 |
+
|
73 |
+
except Exception as e:
|
74 |
+
self.logger.error(f"Error procesando PDF: {e}")
|
75 |
+
raise
|
76 |
+
|
77 |
+
return doc
|
78 |
+
|
79 |
+
def _add_text_to_doc(self, doc: Document, text: str):
|
80 |
+
"""Agrega texto extraído al documento Word."""
|
81 |
+
for line in text.split('\n'):
|
82 |
+
if line.strip(): # Evitar líneas vacías
|
83 |
+
paragraph = doc.add_paragraph(line.strip(), style="Normal")
|
84 |
+
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
|
85 |
+
|
86 |
+
def process_file(self, file_path: Union[str, Path]) -> Tuple[str, str]:
|
87 |
+
"""Procesa un archivo PDF y guarda el documento Word generado."""
|
88 |
+
file_path = Path(file_path)
|
89 |
+
output_path = file_path.with_suffix(".docx")
|
90 |
+
|
91 |
+
try:
|
92 |
+
if file_path.suffix.lower() != ".pdf":
|
93 |
+
raise ValueError(f"Formato no soportado: {file_path.suffix}")
|
94 |
+
|
95 |
+
doc = self.process_pdf(file_path)
|
96 |
+
doc.save(output_path)
|
97 |
+
return "Documento procesado exitosamente", str(output_path)
|
98 |
+
except Exception as e:
|
99 |
+
return f"Error: {e}", ""
|
100 |
+
|
101 |
+
def create_interface():
|
102 |
+
"""Crea la interfaz de usuario con Gradio."""
|
103 |
+
processor = PDFToWordProcessor()
|
104 |
+
|
105 |
+
def process_file(file):
|
106 |
+
if not file:
|
107 |
+
return "Por favor, seleccione un archivo", None
|
108 |
+
return processor.process_file(file.name)
|
109 |
+
|
110 |
+
with gr.Blocks(title="Procesador de PDF a Word") as demo:
|
111 |
+
gr.Markdown("# Procesador PDF a Word con Hugging Face")
|
112 |
+
gr.Markdown("Convierte documentos PDF escaneados a Word utilizando modelos avanzados de Hugging Face.")
|
113 |
+
|
114 |
+
file_input = gr.File(label="Seleccionar PDF", file_types=[".pdf"], type="filepath")
|
115 |
+
process_button = gr.Button("Procesar", variant="primary")
|
116 |
+
output_text = gr.Textbox(label="Estado del Proceso")
|
117 |
+
output_file = gr.File(label="Documento Procesado")
|
118 |
+
|
119 |
+
process_button.click(process_file, inputs=[file_input], outputs=[output_text, output_file])
|
120 |
+
|
121 |
+
return demo
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
demo = create_interface()
|
125 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|