Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,66 +12,59 @@
|
|
| 12 |
# if __name__ == "__main__":
|
| 13 |
# demo.launch()
|
| 14 |
|
| 15 |
-
import
|
|
|
|
| 16 |
import torch
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# ------------------------------
|
| 33 |
-
# Load Model + Processor
|
| 34 |
-
# ------------------------------
|
| 35 |
-
model_id = "rednote/dots-ocr"
|
| 36 |
-
|
| 37 |
-
print("🔄 Loading processor...")
|
| 38 |
-
_base_proc = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 39 |
-
processor = ImageOnlyProcessor(_base_proc)
|
| 40 |
-
|
| 41 |
-
print("🔄 Loading model (may take time)...")
|
| 42 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 43 |
-
model_id,
|
| 44 |
-
trust_remote_code=True,
|
| 45 |
-
torch_dtype=torch.float16,
|
| 46 |
-
device_map="auto"
|
| 47 |
-
)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
demo = gr.Interface(
|
| 69 |
-
fn=
|
| 70 |
inputs=gr.Image(type="pil"),
|
| 71 |
outputs="text",
|
| 72 |
-
title="
|
| 73 |
-
description="
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
demo.launch(
|
|
|
|
|
|
| 12 |
# if __name__ == "__main__":
|
| 13 |
# demo.launch()
|
| 14 |
|
| 15 |
+
import os
|
| 16 |
+
from PIL import Image
|
| 17 |
import torch
|
| 18 |
+
import gradio as gr
|
| 19 |
+
|
| 20 |
+
# Make sure you have the local model folder (e.g., "DotsOCR") with all files from the repo
|
| 21 |
+
LOCAL_MODEL_PATH = "./DotsOCR"
|
| 22 |
+
|
| 23 |
+
# Import the model and processor code locally
|
| 24 |
+
import sys
|
| 25 |
+
sys.path.append(LOCAL_MODEL_PATH)
|
| 26 |
+
|
| 27 |
+
from modeling_dots_ocr import DotsOCRForVision2Text
|
| 28 |
+
from configuration_dots import DotsOCRConfig
|
| 29 |
+
from transformers import PreTrainedTokenizerFast
|
| 30 |
+
|
| 31 |
+
# Load tokenizer
|
| 32 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained(LOCAL_MODEL_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Load model configuration
|
| 35 |
+
config = DotsOCRConfig.from_pretrained(LOCAL_MODEL_PATH)
|
| 36 |
+
|
| 37 |
+
# Load model
|
| 38 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 39 |
+
model = DotsOCRForVision2Text.from_pretrained(LOCAL_MODEL_PATH, config=config)
|
| 40 |
+
model.to(device)
|
| 41 |
+
model.eval()
|
| 42 |
+
|
| 43 |
+
# Load only the image processor
|
| 44 |
+
from transformers import AutoFeatureExtractor
|
| 45 |
+
image_processor = AutoFeatureExtractor.from_pretrained(LOCAL_MODEL_PATH)
|
| 46 |
+
|
| 47 |
+
def parse_document(image: Image.Image):
|
| 48 |
+
# Preprocess the image
|
| 49 |
+
inputs = image_processor(images=image, return_tensors="pt").to(device)
|
| 50 |
+
|
| 51 |
+
# Forward pass
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
output_ids = model.generate(**inputs, max_new_tokens=1024)
|
| 54 |
+
|
| 55 |
+
# Decode output
|
| 56 |
+
text = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
|
| 57 |
+
return text
|
| 58 |
+
|
| 59 |
+
# Gradio demo
|
| 60 |
demo = gr.Interface(
|
| 61 |
+
fn=parse_document,
|
| 62 |
inputs=gr.Image(type="pil"),
|
| 63 |
outputs="text",
|
| 64 |
+
title="dots.ocr Document Parser",
|
| 65 |
+
description="Parse text from images using dots.ocr"
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|
| 70 |
+
|