Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from mineru_vl_utils import MinerUClient
|
| 4 |
+
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
|
| 5 |
+
|
| 6 |
+
# Charger le modèle MinerU
|
| 7 |
+
model_path = "opendatalab/MinerU2.5-2509-1.2B"
|
| 8 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 9 |
+
model_path,
|
| 10 |
+
torch_dtype="auto",
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
processor = AutoProcessor.from_pretrained(model_path, use_fast=True)
|
| 14 |
+
|
| 15 |
+
client = MinerUClient(
|
| 16 |
+
backend="transformers",
|
| 17 |
+
model=model,
|
| 18 |
+
processor=processor
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def extract_from_image(image):
|
| 22 |
+
# Conversion si nécessaire
|
| 23 |
+
if not isinstance(image, Image.Image):
|
| 24 |
+
image = Image.fromarray(image)
|
| 25 |
+
|
| 26 |
+
# Extraction
|
| 27 |
+
blocks = client.two_step_extract(image)
|
| 28 |
+
# On retourne le texte concaténé
|
| 29 |
+
extracted_text = "\n".join([b.text for b in blocks if hasattr(b, "text")])
|
| 30 |
+
return extracted_text
|
| 31 |
+
|
| 32 |
+
# Interface Gradio
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=extract_from_image,
|
| 35 |
+
inputs=gr.Image(type="pil"),
|
| 36 |
+
outputs="text",
|
| 37 |
+
title="MinerU2.5 - Document Extract",
|
| 38 |
+
description="Upload an image or PDF page and extract structured text with MinerU2.5"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
demo.launch()
|