Camera context improvements: image tokens before user text + system prompt
Browse files- Image tokens now placed BEFORE user text so model 'sees' image first
- System prompt added when camera active: 'Use visual context naturally,
do not describe unless asked'
- Helps model distinguish live camera feed from an image description task
- scripts/omni_o_call.py +14 -5
scripts/omni_o_call.py
CHANGED
|
@@ -33,11 +33,16 @@ def prep_image(b64):
|
|
| 33 |
img = Image.open(io.BytesIO(base64.b64decode(b64))).convert('RGB')
|
| 34 |
return {k: v.to(M['device']) for k, v in M['model'].vision_processor(images=img, return_tensors="pt").items()}
|
| 35 |
|
| 36 |
-
def build_ids(prompt, history):
|
| 37 |
tok, dev = M['tokenizer'], M['device']
|
| 38 |
cfg = M['cfg']
|
| 39 |
hist = history[-cfg.max_history_turns:] if cfg.max_history_turns > 0 else []
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
t = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
|
| 42 |
return torch.tensor(tok(t)['input_ids'], dtype=torch.long, device=dev)[None, ...]
|
| 43 |
|
|
@@ -113,7 +118,10 @@ def prepare_turn(text, samples, image_b64, do_asr_for_image):
|
|
| 113 |
pixel_values = prep_image(image_b64)
|
| 114 |
m = M['model']
|
| 115 |
img_tokens = m.config.image_special_token * m.config.image_token_len
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
| 117 |
return audio_inputs, audio_lens, pixel_values, prompt, user_text, asr_thread, asr_result
|
| 118 |
|
| 119 |
|
|
@@ -153,7 +161,7 @@ def init_web_app():
|
|
| 153 |
def gen():
|
| 154 |
audio_inputs, audio_lens, pixel_values, prompt, user_text, asr_th, asr_res = prepare_turn(
|
| 155 |
d.get('text', ''), samples, d.get('image'), do_asr_for_image=True)
|
| 156 |
-
x = build_ids(prompt, history)
|
| 157 |
asr_sent = False
|
| 158 |
if user_text and samples is not None and d.get('image'):
|
| 159 |
yield sse({'type': 'user_prompt', 'content': user_text}); asr_sent = True
|
|
@@ -252,10 +260,11 @@ def init_web_app():
|
|
| 252 |
session.generating = True
|
| 253 |
audio = session.get_audio()
|
| 254 |
ws.send(json.dumps({'type': 'generating'}))
|
|
|
|
| 255 |
audio_inputs, audio_lens, pixel_values, prompt, user_text, asr_th, asr_res = prepare_turn(
|
| 256 |
'', audio, state['image'], do_asr_for_image=True)
|
| 257 |
if state['image']: state['image'] = None
|
| 258 |
-
x = build_ids(prompt, state['history'])
|
| 259 |
va_rt = voice_args(state.get('voice', 'default'))
|
| 260 |
|
| 261 |
frames, full_text, interrupted = [], '', False
|
|
|
|
| 33 |
img = Image.open(io.BytesIO(base64.b64decode(b64))).convert('RGB')
|
| 34 |
return {k: v.to(M['device']) for k, v in M['model'].vision_processor(images=img, return_tensors="pt").items()}
|
| 35 |
|
| 36 |
+
def build_ids(prompt, history, image_context=False):
|
| 37 |
tok, dev = M['tokenizer'], M['device']
|
| 38 |
cfg = M['cfg']
|
| 39 |
hist = history[-cfg.max_history_turns:] if cfg.max_history_turns > 0 else []
|
| 40 |
+
if image_context:
|
| 41 |
+
sys_prompt = "You have live camera input. Use visual information as conversation context naturally. Do not describe what you see unless asked."
|
| 42 |
+
msgs = [{"role": "system", "content": sys_prompt}]
|
| 43 |
+
else:
|
| 44 |
+
msgs = []
|
| 45 |
+
msgs += hist + [{"role": "user", "content": prompt}]
|
| 46 |
t = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
|
| 47 |
return torch.tensor(tok(t)['input_ids'], dtype=torch.long, device=dev)[None, ...]
|
| 48 |
|
|
|
|
| 118 |
pixel_values = prep_image(image_b64)
|
| 119 |
m = M['model']
|
| 120 |
img_tokens = m.config.image_special_token * m.config.image_token_len
|
| 121 |
+
if prompt.strip():
|
| 122 |
+
prompt = img_tokens + "\n" + prompt
|
| 123 |
+
else:
|
| 124 |
+
prompt = img_tokens
|
| 125 |
return audio_inputs, audio_lens, pixel_values, prompt, user_text, asr_thread, asr_result
|
| 126 |
|
| 127 |
|
|
|
|
| 161 |
def gen():
|
| 162 |
audio_inputs, audio_lens, pixel_values, prompt, user_text, asr_th, asr_res = prepare_turn(
|
| 163 |
d.get('text', ''), samples, d.get('image'), do_asr_for_image=True)
|
| 164 |
+
x = build_ids(prompt, history, image_context=d.get('image') is not None)
|
| 165 |
asr_sent = False
|
| 166 |
if user_text and samples is not None and d.get('image'):
|
| 167 |
yield sse({'type': 'user_prompt', 'content': user_text}); asr_sent = True
|
|
|
|
| 260 |
session.generating = True
|
| 261 |
audio = session.get_audio()
|
| 262 |
ws.send(json.dumps({'type': 'generating'}))
|
| 263 |
+
has_image = state.get('image') is not None
|
| 264 |
audio_inputs, audio_lens, pixel_values, prompt, user_text, asr_th, asr_res = prepare_turn(
|
| 265 |
'', audio, state['image'], do_asr_for_image=True)
|
| 266 |
if state['image']: state['image'] = None
|
| 267 |
+
x = build_ids(prompt, state['history'], image_context=has_image)
|
| 268 |
va_rt = voice_args(state.get('voice', 'default'))
|
| 269 |
|
| 270 |
frames, full_text, interrupted = [], '', False
|