Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import fitz # PyMuPDF
|
@@ -45,22 +47,27 @@ def needs_ocr(doc):
|
|
45 |
|
46 |
@spaces.GPU
|
47 |
def convert(pdf_file):
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
-
|
62 |
-
metadata = {} # Puedes agregar metadatos aquí si lo necesitas
|
63 |
-
return markdown, metadata
|
64 |
|
65 |
gr.Interface(
|
66 |
fn=convert,
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import pytesseract
|
3 |
import spaces
|
4 |
import gradio as gr
|
5 |
import fitz # PyMuPDF
|
|
|
47 |
|
48 |
@spaces.GPU
|
49 |
def convert(pdf_file):
|
50 |
+
doc = fitz.open(pdf_file)
|
51 |
+
markdown_output = ""
|
52 |
+
image_counter = 1
|
53 |
+
|
54 |
+
for page_num in range(len(doc)):
|
55 |
+
page = doc[page_num]
|
56 |
+
text = page.get_text("text").strip()
|
57 |
|
58 |
+
if len(text) > 30:
|
59 |
+
# Página con texto normal
|
60 |
+
markdown_output += extract_text_markdown([page]) + "\n"
|
61 |
+
else:
|
62 |
+
# Página sin texto: usar OCR por imagen
|
63 |
+
pix = page.get_pixmap(dpi=300)
|
64 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
65 |
+
ocr_text = pytesseract.image_to_string(img, lang="spa")
|
66 |
+
markdown_output += ocr_text.strip() + "\n"
|
67 |
+
|
68 |
+
markdown_output += "\n---\n\n"
|
69 |
|
70 |
+
return markdown_output.strip(), {}
|
|
|
|
|
71 |
|
72 |
gr.Interface(
|
73 |
fn=convert,
|