Abdulmateen commited on
Commit
5a65a07
·
verified ·
1 Parent(s): 2eccf7e

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +11 -9
handler.py CHANGED
@@ -34,30 +34,32 @@ class EndpointHandler:
34
  return {"error": f"Failed to decode or open base64 image: {e}"}
35
 
36
  if image is not None:
37
- # --- Case 1: Multimodal (Image + Text) - This part is correct ---
38
  print("Processing multimodal request...")
39
  prompt = f"USER: <image>\n{prompt_text} ASSISTANT:"
40
  inputs = self.processor(text=prompt, images=image, return_tensors="pt").to(self.model.device)
41
  else:
42
- # --- Case 2: Text-Only - This is the corrected logic ---
43
  print("Processing text-only request...")
44
  prompt = f"USER: {prompt_text} ASSISTANT:"
45
 
46
  # First, process the text to get input_ids
47
  inputs = self.processor(text=prompt, return_tensors="pt")
48
 
49
- # Second, create a dummy image tensor with the correct shape, type, and device.
50
- # This placeholder satisfies the model's input requirements.
51
  image_processor = self.processor.image_processor
 
 
 
52
  dummy_pixel_values = torch.zeros(
53
  (
54
  1,
55
- image_processor.num_channels,
56
- image_processor.size['height'],
57
- image_processor.size['width']
58
  ),
59
- dtype=self.model.dtype, # Use the model's dtype (e.g., float16)
60
- device=self.model.device # Put the tensor on the same device as the model
61
  )
62
 
63
  # Add the dummy tensor to the inputs dictionary
 
34
  return {"error": f"Failed to decode or open base64 image: {e}"}
35
 
36
  if image is not None:
37
+ # --- Case 1: Multimodal (Image + Text) ---
38
  print("Processing multimodal request...")
39
  prompt = f"USER: <image>\n{prompt_text} ASSISTANT:"
40
  inputs = self.processor(text=prompt, images=image, return_tensors="pt").to(self.model.device)
41
  else:
42
+ # --- Case 2: Text-Only - CORRECTED LOGIC ---
43
  print("Processing text-only request...")
44
  prompt = f"USER: {prompt_text} ASSISTANT:"
45
 
46
  # First, process the text to get input_ids
47
  inputs = self.processor(text=prompt, return_tensors="pt")
48
 
49
+ # --- THE FIX: Get image dimensions from the processor's .config ---
 
50
  image_processor = self.processor.image_processor
51
+ config = image_processor.config
52
+
53
+ # Create a dummy image tensor using the correct config values
54
  dummy_pixel_values = torch.zeros(
55
  (
56
  1,
57
+ config.num_channels,
58
+ config.crop_size['height'],
59
+ config.crop_size['width']
60
  ),
61
+ dtype=self.model.dtype,
62
+ device=self.model.device
63
  )
64
 
65
  # Add the dummy tensor to the inputs dictionary