Metal3d commited on
Commit
18f33a7
·
1 Parent(s): 614e089

Back to my working implementation

Browse files
Files changed (1) hide show
  1. labelizer/__init__.py +21 -17
labelizer/__init__.py CHANGED
@@ -1,28 +1,34 @@
1
  import torch
2
  from PIL import Image
3
- from transformers import AutoProcessor, AutoModelForCausalLM
4
 
5
  MODEL_ID = "ducviet00/Florence-2-large-hf"
6
 
7
  # Global variables for lazy loading
8
  _model = None
9
  _processor = None
 
 
10
 
11
 
12
  def _load_model():
13
  """Load model and processor lazily"""
14
- global _model, _processor
15
 
16
  if _model is None:
17
- device = "cuda" if torch.cuda.is_available() else "cpu"
18
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
19
-
20
- print(f"Loading model {MODEL_ID} on {device}...")
21
- _model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True)
22
- _processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
 
 
 
 
23
  print("Model loaded successfully!")
24
 
25
- return _model, _processor
26
 
27
 
28
  def get_task_response(task_prompt: str, image: Image.Image, text_input=None):
@@ -35,7 +41,7 @@ def get_task_response(task_prompt: str, image: Image.Image, text_input=None):
35
 
36
  """
37
  # Lazy load model only when needed
38
- model, processor = _load_model()
39
  if text_input is None:
40
  prompt = task_prompt
41
  else:
@@ -47,13 +53,11 @@ def get_task_response(task_prompt: str, image: Image.Image, text_input=None):
47
 
48
  if processor is None:
49
  raise ValueError("processor is None")
50
-
51
- # Process inputs using the correct API
52
- inputs = processor(text=prompt, images=image, return_tensors="pt")
53
-
54
- # Move inputs to device if model is on CUDA
55
- device = next(model.parameters()).device
56
- inputs = {k: v.to(device) for k, v in inputs.items()}
57
 
58
  generated_ids = model.generate(
59
  input_ids=inputs["input_ids"],
 
1
  import torch
2
  from PIL import Image
3
+ from transformers import Florence2ForConditionalGeneration, Florence2Processor
4
 
5
  MODEL_ID = "ducviet00/Florence-2-large-hf"
6
 
7
  # Global variables for lazy loading
8
  _model = None
9
  _processor = None
10
+ _device = None
11
+ _torch_dtype = None
12
 
13
 
14
  def _load_model():
15
  """Load model and processor lazily"""
16
+ global _model, _processor, _device, _torch_dtype
17
 
18
  if _model is None:
19
+ _device = "cuda:0" if torch.cuda.is_available() else "cpu"
20
+ _torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
21
+
22
+ print(f"Loading model {MODEL_ID} on {_device} with dtype {_torch_dtype}...")
23
+ _model = Florence2ForConditionalGeneration.from_pretrained(
24
+ MODEL_ID, torch_dtype=_torch_dtype, trust_remote_code=True
25
+ ).to(_device) # type: ignore
26
+ _processor = Florence2Processor.from_pretrained(
27
+ MODEL_ID, trust_remote_code=True
28
+ )
29
  print("Model loaded successfully!")
30
 
31
+ return _model, _processor, _device, _torch_dtype
32
 
33
 
34
  def get_task_response(task_prompt: str, image: Image.Image, text_input=None):
 
41
 
42
  """
43
  # Lazy load model only when needed
44
+ model, processor, device, torch_dtype = _load_model()
45
  if text_input is None:
46
  prompt = task_prompt
47
  else:
 
53
 
54
  if processor is None:
55
  raise ValueError("processor is None")
56
+ inputs = processor(
57
+ text=prompt,
58
+ images=image,
59
+ return_tensors="pt", # type: ignore
60
+ ).to(device, torch_dtype)
 
 
61
 
62
  generated_ids = model.generate(
63
  input_ids=inputs["input_ids"],