Spaces:
Running
Running
File size: 1,035 Bytes
145d936 d4b4544 b697ac0 145d936 312add7 d4b4544 3920f3b c1d7645 d4b4544 145d936 d4b4544 145d936 d4b4544 145d936 c1d7645 145d936 |
1 2 3 4 5 6 7 8 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 |
import spaces
import gradio as gr
import fitz # PyMuPDF
@spaces.GPU
def convert(pdf_file):
doc = fitz.open(pdf_file)
markdown_output = ""
for page in doc:
blocks = page.get_text("dict")["blocks"]
elements = []
for b in blocks:
if b["type"] == 0: # texto
for line in b["lines"]:
for span in line["spans"]:
elements.append((span["bbox"][1], span["text"])) # y, texto
elif b["type"] == 1: # imagen
y_pos = b["bbox"][1]
elements.append((y_pos, "[imagen]()"))
# Ordenar por posición vertical
elements.sort(key=lambda x: x[0])
for _, content in elements:
markdown_output += content.strip() + "\n\n"
return markdown_output.strip(), {}
gr.Interface(
convert,
inputs=[
gr.File(label="Upload PDF", type="filepath"),
],
outputs=[
gr.Text(label="Markdown"),
gr.JSON(label="Metadata"),
],
).launch()
|