Biifruu commited on
Commit
beb65ba
verified
1 Parent(s): d4b4544

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
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: # 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
 
 
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"![imagen]({img_path})"))
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