Spaces:
Running
on
Zero
Running
on
Zero
Create model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoProcessor, VisionEncoderDecoderModel
|
| 3 |
+
from huggingface_hub import snapshot_download
|
| 4 |
+
from qwen_vl_utils import process_vision_info
|
| 5 |
+
|
| 6 |
+
def load_model(model_name):
|
| 7 |
+
"""
|
| 8 |
+
Load the specified model and its processor based on the model name.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
model_name (str): Name of the model ("dots.ocr" or "Dolphin").
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
tuple: (model, processor) for the specified model.
|
| 15 |
+
"""
|
| 16 |
+
if model_name == "dots.ocr":
|
| 17 |
+
model_id = "rednote-hilab/dots.ocr"
|
| 18 |
+
model_path = "./models/dots-ocr-local"
|
| 19 |
+
snapshot_download(
|
| 20 |
+
repo_id=model_id,
|
| 21 |
+
local_dir=model_path,
|
| 22 |
+
local_dir_use_symlinks=False,
|
| 23 |
+
)
|
| 24 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
+
model_path,
|
| 26 |
+
attn_implementation="flash_attention_2",
|
| 27 |
+
torch_dtype=torch.bfloat16,
|
| 28 |
+
device_map="auto",
|
| 29 |
+
trust_remote_code=True
|
| 30 |
+
)
|
| 31 |
+
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
|
| 32 |
+
elif model_name == "Dolphin":
|
| 33 |
+
model_id = "ByteDance/Dolphin"
|
| 34 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 35 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_id)
|
| 36 |
+
model.eval()
|
| 37 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 38 |
+
model.to(device)
|
| 39 |
+
model = model.half() # Use half precision
|
| 40 |
+
else:
|
| 41 |
+
raise ValueError(f"Unknown model: {model_name}")
|
| 42 |
+
return model, processor
|
| 43 |
+
|
| 44 |
+
def inference_dots_ocr(model, processor, image, prompt, max_new_tokens):
|
| 45 |
+
"""
|
| 46 |
+
Perform inference using the dots.ocr model.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
model: The loaded dots.ocr model.
|
| 50 |
+
processor: The corresponding processor.
|
| 51 |
+
image (PIL.Image): Input image.
|
| 52 |
+
prompt (str): Prompt for inference.
|
| 53 |
+
max_new_tokens (int): Maximum number of tokens to generate.
|
| 54 |
+
|
| 55 |
+
Returns:
|
| 56 |
+
str: Generated text output.
|
| 57 |
+
"""
|
| 58 |
+
messages = [
|
| 59 |
+
{
|
| 60 |
+
"role": "user",
|
| 61 |
+
"content": [
|
| 62 |
+
{"type": "image", "image": image},
|
| 63 |
+
{"type": "text", "text": prompt}
|
| 64 |
+
]
|
| 65 |
+
}
|
| 66 |
+
]
|
| 67 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 68 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 69 |
+
inputs = processor(
|
| 70 |
+
text=[text],
|
| 71 |
+
images=image_inputs,
|
| 72 |
+
videos=video_inputs,
|
| 73 |
+
padding=True,
|
| 74 |
+
return_tensors="pt",
|
| 75 |
+
)
|
| 76 |
+
inputs = inputs.to(model.device)
|
| 77 |
+
with torch.no_grad():
|
| 78 |
+
generated_ids = model.generate(
|
| 79 |
+
**inputs,
|
| 80 |
+
max_new_tokens=max_new_tokens,
|
| 81 |
+
do_sample=False,
|
| 82 |
+
temperature=0.1
|
| 83 |
+
)
|
| 84 |
+
generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
|
| 85 |
+
output_text = processor.batch_decode(
|
| 86 |
+
generated_ids_trimmed,
|
| 87 |
+
skip_special_tokens=True,
|
| 88 |
+
clean_up_tokenization_spaces=False
|
| 89 |
+
)
|
| 90 |
+
return output_text[0] if output_text else ""
|
| 91 |
+
|
| 92 |
+
def inference_dolphin(model, processor, image):
|
| 93 |
+
"""
|
| 94 |
+
Perform inference using the Dolphin model.
|
| 95 |
+
|
| 96 |
+
Args:
|
| 97 |
+
model: The loaded Dolphin model.
|
| 98 |
+
processor: The corresponding processor.
|
| 99 |
+
image (PIL.Image): Input image.
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
str: Generated text output.
|
| 103 |
+
"""
|
| 104 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values.to(model.device).half()
|
| 105 |
+
generated_ids = model.generate(pixel_values)
|
| 106 |
+
generated_text = processor.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 107 |
+
return generated_text
|