TimeLens2
Collection
3 items • Updated • 1
How to use MCG-NJU/TimeLens2-8B with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("image-text-to-text", model="MCG-NJU/TimeLens2-8B")
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},
{"type": "text", "text": "What animal is on the candy?"}
]
},
]
pipe(text=messages) # Load model directly
from transformers import AutoProcessor, AutoModelForMultimodalLM
processor = AutoProcessor.from_pretrained("MCG-NJU/TimeLens2-8B")
model = AutoModelForMultimodalLM.from_pretrained("MCG-NJU/TimeLens2-8B")
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},
{"type": "text", "text": "What animal is on the candy?"}
]
},
]
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=40)
print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use MCG-NJU/TimeLens2-8B with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "MCG-NJU/TimeLens2-8B"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "MCG-NJU/TimeLens2-8B",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
]
}'docker model run hf.co/MCG-NJU/TimeLens2-8B
How to use MCG-NJU/TimeLens2-8B with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "MCG-NJU/TimeLens2-8B" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "MCG-NJU/TimeLens2-8B",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
]
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "MCG-NJU/TimeLens2-8B" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "MCG-NJU/TimeLens2-8B",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
]
}'How to use MCG-NJU/TimeLens2-8B with Docker Model Runner:
docker model run hf.co/MCG-NJU/TimeLens2-8B
TimeLens2-8B is a video multimodal large language model for temporal grounding. Given a video and a text query, it returns the time interval containing the relevant visual evidence.
The model is built on Qwen3-VL-8B-Instruct and achieves 48.0 average mIoU across seven temporal grounding benchmarks.
TimeLens2-8B sets a new state of the art on this seven-benchmark suite, with strong performance across short-, long-, and egocentric-video grounding.
pip install -U torch torchvision "transformers>=4.57.0" accelerate "qwen-vl-utils[decord]>=0.0.14"
pip install -U flash-attn --no-build-isolation
from pathlib import Path
from qwen_vl_utils import process_vision_info
from transformers import AutoModelForImageTextToText, AutoProcessor
model_id = "MCG-NJU/TimeLens2-8B"
video_path = "/path/to/video.mp4"
query = "A man opens the refrigerator."
model = AutoModelForImageTextToText.from_pretrained(
model_id,
torch_dtype="auto",
device_map="auto",
attn_implementation="flash_attention_2",
)
processor = AutoProcessor.from_pretrained(model_id)
prompt = (
f'Given the query: "{query}", return ALL time spans (in seconds) where the query is relevant.\n'
"Output format MUST be a JSON array of [start, end] pairs.\n"
)
messages = [
{
"role": "user",
"content": [
{
"type": "video",
"video": Path(video_path).resolve().as_uri(),
"fps": 2.0,
"min_pixels": 32 * 32,
"max_pixels": 480 * 480,
"total_pixels": 128000 * 32 * 32,
},
{"type": "text", "text": prompt},
],
}
]
text = processor.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
images, videos, video_kwargs = process_vision_info(
messages,
image_patch_size=16,
return_video_kwargs=True,
return_video_metadata=True,
)
if videos is not None:
videos, video_metadatas = zip(*videos)
videos, video_metadatas = list(videos), list(video_metadatas)
else:
video_metadatas = None
inputs = processor(
text=text,
images=images,
videos=videos,
video_metadata=video_metadatas,
do_resize=False,
return_tensors="pt",
**video_kwargs,
).to(model.device)
output_ids = model.generate(
**inputs,
max_new_tokens=4096,
temperature=0.01,
top_p=0.001,
top_k=1,
repetition_penalty=1.0,
)
output_ids = [
output[len(input_ids) :]
for input_ids, output in zip(inputs.input_ids, output_ids)
]
response = processor.batch_decode(
output_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)
print(response[0])
docker model run hf.co/MCG-NJU/TimeLens2-8B