Video-Text-to-Text
Transformers
Safetensors
English
gemma4
image-text-to-text
video-captioning
multimodal
gemma
parakeet
Instructions to use SulphurAI/sulphur-caption with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use SulphurAI/sulphur-caption with Transformers:
# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("SulphurAI/sulphur-caption") model = AutoModelForMultimodalLM.from_pretrained("SulphurAI/sulphur-caption", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 8,351 Bytes
7011765 fd458d6 7011765 001fcfe 7011765 fd458d6 7011765 fd458d6 7011765 001fcfe 7011765 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | from __future__ import annotations
import sys
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
sys.path.insert(0, str(SCRIPT_DIR / "vllm"))
from vllm_caption_runtime import build_vllm_request, load_llm, load_processor, sampling_params
# =============================================================================
# Paths
# =============================================================================
INPUT_FOLDER = "/workspace/videos_to_caption"
OUTPUT_FOLDER = "/workspace/captions_out"
MODEL_DIR = str(SCRIPT_DIR / "model")
RECURSIVE = True
OVERWRITE_EXISTING = False
OUTPUT_EXTENSION = ".txt"
ERROR_EXTENSION = ".error.txt"
VIDEO_EXTENSIONS = (".mp4", ".mov", ".mkv", ".webm", ".avi", ".m4v")
# =============================================================================
# Prompt Settings
# =============================================================================
PROMPT_OVERRIDE = ""
CAPTION_LENGTH = "very large"
INCLUDE_WATERMARK_INFO = False
HAS_THINKING = True
VULGARITY = "low"
UNCERTAINTY = "low"
CHARACTER_NAMES = "none"
FLUFF = "none"
HAS_REPETITION = False
SPECULATION = "low"
TEMPORAL_DETAIL = "medium"
VISUAL_SPECIFICITY = "moderate"
CAMERA_DETAIL = "medium"
CAPTION_STYLE = "plain"
# =============================================================================
# vLLM / Generation Hyperparameters
# =============================================================================
NUM_FRAMES = 12
SAMPLING_RATE = 16_000
MAX_MODEL_LEN = 4096
MAX_NUM_SEQS = 20
BATCH_SIZE = 20
GPU_MEMORY_UTILIZATION = 0.88
DTYPE = "bfloat16"
# Online vLLM FP8 for Gemma linear weights. Parakeet is left unchanged.
USE_FP8 = True
FP8_QUANTIZATION = "fp8_per_tensor"
ENFORCE_EAGER = False
ENABLE_PREFIX_CACHING = False
TRUST_REMOTE_CODE = False
MAX_TOKENS = 1200
TEMPERATURE = 0.0
TOP_P = 0.9
REPETITION_PENALTY = 1.1
def prompt_settings() -> dict[str, object]:
return {
"caption_length": CAPTION_LENGTH,
"include_watermark_info": INCLUDE_WATERMARK_INFO,
"has_thinking": HAS_THINKING,
"vulgarity": VULGARITY,
"uncertainty": UNCERTAINTY,
"character_names": CHARACTER_NAMES,
"fluff": FLUFF,
"has_repetition": HAS_REPETITION,
"speculation": SPECULATION,
"temporal_detail": TEMPORAL_DETAIL,
"visual_specificity": VISUAL_SPECIFICITY,
"camera_detail": CAMERA_DETAIL,
"caption_style": CAPTION_STYLE,
}
def find_videos(input_folder: Path) -> list[Path]:
iterator = input_folder.rglob("*") if RECURSIVE else input_folder.glob("*")
videos = [
path
for path in iterator
if path.is_file() and path.suffix.lower() in VIDEO_EXTENSIONS
]
return sorted(videos)
def output_path_for(video_path: Path, input_folder: Path, output_folder: Path) -> Path:
relative = video_path.relative_to(input_folder)
return (output_folder / relative).with_suffix(OUTPUT_EXTENSION)
def write_text(path: Path, text: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text.rstrip() + "\n", encoding="utf-8")
def chunks(items: list[Path], size: int) -> list[list[Path]]:
return [items[index : index + size] for index in range(0, len(items), size)]
def find_json_end(text: str) -> int | None:
if not text.startswith("{"):
return None
depth = 0
in_string = False
escape = False
for index, char in enumerate(text):
if in_string:
if escape:
escape = False
elif char == "\\":
escape = True
elif char == '"':
in_string = False
continue
if char == '"':
in_string = True
elif char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
return index + 1
return None
def strip_caption_label(text: str) -> str:
caption = text.strip()
lowered = caption.lower()
for label in ("final caption", "final_caption", "caption", "final"):
if lowered == label:
return ""
if lowered.startswith(label):
rest = caption[len(label) :]
stripped = rest.lstrip()
if stripped.startswith(":"):
return stripped[1:].lstrip()
if stripped.startswith("- "):
return stripped[1:].lstrip()
if rest[:1].isspace():
return rest.lstrip()
return caption
def final_caption_from_raw_output(text: str) -> str:
raw = text.strip()
lowered = raw.lower()
for marker in ("thought", "thinking", "reasoning"):
if lowered == marker:
return ""
if lowered.startswith(marker):
rest = raw[len(marker) :].lstrip()
if rest.startswith(":"):
rest = rest[1:].lstrip()
raw = rest
break
json_end = find_json_end(raw)
if json_end is not None:
return strip_caption_label(raw[json_end:])
return strip_caption_label(raw)
def main() -> None:
quantization = FP8_QUANTIZATION if USE_FP8 else None
input_folder = Path(INPUT_FOLDER)
output_folder = Path(OUTPUT_FOLDER)
if not input_folder.is_dir():
raise RuntimeError(f"INPUT_FOLDER does not exist or is not a directory: {input_folder}")
videos = find_videos(input_folder)
if not videos:
print(f"No videos found in {input_folder}")
return
processor = load_processor(MODEL_DIR)
llm = load_llm(
model_dir=MODEL_DIR,
max_model_len=MAX_MODEL_LEN,
max_num_seqs=MAX_NUM_SEQS,
gpu_memory_utilization=GPU_MEMORY_UTILIZATION,
dtype=DTYPE,
quantization=quantization,
enforce_eager=ENFORCE_EAGER,
enable_prefix_caching=ENABLE_PREFIX_CACHING,
trust_remote_code=TRUST_REMOTE_CODE,
)
params = sampling_params(
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
top_p=TOP_P,
repetition_penalty=REPETITION_PENALTY,
)
done = 0
skipped = 0
failed = 0
settings = prompt_settings()
for batch in chunks(videos, BATCH_SIZE):
requests = []
request_videos = []
for video_path in batch:
output_path = output_path_for(video_path, input_folder, output_folder)
if output_path.exists() and not OVERWRITE_EXISTING:
skipped += 1
continue
try:
requests.append(
build_vllm_request(
processor=processor,
model_dir=MODEL_DIR,
video_path=video_path,
num_frames=NUM_FRAMES,
sampling_rate=SAMPLING_RATE,
prompt_override=PROMPT_OVERRIDE,
prompt_settings=settings,
)
)
request_videos.append(video_path)
except Exception as exc:
failed += 1
error_path = output_path.with_suffix(ERROR_EXTENSION)
write_text(error_path, f"{type(exc).__name__}: {exc}")
print(f"FAILED preprocess {video_path}: {exc}")
if not requests:
continue
try:
outputs = llm.generate(requests, sampling_params=params)
except Exception as exc:
failed += len(request_videos)
for video_path in request_videos:
output_path = output_path_for(video_path, input_folder, output_folder)
error_path = output_path.with_suffix(ERROR_EXTENSION)
write_text(error_path, f"{type(exc).__name__}: {exc}")
print(f"FAILED batch starting {request_videos[0]}: {exc}")
continue
for video_path, output in zip(request_videos, outputs, strict=True):
output_path = output_path_for(video_path, input_folder, output_folder)
raw_text = output.outputs[0].text if output.outputs else ""
text = final_caption_from_raw_output(raw_text)
write_text(output_path, text)
done += 1
print(f"WROTE {output_path}")
print(f"Done. wrote={done} skipped={skipped} failed={failed}")
if __name__ == "__main__":
main()
|