Update handler.py
Browse files- 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) -
|
| 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 -
|
| 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 |
-
#
|
| 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 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
),
|
| 59 |
-
dtype=self.model.dtype,
|
| 60 |
-
device=self.model.device
|
| 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
|