CanerDedeoglu commited on
Commit
4284e59
·
verified ·
1 Parent(s): 637c5d5

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +55 -19
handler.py CHANGED
@@ -1,3 +1,4 @@
 
1
  # -*- coding: utf-8 -*-
2
  import os, io, sys, subprocess, base64
3
  from typing import Any, Dict, List, Optional
@@ -121,9 +122,11 @@ class EndpointHandler:
121
  # Sadece görüntü varsa image token'ları ekle
122
  if has_image:
123
  if self.use_im_start_end:
124
- content = f"{DEFAULT_IM_START_TOKEN}{DEFAULT_IMAGE_TOKEN}{DEFAULT_IM_END_TOKEN}\n{user_text}"
 
125
  else:
126
- content = f"{DEFAULT_IMAGE_TOKEN}\n{user_text}"
 
127
  else:
128
  # Görüntü yoksa sadece text
129
  content = user_text
@@ -163,7 +166,7 @@ class EndpointHandler:
163
  else:
164
  image_tensors = processed_images
165
 
166
- if image_tensors is not None:
167
  image_tensors = image_tensors.to(
168
  self.model.device,
169
  dtype=torch.float16,
@@ -172,29 +175,62 @@ class EndpointHandler:
172
  has_image = True
173
  print(f"[info] Image processed successfully, shape: {image_tensors.shape}")
174
  else:
175
- print("[warn] Image processing returned None")
 
 
176
 
177
  except Exception as e:
178
  print(f"[warn] image processing failed: {e}")
 
 
179
  image_tensors = None
180
  has_image = False
181
 
182
  # 2) Prompt oluştur (görüntü durumuna göre)
183
  prompt = self._build_prompt(query_text, conv_mode, has_image)
184
  print(f"[debug] Generated prompt: {repr(prompt[:200])}")
 
185
 
186
- # 3) Tokenize
187
- if has_image:
188
- # Görüntü varsa IMAGE_TOKEN_INDEX ile tokenize et
189
- input_ids = tokenizer_image_token(
190
- prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt"
191
- )
192
- else:
193
- # Görüntü yoksa normal tokenize
194
- input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids[0]
195
-
196
- # Batch dimension ekle
197
- input_ids = input_ids.unsqueeze(0).to(self.model.device, non_blocking=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  # Input uzunluk kontrolü
200
  if input_ids.shape[-1] > self.context_len - 100:
@@ -216,12 +252,12 @@ class EndpointHandler:
216
  "pad_token_id": self.tokenizer.eos_token_id,
217
  }
218
 
219
- # Görüntü varsa images parametresini ekle
220
- if has_image and image_tensors is not None:
221
  gen_kwargs["images"] = image_tensors
222
  print(f"[info] Using images in generation, shape: {image_tensors.shape}")
223
  else:
224
- print("[info] No images in generation")
225
 
226
  try:
227
  with torch.inference_mode():
 
1
+
2
  # -*- coding: utf-8 -*-
3
  import os, io, sys, subprocess, base64
4
  from typing import Any, Dict, List, Optional
 
122
  # Sadece görüntü varsa image token'ları ekle
123
  if has_image:
124
  if self.use_im_start_end:
125
+ # <image> tag'ini kullan - tokenizer_image_token bunu arar
126
+ content = f"{DEFAULT_IM_START_TOKEN}<image>{DEFAULT_IM_END_TOKEN}\n{user_text}"
127
  else:
128
+ # <image> tag'ini kullan - tokenizer_image_token bunu arar
129
+ content = f"<image>\n{user_text}"
130
  else:
131
  # Görüntü yoksa sadece text
132
  content = user_text
 
166
  else:
167
  image_tensors = processed_images
168
 
169
+ if image_tensors is not None and image_tensors.numel() > 0:
170
  image_tensors = image_tensors.to(
171
  self.model.device,
172
  dtype=torch.float16,
 
175
  has_image = True
176
  print(f"[info] Image processed successfully, shape: {image_tensors.shape}")
177
  else:
178
+ print("[warn] Image processing returned empty tensor")
179
+ image_tensors = None
180
+ has_image = False
181
 
182
  except Exception as e:
183
  print(f"[warn] image processing failed: {e}")
184
+ import traceback
185
+ traceback.print_exc()
186
  image_tensors = None
187
  has_image = False
188
 
189
  # 2) Prompt oluştur (görüntü durumuna göre)
190
  prompt = self._build_prompt(query_text, conv_mode, has_image)
191
  print(f"[debug] Generated prompt: {repr(prompt[:200])}")
192
+ print(f"[debug] Has image: {has_image}")
193
 
194
+ # 3) Tokenize - CRITICAL: <image> tag kontrolü
195
+ try:
196
+ if has_image and image_tensors is not None:
197
+ # Görüntü varsa IMAGE_TOKEN_INDEX ile tokenize et
198
+ # tokenizer_image_token fonksiyonu <image> tag'ini arar
199
+ input_ids = tokenizer_image_token(
200
+ prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt"
201
+ )
202
+ if input_ids.dim() == 1:
203
+ input_ids = input_ids.unsqueeze(0)
204
+
205
+ # <image> tag'inin prompt'ta olduğunu kontrol et
206
+ if '<image>' not in prompt:
207
+ print("[warn] <image> tag not found in prompt, switching to text-only mode")
208
+ has_image = False
209
+ image_tensors = None
210
+ input_ids = self.tokenizer(user_text, return_tensors="pt").input_ids
211
+ elif IMAGE_TOKEN_INDEX not in input_ids:
212
+ print(f"[warn] IMAGE_TOKEN_INDEX ({IMAGE_TOKEN_INDEX}) not found in input_ids after tokenization")
213
+ print(f"[debug] input_ids unique values: {torch.unique(input_ids)[:10]}...") # İlk 10 unique value
214
+ # Fallback: Normal tokenization
215
+ has_image = False
216
+ image_tensors = None
217
+ input_ids = self.tokenizer(user_text, return_tensors="pt").input_ids
218
+ else:
219
+ print(f"[info] Successfully tokenized with IMAGE_TOKEN_INDEX: {IMAGE_TOKEN_INDEX}")
220
+ else:
221
+ # Görüntü yoksa normal tokenize - sadece user_text kullan
222
+ input_ids = self.tokenizer(user_text, return_tensors="pt").input_ids
223
+
224
+ input_ids = input_ids.to(self.model.device, non_blocking=True)
225
+ print(f"[debug] input_ids shape: {input_ids.shape}")
226
+
227
+ except Exception as e:
228
+ print(f"[error] Tokenization failed: {e}")
229
+ # Fallback to text-only mode
230
+ has_image = False
231
+ image_tensors = None
232
+ input_ids = self.tokenizer(user_text, return_tensors="pt").input_ids
233
+ input_ids = input_ids.to(self.model.device, non_blocking=True)
234
 
235
  # Input uzunluk kontrolü
236
  if input_ids.shape[-1] > self.context_len - 100:
 
252
  "pad_token_id": self.tokenizer.eos_token_id,
253
  }
254
 
255
+ # CRITICAL: Sadece gerçekten geçerli görüntü tensors varsa ekle
256
+ if has_image and image_tensors is not None and IMAGE_TOKEN_INDEX in input_ids:
257
  gen_kwargs["images"] = image_tensors
258
  print(f"[info] Using images in generation, shape: {image_tensors.shape}")
259
  else:
260
+ print("[info] No images in generation - text-only mode")
261
 
262
  try:
263
  with torch.inference_mode():