File size: 2,233 Bytes
52e44f1
 
18f33a7
52e44f1
 
 
 
 
 
18f33a7
 
52e44f1
 
 
 
18f33a7
52e44f1
 
18f33a7
 
 
 
 
d5f1e98
18f33a7
 
d5f1e98
18f33a7
52e44f1
 
18f33a7
52e44f1
 
 
 
 
 
 
 
 
 
 
 
18f33a7
52e44f1
 
 
 
 
 
 
 
 
 
 
18f33a7
 
 
 
 
52e44f1
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import torch
from PIL import Image
from transformers import Florence2ForConditionalGeneration, Florence2Processor

MODEL_ID = "ducviet00/Florence-2-large-hf"

# Global variables for lazy loading
_model = None
_processor = None
_device = None
_torch_dtype = None


def _load_model():
    """Load model and processor lazily"""
    global _model, _processor, _device, _torch_dtype

    if _model is None:
        _device = "cuda:0" if torch.cuda.is_available() else "cpu"
        _torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

        print(f"Loading model {MODEL_ID} on {_device} with dtype {_torch_dtype}...")
        _model = Florence2ForConditionalGeneration.from_pretrained(
            MODEL_ID, dtype=_torch_dtype, trust_remote_code=True
        ).to(_device)  # type: ignore
        _processor = Florence2Processor.from_pretrained(
            MODEL_ID, trust_remote_code=True, use_fast=True
        )
        print("Model loaded successfully!")

    return _model, _processor, _device, _torch_dtype


def get_task_response(task_prompt: str, image: Image.Image, text_input=None):
    """Return associated task response

    Task can be:
        '<MORE_DETAILED_CAPTION>'
        '<DETAILED_CAPTION>'
        '<CAPTION>'

    """
    # Lazy load model only when needed
    model, processor, device, torch_dtype = _load_model()
    if text_input is None:
        prompt = task_prompt
    else:
        prompt = task_prompt + text_input

    # Ensure image is in RGB mode
    if image.mode != "RGB":
        image = image.convert("RGB")

    if processor is None:
        raise ValueError("processor is None")
    inputs = processor(
        text=prompt,
        images=image,
        return_tensors="pt",  # type: ignore
    ).to(device, torch_dtype)

    generated_ids = model.generate(
        input_ids=inputs["input_ids"],
        pixel_values=inputs["pixel_values"],
        max_new_tokens=1024,
        num_beams=3,
    )
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]

    parsed_answer = processor.post_process_generation(
        generated_text, task=task_prompt, image_size=(image.width, image.height)
    )

    return parsed_answer[task_prompt]