File size: 1,669 Bytes
1648a50
87cc209
 
 
 
1648a50
87cc209
 
338ecf0
 
 
 
 
 
 
 
1648a50
87cc209
 
 
1648a50
87cc209
 
338ecf0
 
87cc209
 
 
338ecf0
87cc209
 
1648a50
87cc209
338ecf0
 
87cc209
 
 
1648a50
338ecf0
87cc209
 
 
 
ce41633
87cc209
 
338ecf0
87cc209
 
 
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
42
43
44
45
46
47
48
49
50
51
import gradio as gr
from mineru_vl_utils.mineru_client import MinerUClient
from PIL import Image
import fitz  # PyMuPDF pour lire les PDFs
import os

# Init client
model_path = "opendatalab/MinerU2.5-2509-1.2B"
client = MinerUClient(
    backend="transformers",
    model_path=model_path,
    device="cuda"   # Utilisation GPU obligatoire
)

def extract_from_file(file, progress=gr.Progress()):
    progress(0, desc="Analyse du fichier...")

    # Vérifier si PDF ou image
    ext = os.path.splitext(file.name)[-1].lower()
    images = []

    if ext == ".pdf":
        doc = fitz.open(file.name)
        total_pages = len(doc)
        for i, page in enumerate(doc):
            pix = page.get_pixmap()
            img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
            images.append(img)
            progress((i+1)/total_pages, desc=f"Conversion page {i+1}/{total_pages}")
    else:
        images.append(Image.open(file.name))

    results = []
    for i, img in enumerate(images):
        progress(i/len(images), desc=f"Extraction page {i+1}/{len(images)}")
        blocks = client.two_step_extract(img)
        text_blocks = [b.text for b in blocks if hasattr(b, "text")]
        results.append("\n".join(text_blocks))

    progress(1, desc="Extraction terminée ✅")
    return "\n\n--- PAGE ---\n\n".join(results)

demo = gr.Interface(
    fn=extract_from_file,
    inputs=gr.File(type="filepath", label="Upload PDF or Image"),
    outputs=gr.Textbox(label="Extracted Text", lines=20),
    title="MinerU2.5 Document Extractor",
    description="Upload a PDF or Image to extract structured text using MinerU2.5 with GPU."
)

demo.launch()