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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Step 2: clean, fix, and split the audited dataset.
|
| 3 |
+
|
| 4 |
+
Input : /work/out/audit_table.csv (produced by audit.py)
|
| 5 |
+
Output: /work/out/clean_dataset.csv - all 1,024 unique rows with labels + splits
|
| 6 |
+
/work/out/report_splits.json - split stats, seen/unseen families, edit log
|
| 7 |
+
|
| 8 |
+
Policy (user-approved):
|
| 9 |
+
- drop exact and near duplicates (label 'dup')
|
| 10 |
+
- exclude 'fails' rows from training/evaluation (split left empty)
|
| 11 |
+
- one-token fixes, logged: merge duplicate -af flags (rows 127, 231)
|
| 12 |
+
- 80/10/10 train/val/test grouped by main filter/codec family
|
| 13 |
+
- rare families (<=2 rows) go entirely to test/val so the test set contains
|
| 14 |
+
families absent from train; reported separately as 'unseen'
|
| 15 |
+
- robustness slice: test prompts rewritten with different filenames (applied
|
| 16 |
+
consistently to prompt AND command) and rule-based rephrasing. Never applied
|
| 17 |
+
to train/val. Split tag 'test-robust'.
|
| 18 |
+
"""
|
| 19 |
+
import json
|
| 20 |
+
import random
|
| 21 |
+
import re
|
| 22 |
+
import shlex
|
| 23 |
+
from collections import Counter, defaultdict
|
| 24 |
+
|
| 25 |
+
import pandas as pd
|
| 26 |
+
|
| 27 |
+
OUT = "/work/out"
|
| 28 |
+
SEED = 42
|
| 29 |
+
rng = random.Random(SEED)
|
| 30 |
+
|
| 31 |
+
df = pd.read_csv(f"{OUT}/audit_table.csv")
|
| 32 |
+
df = df[df.label != "dup"].copy() # 1,024 unique rows
|
| 33 |
+
edits = []
|
| 34 |
+
|
| 35 |
+
# ---------------------------------------------------------------- fixes
|
| 36 |
+
AF_FIXES = {
|
| 37 |
+
127: ("-af atempo=0.8 -af aresample=44100", "-af atempo=0.8,aresample=44100"),
|
| 38 |
+
231: ("-af atempo=0.5 -af aresample=44100", "-af atempo=0.5,aresample=44100"),
|
| 39 |
+
}
|
| 40 |
+
for rid, (old, new) in AF_FIXES.items():
|
| 41 |
+
m = df.id == rid
|
| 42 |
+
if m.any() and old in df.loc[m, "ffmpeg_command"].iloc[0]:
|
| 43 |
+
df.loc[m, "ffmpeg_command"] = df.loc[m, "ffmpeg_command"].str.replace(old, new, regex=False)
|
| 44 |
+
edits.append({"id": int(rid), "change": f"{old!r} -> {new!r}",
|
| 45 |
+
"reason": "second -af overrode the first; merged into one chain"})
|
| 46 |
+
|
| 47 |
+
# ---------------------------------------------------------------- families
|
| 48 |
+
LAVFI = {"testsrc", "testsrc2", "sine", "color", "anullsrc", "mandelbrot", "smptebars",
|
| 49 |
+
"smptehdbars", "rgbtestsrc", "nullsrc", "gradients", "aevalsrc", "life", "cellauto"}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def family(cmd):
|
| 53 |
+
if "-f concat" in cmd:
|
| 54 |
+
return "concat"
|
| 55 |
+
for m in re.finditer(r"-v(?:f)?\s+(?:(['\"])(.*?)\1|(\S+))|-af\s+(?:(['\"])(.*?)\4|(\S+))|"
|
| 56 |
+
r"-filter_complex\s+(?:(['\"])(.*?)\7|(\S+))", cmd):
|
| 57 |
+
chain = next((g for g in m.groups() if g), "")
|
| 58 |
+
for part in re.split(r"[,;]", chain):
|
| 59 |
+
n = part.strip().split("=")[0].split("[")[0].strip()
|
| 60 |
+
if n and re.fullmatch(r"[a-z0-9_]+", n) and n not in LAVFI:
|
| 61 |
+
return n
|
| 62 |
+
enc = re.findall(r"-c:v\s+(\S+)|-c:a\s+(\S+)|-vcodec\s+(\S+)|-acodec\s+(\S+)", cmd)
|
| 63 |
+
for tup in enc:
|
| 64 |
+
v = next(x for x in tup if x)
|
| 65 |
+
if v != "copy":
|
| 66 |
+
return f"codec:{v}"
|
| 67 |
+
if any(t.split("=")[0].split(":")[0] in LAVFI for t in shlex.split(cmd)):
|
| 68 |
+
return "lavfi-source"
|
| 69 |
+
return "plain-conversion"
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
df["filter_family"] = df.ffmpeg_command.map(family)
|
| 73 |
+
|
| 74 |
+
# ---------------------------------------------------------------- splits
|
| 75 |
+
splittable = df[df.label.isin(["runs", "static-only"])].index.tolist()
|
| 76 |
+
by_fam = defaultdict(list)
|
| 77 |
+
for i in splittable:
|
| 78 |
+
by_fam[df.at[i, "filter_family"]].append(i)
|
| 79 |
+
|
| 80 |
+
df["split"] = ""
|
| 81 |
+
unseen_fams = []
|
| 82 |
+
for fam, idxs in sorted(by_fam.items(), key=lambda kv: -len(kv[1])):
|
| 83 |
+
n = len(idxs)
|
| 84 |
+
if n >= 10:
|
| 85 |
+
rng.shuffle(idxs)
|
| 86 |
+
n_tr = int(round(0.8 * n))
|
| 87 |
+
n_va = max(1, int(round(0.1 * n)))
|
| 88 |
+
for i in idxs[:n_tr]:
|
| 89 |
+
df.at[i, "split"] = "train"
|
| 90 |
+
for i in idxs[n_tr:n_tr + n_va]:
|
| 91 |
+
df.at[i, "split"] = "val"
|
| 92 |
+
for i in idxs[n_tr + n_va:]:
|
| 93 |
+
df.at[i, "split"] = "test"
|
| 94 |
+
elif n in (3, 4):
|
| 95 |
+
# tiny family: keep one example in train, rest to val/test
|
| 96 |
+
rng.shuffle(idxs)
|
| 97 |
+
df.at[idxs[0], "split"] = "train"
|
| 98 |
+
df.at[idxs[1], "split"] = "val" if n == 4 else "test"
|
| 99 |
+
if n == 4:
|
| 100 |
+
df.at[idxs[2], "split"] = "test"
|
| 101 |
+
unseen_fams.append(fam)
|
| 102 |
+
else: # n <= 2: a deliberate subset goes to test, absent from train;
|
| 103 |
+
# the rest stay in train so the long tail does not hollow it out
|
| 104 |
+
if rng.random() < 0.35:
|
| 105 |
+
for i in idxs:
|
| 106 |
+
df.at[i, "split"] = "test"
|
| 107 |
+
unseen_fams.append(fam)
|
| 108 |
+
else:
|
| 109 |
+
for i in idxs:
|
| 110 |
+
df.at[i, "split"] = "train"
|
| 111 |
+
|
| 112 |
+
# ---------------------------------------------------------------- robustness
|
| 113 |
+
FNAME_MAP = {
|
| 114 |
+
"video.mp4": "input_video.mp4", "movie.mp4": "film.mp4", "clip.mp4": "segment.mp4",
|
| 115 |
+
"audio.wav": "sound.wav", "song.wav": "track.wav", "voice.wav": "speech.wav",
|
| 116 |
+
"song.mp3": "track.mp3", "audio.mp3": "sound.mp3", "music.mp3": "audio_track.mp3",
|
| 117 |
+
"output.mp4": "result.mp4", "output.gif": "result.gif", "output.mp3": "result.mp3",
|
| 118 |
+
"output.wav": "result.wav", "output.avi": "result.avi", "output.mkv": "result.mkv",
|
| 119 |
+
"output.webm": "result.webm", "output.mov": "result.mov", "output.m4a": "result.m4a",
|
| 120 |
+
"output.jpg": "result.jpg", "output.png": "result.png", "output.flac": "result.flac",
|
| 121 |
+
"output.aac": "result.aac", "output.ogg": "result.ogg", "thumbnail.jpg": "poster.jpg",
|
| 122 |
+
"frame.jpg": "snapshot.jpg", "cover.jpg": "album_art.jpg", "image.jpg": "picture.jpg",
|
| 123 |
+
"image.png": "picture.png", "input.jpg": "source_image.jpg", "input.png": "source_image.png",
|
| 124 |
+
"input.mp4": "source_video.mp4", "logo.png": "branding.png", "watermark.png": "overlay_wm.png",
|
| 125 |
+
"subtitles.srt": "captions.srt", "video.gif": "animation.gif", "screenshot.png": "grab.png",
|
| 126 |
+
"slowmotion.mp4": "slowed.mp4", "reversed.mp4": "backwards.mp4", "cropped.mp4": "cut_video.mp4",
|
| 127 |
+
"resized.mp4": "scaled_video.mp4", "thumbnail.png": "poster.png", "highlight.mp4": "best_of.mp4",
|
| 128 |
+
"cat.png": "pet.png", "kick.wav": "drum.wav", "merged.mp4": "combined_video.mp4",
|
| 129 |
+
"video_with_alpha.mov": "alpha_source.mov", "rgba.mov": "alpha_source2.mov",
|
| 130 |
+
}
|
| 131 |
+
REPHRASE = [
|
| 132 |
+
(r"\bconvert (.+?) to ", r"change \1 into "),
|
| 133 |
+
(r"\bresize (.+?) to ", r"scale \1 to "),
|
| 134 |
+
(r"\bextract (.+?) from ", r"pull \1 out of "),
|
| 135 |
+
(r"\badd (.+?) to ", r"apply \1 onto "),
|
| 136 |
+
(r"\bcreate (.+?) from ", r"make \1 out of "),
|
| 137 |
+
(r"\bcreate (.+?) for ", r"make \1 for "),
|
| 138 |
+
(r"\bremove (.+?) from ", r"delete \1 from "),
|
| 139 |
+
(r"\btrim ", "cut "),
|
| 140 |
+
(r"\bmerge ", "join "),
|
| 141 |
+
(r"\bslow down ", "reduce the speed of "),
|
| 142 |
+
(r"\bspeed up ", "increase the speed of "),
|
| 143 |
+
(r"\brotate ", "turn "),
|
| 144 |
+
(r"\bflip ", "invert "),
|
| 145 |
+
(r"\bcrop ", "cut out a region from "),
|
| 146 |
+
(r"\bincrease ", "raise "),
|
| 147 |
+
(r"\bdecrease ", "lower "),
|
| 148 |
+
(r"\bcompress ", "shrink "),
|
| 149 |
+
(r"\breverse ", "play backwards "),
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def robust_variant(row):
|
| 154 |
+
"""Return (prompt, command, notes) or None if nothing applies."""
|
| 155 |
+
prompt, cmd = row.natural_language, row.ffmpeg_command
|
| 156 |
+
notes = []
|
| 157 |
+
# filename swaps, consistently in prompt and command
|
| 158 |
+
for old, new in FNAME_MAP.items():
|
| 159 |
+
if old in cmd:
|
| 160 |
+
cmd = cmd.replace(old, new)
|
| 161 |
+
if old in prompt:
|
| 162 |
+
prompt = prompt.replace(old, new)
|
| 163 |
+
notes.append(f"file {old}->{new}")
|
| 164 |
+
# rephrase: apply the first matching rule
|
| 165 |
+
for pat, rep in REPHRASE:
|
| 166 |
+
if re.search(pat, prompt):
|
| 167 |
+
prompt = re.sub(pat, rep, prompt, count=1)
|
| 168 |
+
notes.append(f"rephrase {pat}")
|
| 169 |
+
break
|
| 170 |
+
if not notes:
|
| 171 |
+
return None
|
| 172 |
+
return prompt, cmd, "; ".join(notes)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
robust_rows = []
|
| 176 |
+
for _, r in df[df.split == "test"].iterrows():
|
| 177 |
+
v = robust_variant(r)
|
| 178 |
+
if v is None:
|
| 179 |
+
continue
|
| 180 |
+
p, c, notes = v
|
| 181 |
+
if p == r.natural_language and c == r.ffmpeg_command:
|
| 182 |
+
continue
|
| 183 |
+
robust_rows.append({
|
| 184 |
+
"id": f"robust-{r.id}", "natural_language": p, "ffmpeg_command": c,
|
| 185 |
+
"label": r.label, "category": r.category, "filter_family": r.filter_family,
|
| 186 |
+
"split": "test-robust", "robustness_notes": notes, "source_id": int(r.id),
|
| 187 |
+
})
|
| 188 |
+
|
| 189 |
+
rob = pd.DataFrame(robust_rows)
|
| 190 |
+
for col in ("dup_of", "notes", "reason"):
|
| 191 |
+
if col not in rob.columns:
|
| 192 |
+
rob[col] = ""
|
| 193 |
+
df["robustness_notes"] = ""
|
| 194 |
+
df["source_id"] = ""
|
| 195 |
+
clean = pd.concat([df, rob], ignore_index=True)
|
| 196 |
+
clean.to_csv(f"{OUT}/clean_dataset.csv", index=False)
|
| 197 |
+
|
| 198 |
+
# ---------------------------------------------------------------- report
|
| 199 |
+
core = clean[clean.split.isin(["train", "val", "test"])]
|
| 200 |
+
rep = {
|
| 201 |
+
"unique_rows": len(df),
|
| 202 |
+
"excluded_fails": int((df.label == "fails").sum()),
|
| 203 |
+
"edits": edits,
|
| 204 |
+
"split_counts": core.split.value_counts().to_dict(),
|
| 205 |
+
"split_by_label": {f"{s}|{l}": n for (s, l), n in core.groupby(["split", "label"]).size().items()},
|
| 206 |
+
"families_total": len(by_fam),
|
| 207 |
+
"unseen_families_in_test": sorted(unseen_fams),
|
| 208 |
+
"unseen_family_rows": int((core.split == "test").sum() and sum(
|
| 209 |
+
len(by_fam[f]) for f in unseen_fams)),
|
| 210 |
+
"robustness_rows": len(rob),
|
| 211 |
+
"train_families": sorted(set(df[df.split == "train"].filter_family)),
|
| 212 |
+
}
|
| 213 |
+
with open(f"{OUT}/report_splits.json", "w") as f:
|
| 214 |
+
json.dump(rep, f, indent=2)
|
| 215 |
+
|
| 216 |
+
print(json.dumps({k: v for k, v in rep.items() if k != "train_families"}, indent=1))
|
| 217 |
+
print(f"train families ({len(rep['train_families'])}):", ", ".join(rep["train_families"][:25]), "...")
|
| 218 |
+
print("DONE")
|