Remove gearcut_ground.py from public space (moved to private repo)
Browse files- gearcut_ground.py +0 -112
gearcut_ground.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
GearCut — operation grounding (constrained-decoding safety layer).
|
| 3 |
-
|
| 4 |
-
Takes the operations the model produced + the user's instruction, and forces the
|
| 5 |
-
output to be CONSISTENT with what the user actually wrote:
|
| 6 |
-
|
| 7 |
-
* numbers must come from the instruction (kills hallucinated values like 600)
|
| 8 |
-
* filenames must come from the instruction (fixes out.mp4 -> output.mp4 drift)
|
| 9 |
-
* presets / ops / clip ids must be valid
|
| 10 |
-
* a number-requiring op with no number to ground is REJECTED (and reported),
|
| 11 |
-
so a misunderstanding becomes a visible warning, never a wrong silent edit.
|
| 12 |
-
|
| 13 |
-
Pure standard library — no torch. Fully unit-tested.
|
| 14 |
-
"""
|
| 15 |
-
import re
|
| 16 |
-
|
| 17 |
-
KNOWN_OPS = {"trim", "split", "import", "append", "delete", "reorder", "export"}
|
| 18 |
-
PRESETS = {"youtube_1080p", "tiktok_vertical", "instagram_square", "web_720p", "master_4k"}
|
| 19 |
-
NUM_FIELDS = ("seconds", "at", "start", "end")
|
| 20 |
-
|
| 21 |
-
_FILE_RE = re.compile(r"\b[\w-]+\.(?:mp4|mov|webm|mkv|avi|m4v|mp3|wav|aac)\b", re.I)
|
| 22 |
-
_NUM_RE = re.compile(r"\d+(?:\.\d+)?")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def _to_float(x):
|
| 26 |
-
try:
|
| 27 |
-
return float(x)
|
| 28 |
-
except (TypeError, ValueError):
|
| 29 |
-
return None
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def instruction_facts(instruction: str):
|
| 33 |
-
"""Extract the numbers, filenames and presets the user actually wrote."""
|
| 34 |
-
files = _FILE_RE.findall(instruction)
|
| 35 |
-
# strip filenames and clip references first so their digits
|
| 36 |
-
# (test2.mp4, "clip 1") don't get mistaken for editing numbers
|
| 37 |
-
text = _FILE_RE.sub(" ", instruction)
|
| 38 |
-
text = re.sub(r"\bclips?\s*\d+", " ", text, flags=re.I) # "clip 1", "clips 2"
|
| 39 |
-
text = re.sub(r"\bc\d+\b", " ", text, flags=re.I) # "c1", "c2"
|
| 40 |
-
numbers = [float(m) for m in _NUM_RE.findall(text)]
|
| 41 |
-
presets = [p for p in PRESETS if p in instruction]
|
| 42 |
-
return numbers, files, presets
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def ground_operations(ops, instruction, clip_ids=None):
|
| 46 |
-
"""Return (grounded_ops, repairs). `repairs` is a list of human-readable notes."""
|
| 47 |
-
repairs = []
|
| 48 |
-
if not isinstance(ops, list):
|
| 49 |
-
return [], ["model output was not a list of operations"]
|
| 50 |
-
|
| 51 |
-
numbers, files, presets = instruction_facts(instruction)
|
| 52 |
-
unused = list(numbers) # instruction numbers still available to assign
|
| 53 |
-
clip_ids = set(clip_ids or [])
|
| 54 |
-
grounded = []
|
| 55 |
-
|
| 56 |
-
def take_match(v): # consume an instruction number equal to v
|
| 57 |
-
for i, x in enumerate(unused):
|
| 58 |
-
if abs(x - v) < 0.01:
|
| 59 |
-
return unused.pop(i)
|
| 60 |
-
return None
|
| 61 |
-
|
| 62 |
-
for op in ops:
|
| 63 |
-
if not isinstance(op, dict) or op.get("op") not in KNOWN_OPS:
|
| 64 |
-
repairs.append(f"dropped unrecognized op: {op!r}")
|
| 65 |
-
continue
|
| 66 |
-
op = dict(op)
|
| 67 |
-
kind = op["op"]
|
| 68 |
-
drop = False
|
| 69 |
-
|
| 70 |
-
# 1) ground numeric fields against the instruction's numbers
|
| 71 |
-
for key in NUM_FIELDS:
|
| 72 |
-
if key not in op:
|
| 73 |
-
continue
|
| 74 |
-
v = _to_float(op[key])
|
| 75 |
-
if v is not None and take_match(v) is not None:
|
| 76 |
-
op[key] = v # the model copied a real number — keep it
|
| 77 |
-
elif unused:
|
| 78 |
-
new = unused.pop(0) # hallucinated/wrong -> next instruction number
|
| 79 |
-
repairs.append(f"{kind}.{key}: {op[key]} -> {new} (grounded to instruction)")
|
| 80 |
-
op[key] = new
|
| 81 |
-
else:
|
| 82 |
-
drop = True # no number to ground -> unsafe, reject
|
| 83 |
-
repairs.append(f"rejected {kind}: '{key}={op[key]}' has no matching number "
|
| 84 |
-
f"in the instruction (possible misread)")
|
| 85 |
-
break
|
| 86 |
-
if drop:
|
| 87 |
-
continue
|
| 88 |
-
|
| 89 |
-
# 2) ground export path + preset
|
| 90 |
-
if kind == "export":
|
| 91 |
-
if files and op.get("path") not in files:
|
| 92 |
-
repairs.append(f"export.path: {op.get('path')!r} -> {files[-1]!r} (from instruction)")
|
| 93 |
-
op["path"] = files[-1]
|
| 94 |
-
if op.get("preset") not in PRESETS:
|
| 95 |
-
op["preset"] = presets[0] if presets else "youtube_1080p"
|
| 96 |
-
repairs.append(f"export.preset -> {op['preset']}")
|
| 97 |
-
elif presets and op["preset"] not in presets:
|
| 98 |
-
repairs.append(f"export.preset: {op['preset']} -> {presets[0]} (from instruction)")
|
| 99 |
-
op["preset"] = presets[0]
|
| 100 |
-
|
| 101 |
-
# 3) ground import source
|
| 102 |
-
if kind == "import" and files and op.get("source") not in files:
|
| 103 |
-
repairs.append(f"import.source: {op.get('source')!r} -> {files[0]!r} (from instruction)")
|
| 104 |
-
op["source"] = files[0]
|
| 105 |
-
|
| 106 |
-
# 4) sanity-check clip id (don't auto-fix; flag so the app can warn)
|
| 107 |
-
if "clip" in op and clip_ids and op["clip"] not in clip_ids:
|
| 108 |
-
repairs.append(f"warning: {kind} refers to unknown clip {op['clip']!r}")
|
| 109 |
-
|
| 110 |
-
grounded.append(op)
|
| 111 |
-
|
| 112 |
-
return grounded, repairs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|