Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,48 @@
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import fitz # PyMuPDF
|
|
|
4 |
|
5 |
@spaces.GPU
|
6 |
def convert(pdf_file):
|
7 |
doc = fitz.open(pdf_file)
|
8 |
markdown_output = ""
|
|
|
|
|
|
|
9 |
|
10 |
-
for page in doc:
|
11 |
blocks = page.get_text("dict")["blocks"]
|
12 |
elements = []
|
13 |
|
14 |
for b in blocks:
|
15 |
-
if b["type"] == 0: #
|
16 |
for line in b["lines"]:
|
17 |
for span in line["spans"]:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
elements.sort(key=lambda x: x[0])
|
25 |
|
26 |
for _, content in elements:
|
27 |
-
markdown_output += content
|
28 |
|
29 |
return markdown_output.strip(), {}
|
30 |
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import fitz # PyMuPDF
|
4 |
+
import os
|
5 |
|
6 |
@spaces.GPU
|
7 |
def convert(pdf_file):
|
8 |
doc = fitz.open(pdf_file)
|
9 |
markdown_output = ""
|
10 |
+
image_dir = "extracted_images"
|
11 |
+
os.makedirs(image_dir, exist_ok=True)
|
12 |
+
image_counter = 0
|
13 |
|
14 |
+
for page_number, page in enumerate(doc):
|
15 |
blocks = page.get_text("dict")["blocks"]
|
16 |
elements = []
|
17 |
|
18 |
for b in blocks:
|
19 |
+
if b["type"] == 0: # Texto
|
20 |
for line in b["lines"]:
|
21 |
for span in line["spans"]:
|
22 |
+
y = span["bbox"][1]
|
23 |
+
text = span["text"]
|
24 |
+
elements.append((y, text.strip()))
|
25 |
+
elif b["type"] == 1: # Imagen
|
26 |
+
y = b["bbox"][1]
|
27 |
+
img = page.get_image_list(full=True)
|
28 |
+
if img:
|
29 |
+
xref = img[0][0]
|
30 |
+
pix = fitz.Pixmap(doc, xref)
|
31 |
+
img_path = os.path.join(image_dir, f"imagen_{image_counter}.png")
|
32 |
|
33 |
+
if pix.n > 4: # CMYK
|
34 |
+
pix = fitz.Pixmap(fitz.csRGB, pix)
|
35 |
+
pix.save(img_path)
|
36 |
+
pix = None
|
37 |
+
|
38 |
+
elements.append((y, f""))
|
39 |
+
image_counter += 1
|
40 |
+
|
41 |
+
# Ordenar por posici贸n vertical (y)
|
42 |
elements.sort(key=lambda x: x[0])
|
43 |
|
44 |
for _, content in elements:
|
45 |
+
markdown_output += content + "\n\n"
|
46 |
|
47 |
return markdown_output.strip(), {}
|
48 |
|