Update app.py
Browse files
app.py
CHANGED
|
@@ -62,6 +62,26 @@ def to_wav(src: str, dst: str) -> bool:
|
|
| 62 |
return r.returncode == 0 and Path(dst).exists()
|
| 63 |
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def decode_b64(data: str) -> bytes:
|
| 66 |
try:
|
| 67 |
raw = base64.b64decode(data, validate=True)
|
|
@@ -156,21 +176,31 @@ def convert_messages(messages: list[dict], workdir: str):
|
|
| 156 |
|
| 157 |
|
| 158 |
def run_model(messages: list[dict], has_video: bool, max_new_tokens: int) -> str:
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
with model_lock, torch.no_grad():
|
| 176 |
out = model.generate(
|
|
|
|
| 62 |
return r.returncode == 0 and Path(dst).exists()
|
| 63 |
|
| 64 |
|
| 65 |
+
def video_has_audio(path: str) -> bool:
|
| 66 |
+
r = subprocess.run(
|
| 67 |
+
["ffprobe", "-v", "error", "-select_streams", "a",
|
| 68 |
+
"-show_entries", "stream=codec_type", "-of", "csv=p=0", path],
|
| 69 |
+
capture_output=True, text=True,
|
| 70 |
+
)
|
| 71 |
+
return bool(r.stdout.strip())
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def collect_video_paths(messages: list[dict]) -> list[str]:
|
| 75 |
+
paths = []
|
| 76 |
+
for m in messages:
|
| 77 |
+
content = m.get("content")
|
| 78 |
+
if isinstance(content, list):
|
| 79 |
+
for part in content:
|
| 80 |
+
if part.get("type") == "video":
|
| 81 |
+
paths.append(part["video"])
|
| 82 |
+
return paths
|
| 83 |
+
|
| 84 |
+
|
| 85 |
def decode_b64(data: str) -> bytes:
|
| 86 |
try:
|
| 87 |
raw = base64.b64decode(data, validate=True)
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
def run_model(messages: list[dict], has_video: bool, max_new_tokens: int) -> str:
|
| 179 |
+
videos = collect_video_paths(messages) if has_video else []
|
| 180 |
+
# Only ask for the soundtrack when every video actually has one.
|
| 181 |
+
use_audio = bool(videos) and all(video_has_audio(v) for v in videos)
|
| 182 |
+
audio_kwargs = (
|
| 183 |
+
{"load_audio_from_video": True, "processor_kwargs": {"use_audio_in_video": True}}
|
| 184 |
+
if use_audio else {}
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
def build_inputs(kwargs: dict):
|
| 188 |
+
return processor.apply_chat_template(
|
| 189 |
+
messages,
|
| 190 |
+
add_generation_prompt=True,
|
| 191 |
+
tokenize=True,
|
| 192 |
+
return_dict=True,
|
| 193 |
+
return_tensors="pt",
|
| 194 |
+
**kwargs,
|
| 195 |
+
).to(DEVICE)
|
| 196 |
+
|
| 197 |
+
try:
|
| 198 |
+
inputs = build_inputs(audio_kwargs)
|
| 199 |
+
except Exception:
|
| 200 |
+
if not videos:
|
| 201 |
+
raise
|
| 202 |
+
log.exception("Video preprocessing with audio failed; retrying with frames only")
|
| 203 |
+
inputs = build_inputs({})
|
| 204 |
|
| 205 |
with model_lock, torch.no_grad():
|
| 206 |
out = model.generate(
|