Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,32 @@
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
import pytesseract
|
5 |
-
from PIL import Image
|
6 |
-
import os
|
7 |
|
8 |
@spaces.GPU
|
9 |
def convert(pdf_file):
|
10 |
-
|
11 |
markdown_output = ""
|
12 |
-
metadata = {} # Opcional: puedes extraer metadata con PyMuPDF si lo deseas
|
13 |
|
14 |
-
for
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
gr.Interface(
|
27 |
convert,
|
|
|
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: # texto
|
16 |
+
for line in b["lines"]:
|
17 |
+
for span in line["spans"]:
|
18 |
+
elements.append((span["bbox"][1], span["text"])) # y, texto
|
19 |
+
elif b["type"] == 1: # imagen
|
20 |
+
y_pos = b["bbox"][1]
|
21 |
+
elements.append((y_pos, "[imagen]()"))
|
22 |
|
23 |
+
# Ordenar por posici贸n vertical
|
24 |
+
elements.sort(key=lambda x: x[0])
|
25 |
+
|
26 |
+
for _, content in elements:
|
27 |
+
markdown_output += content.strip() + "\n\n"
|
28 |
+
|
29 |
+
return markdown_output.strip(), {}
|
30 |
|
31 |
gr.Interface(
|
32 |
convert,
|