multimodalart HF Staff commited on
Commit
6c5be39
·
verified ·
1 Parent(s): 4f55c10

Use the repo's reference transformers inference path (apply_chat_template tokenize=True)

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -25,7 +25,6 @@ import gradio as gr # noqa: E402
25
  import imageio.v2 as imageio # noqa: E402
26
  import numpy as np # noqa: E402
27
  import torch # noqa: E402
28
- from molmo_utils import process_vision_info # noqa: E402
29
  from transformers import AutoModelForImageTextToText, AutoProcessor # noqa: E402
30
 
31
  MODEL_ID = "tidalove/Molmo2Fish"
@@ -38,6 +37,7 @@ SAMPLING_FPS = 2
38
 
39
  TRACK_STYLE = "video_point_track_per_frame"
40
  DEFAULT_PROMPT = "track all fish"
 
41
 
42
  # html-v2 pointing format, exactly as in olmo/preprocessing/point_formatter.py
43
  COORD_RE = re.compile(r"<(?:points|tracks).*? coords=\"([0-9\t:;, .]+)\"/?>")
@@ -167,14 +167,10 @@ def build_messages(video_path: str, turns: list) -> list:
167
  for i, (user_text, assistant_text) in enumerate(turns):
168
  content = [dict(type="text", text=user_text, style=TRACK_STYLE)]
169
  if i == 0:
170
- content.append(dict(
171
- type="video",
172
- video=video_path,
173
- num_frames=NUM_FRAMES,
174
- frame_sample_mode=FRAME_SAMPLE_MODE,
175
- max_fps=MAX_FPS,
176
- sampling_fps=SAMPLING_FPS,
177
- ))
178
  messages.append({"role": "user", "content": content})
179
  if assistant_text is not None:
180
  messages.append({"role": "assistant",
@@ -184,18 +180,21 @@ def build_messages(video_path: str, turns: list) -> list:
184
 
185
  def run_model(video_path: str, turns: list, max_new_tokens: int) -> str:
186
  messages = build_messages(video_path, turns)
187
- _, videos, video_kwargs = process_vision_info(messages)
188
- frames, metadatas = zip(*videos)
189
- text = processor.apply_chat_template(
190
- messages, tokenize=False, add_generation_prompt=True
191
- )
192
- inputs = processor(
193
- videos=list(frames),
194
- video_metadata=list(metadatas),
195
- text=text,
196
- padding=True,
197
  return_tensors="pt",
198
- **video_kwargs,
 
 
 
 
 
 
 
199
  )
200
  inputs = {k: (v.to(model.device) if hasattr(v, "to") else v)
201
  for k, v in inputs.items()}
 
25
  import imageio.v2 as imageio # noqa: E402
26
  import numpy as np # noqa: E402
27
  import torch # noqa: E402
 
28
  from transformers import AutoModelForImageTextToText, AutoProcessor # noqa: E402
29
 
30
  MODEL_ID = "tidalove/Molmo2Fish"
 
37
 
38
  TRACK_STYLE = "video_point_track_per_frame"
39
  DEFAULT_PROMPT = "track all fish"
40
+ IM_END_TOKEN_ID = 151937 # <im_end>; config.image_end_token_id
41
 
42
  # html-v2 pointing format, exactly as in olmo/preprocessing/point_formatter.py
43
  COORD_RE = re.compile(r"<(?:points|tracks).*? coords=\"([0-9\t:;, .]+)\"/?>")
 
167
  for i, (user_text, assistant_text) in enumerate(turns):
168
  content = [dict(type="text", text=user_text, style=TRACK_STYLE)]
169
  if i == 0:
170
+ # Frame sampling (num_frames=128, uniform_last_frame, max_fps/sampling_fps=2)
171
+ # comes from the model's own video_preprocessor_config.json, so the path is
172
+ # all the processor needs — same as olmo/hf_model/test_molmo2.py.
173
+ content.append(dict(type="video", video=video_path))
 
 
 
 
174
  messages.append({"role": "user", "content": content})
175
  if assistant_text is not None:
176
  messages.append({"role": "assistant",
 
180
 
181
  def run_model(video_path: str, turns: list, max_new_tokens: int) -> str:
182
  messages = build_messages(video_path, turns)
183
+ # Reference path from the repo's own olmo/hf_model/test_molmo2.py: let the
184
+ # Molmo2Processor decode + sample the video and expand <|video|> itself.
185
+ inputs = processor.apply_chat_template(
186
+ messages,
187
+ tokenize=True,
188
+ add_generation_prompt=True,
 
 
 
 
189
  return_tensors="pt",
190
+ return_dict=True,
191
+ )
192
+ ids = inputs["input_ids"]
193
+ print(
194
+ f"[molmo2fish] input_ids={tuple(ids.shape)} "
195
+ f"im_end={int((ids == IM_END_TOKEN_ID).sum())} "
196
+ f"keys={sorted(inputs.keys())}",
197
+ flush=True,
198
  )
199
  inputs = {k: (v.to(model.device) if hasattr(v, "to") else v)
200
  for k, v in inputs.items()}