Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from mineru_vl_utils.mineru_client import MinerUClient
|
| 3 |
from PIL import Image
|
| 4 |
-
import fitz # PyMuPDF
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
-
# Init client
|
| 8 |
model_path = "opendatalab/MinerU2.5-2509-1.2B"
|
|
|
|
|
|
|
| 9 |
client = MinerUClient(backend="transformers", model_path=model_path)
|
| 10 |
|
| 11 |
def extract_from_file(file):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
demo = gr.Interface(
|
| 34 |
fn=extract_from_file,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from mineru_vl_utils.mineru_client import MinerUClient
|
| 3 |
from PIL import Image
|
| 4 |
+
import fitz # PyMuPDF
|
| 5 |
import os
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
# Init client
|
| 9 |
model_path = "opendatalab/MinerU2.5-2509-1.2B"
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
print(f"⚡ Loading MinerU on device: {device}")
|
| 12 |
client = MinerUClient(backend="transformers", model_path=model_path)
|
| 13 |
|
| 14 |
def extract_from_file(file):
|
| 15 |
+
try:
|
| 16 |
+
print(f"📄 Processing file: {file.name}")
|
| 17 |
+
ext = os.path.splitext(file.name)[-1].lower()
|
| 18 |
+
images = []
|
| 19 |
+
|
| 20 |
+
if ext == ".pdf":
|
| 21 |
+
doc = fitz.open(file.name)
|
| 22 |
+
for i, page in enumerate(doc):
|
| 23 |
+
pix = page.get_pixmap()
|
| 24 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 25 |
+
images.append(img)
|
| 26 |
+
print(f"✅ Converted page {i+1} to image")
|
| 27 |
+
else:
|
| 28 |
+
images.append(Image.open(file.name))
|
| 29 |
+
|
| 30 |
+
results = []
|
| 31 |
+
for i, img in enumerate(images):
|
| 32 |
+
print(f"🔍 Extracting text from page {i+1}")
|
| 33 |
+
blocks = client.two_step_extract(img)
|
| 34 |
+
if not blocks:
|
| 35 |
+
print(f"⚠️ No blocks found on page {i+1}")
|
| 36 |
+
results.append("[EMPTY PAGE]")
|
| 37 |
+
continue
|
| 38 |
+
|
| 39 |
+
text_blocks = []
|
| 40 |
+
for b in blocks:
|
| 41 |
+
if hasattr(b, "text") and b.text:
|
| 42 |
+
text_blocks.append(b.text)
|
| 43 |
+
|
| 44 |
+
page_text = "\n".join(text_blocks) if text_blocks else "[NO TEXT FOUND]"
|
| 45 |
+
results.append(page_text)
|
| 46 |
+
print(f"✅ Page {i+1} extracted")
|
| 47 |
+
|
| 48 |
+
return "\n\n--- PAGE ---\n\n".join(results)
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"❌ ERROR: {e}")
|
| 52 |
+
return f"Error during extraction: {str(e)}"
|
| 53 |
|
| 54 |
demo = gr.Interface(
|
| 55 |
fn=extract_from_file,
|