Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.
Split long-video descriptions without replaying the story in every part
Bowen Xue1989604139c1
diff --git a/freevideo_engine/chain_runner.py b/freevideo_engine/chain_runner.pyindex ad3ce62..d492875 100644--- a/freevideo_engine/chain_runner.py+++ b/freevideo_engine/chain_runner.py@@ -13,6 +13,28 @@ from . import chain_leads, chain_planfrom . import chain_store as store+def _split_value(record, text):+ """What the prompt model sorts into parts: the whole description and each part's new seconds."""+ first = next(row for row in record['blocks'] if row['index'] == 0)+ return dict(text=text, continued=first['after'] is not None,+ seconds=[row['new_frames'] / chain_plan.FPS for row in record['blocks']])+++def _ordered_texts(record, value):+ """Part texts when the description could not be split: its sentences in order, with the LoRA trigger+ words every part carries, or None for a description without sentences."""+ from .lora_triggers import apply_chain_triggers+ from .prompt_vlm.rules import order_split+ if not isinstance(value['text'], str):+ return None+ try:+ probe = dict(snapshot=deepcopy(record['snapshot']), node=record['node'],+ blocks=[dict(inputs=dict(text=text)) for text in order_split(value)])+ apply_chain_triggers(probe)+ except Exception:+ return None+ return [row['inputs']['text'] for row in probe['blocks']]+class ChainBusy(ValueError):pass@@ -373,10 +395,14 @@ class ChainRunner:fraction=0., remaining_seconds=None, continuation=None,skip_frames=0, skip_audio_samples=0)split = record.get('split')+ whole = record['snapshot']['prompt'][record['node']]['inputs'].get('text')if (split is not None and split['state'] == 'failed' and split['reason'] in ('busy', 'memory')and all(row['state'] != 'done' for row in record['blocks'])- and len({row['inputs']['text'] for row in record['blocks']}) == 1):+ and [row['inputs']['text'] for row in record['blocks']] in (+ [whole] * len(record['blocks']), _ordered_texts(record, _split_value(record, whole)))):# Busy and memory pass; nothing has started and no part was rewritten, so split again.+ for row in record['blocks']:+ row['inputs']['text'] = wholesplit.update(state='pending', reason=None)if client_id is not None:record['client_id'] = client_id@@ -722,8 +748,7 @@ class ChainRunner:split['state'] = 'running'record['state'] = 'running'self._save(record)- value = dict(text=block['inputs']['text'], continued=block['after'] is not None,- seconds=[row['new_frames'] / chain_plan.FPS for row in record['blocks']])+ value = _split_value(record, block['inputs']['text'])action = 'split'elif prompt_id is None:self.progress_messages.pop(identity, None)@@ -851,9 +876,13 @@ class ChainRunner:row['inputs']['text'] = value['text']record['split'].update(state='failed', reason='failed')else:- # The whole description goes to every part, as before; the page says why.+ # Without the prompt model the sentences go to the parts in order, so no part plays the whole+ # story again; the page says why. A description without sentences goes to every part.if parts is not None:reason = 'unusable'+ texts = _ordered_texts(record, value)+ for row, text in zip(record['blocks'], texts or [value['text']] * len(record['blocks'])):+ row['inputs']['text'] = textrecord['split'].update(state='failed', reason=reason if reason in store.SPLIT_REASONS else 'failed')self._save(record)diff --git a/freevideo_engine/chain_store.py b/freevideo_engine/chain_store.pyindex 7dd2390..cd4bb63 100644--- a/freevideo_engine/chain_store.py+++ b/freevideo_engine/chain_store.py@@ -19,12 +19,12 @@ OUTPUT_STATES = ('none', 'pending', 'joining', 'done', 'failed')# pending: split before the first part; given: the request gave one text per part; none: nothing to split.SPLIT_STATES = ('pending', 'running', 'done', 'failed', 'given', 'none')SPLIT_REASONS = {- 'model_missing': 'The prompt model is not installed, so every part used the whole description.',- 'busy': 'The prompt model was busy, so every part used the whole description.',- 'memory': 'There was not enough memory to split the description, so every part used the whole description.',- 'unusable': 'The description could not be split into parts, so every part used the whole description.',- 'unavailable': 'Splitting is not available here, so every part used the whole description.',- 'failed': 'Splitting the description failed, so every part used the whole description.',+ 'model_missing': 'The prompt model is not installed, so the parts took the sentences of the description in order.',+ 'busy': 'The prompt model was busy, so the parts took the sentences of the description in order.',+ 'memory': 'There was not enough memory to split the description, so the parts took the sentences of the description in order.',+ 'unusable': 'The description could not be split into parts, so the parts took the sentences of the description in order.',+ 'unavailable': 'Splitting is not available here, so the parts took the sentences of the description in order.',+ 'failed': 'Splitting the description failed, so the parts took the sentences of the description in order.',}# Parts one video may be continued through: the first part and seven continuations (fewer with a short part).MAX_PARTS = chain_plan.MAX_PARTSdiff --git a/freevideo_engine/prompt_vlm/rules.py b/freevideo_engine/prompt_vlm/rules.pyindex 1f36d93..20b6579 100644--- a/freevideo_engine/prompt_vlm/rules.py+++ b/freevideo_engine/prompt_vlm/rules.py@@ -547,12 +547,15 @@ def split_instruction(value):return f"""A long video is made of {count} parts generated one after another; each part continues where theprevious one ended. Sort the user's description into what stays the same and what happens in each part:{chr(10).join(rows)}-Shared: who is in the scene and how they look, the place, the lighting, the camera, the style and the-background sound. Copy these words from the description. Leave out every action, event, change, every [L]-marker and all talking: how someone speaks belongs next to their marker.+Shared: only what stays the same in every part: who is in the scene and how they look, the place, the+lighting, the camera framing, the style and the background sound. Copy these words from the description.+Leave out every action, movement, event and change, camera moves, anything that only appears later, every+[L] marker and all talking: how someone speaks belongs next to their marker. Shared never retells the story.Part N: only what happens during that part, in the order of the description, so each action happensonce. Part 1 {opening}; each later part picks up where the previous part ended. When the story is-shorter than the video, the later parts go on with small, calm, silent actions instead of repeating it.+shorter than the video, the later parts carry it on with a simple new development that follows from it,+such as a next small action or a new view of the scene; they never repeat an earlier action and add no+speech. Every part has something of its own.The markers [L1], [L2], ... stand for spoken lines; their speaking time is given. Put each marker inexactly one part, in order, where it is said, as "he says [L2]" or "她说 [L2]"; spread them so eachpart's speech fits its seconds. Do not write any other speech: nobody talks, keeps talking or goes@@ -625,26 +628,97 @@ def spoken(line, words):return ('说:' if language(words) in ('Chinese', 'Japanese') else 'says: ') + line+def clauses(text):+ """Sentences, and the comma-separated pieces inside them, in order. A period before a digit (00:04.000)+ or a letter does not end one."""+ pieces = re.findall(r'.+?(?:[。!?!?;;,,]+[”"’』」)]*|\.(?![\w])[”"’』」)]*|$)', text, re.S)+ return [piece.strip() for piece in pieces if piece.strip()]+++def sentences(body):+ """Sentences in order; a spoken line [L1] ends one. A period before a digit or a letter does not."""+ return [piece.strip() for piece in re.findall(+ r'.+?(?:\[L\d+\][。!?!?.,,]*|[。!?!?]+[”"’』」)]*|\.(?![\w])[”"’』」)]*|$)', ' '.join(body.split()), re.S)+ if piece.strip()]+++def wide(words):+ """Mostly Chinese, Japanese or Korean letters."""+ letters = re.findall(r'[^\W\d_]', words)+ return sum(bool(re.match('[\u3040-\u30ff\u4e00-\u9fff\uac00-\ud7af]', c)) for c in letters) * 2 > len(letters)+++def action(words):+ """Long enough to be an action rather than a name: six Chinese or Japanese characters, or twelve letters."""+ return len(words) >= (6 if re.search('[\u3040-\u30ff\u4e00-\u9fff\uac00-\ud7af]', words) else 12)+++def quiet(body):+ return '画面继续,没有人说话。' if language(body) == 'Chinese' else 'The scene goes on and nobody speaks.'++def validate_split(text, value, *, strict=False):"""Part texts built from the model's sorting; every spoken line kept once, in order.- A part with nothing new carries on from the previous one; a misplaced line is placed by speaking time.- Only an answer without the expected layout or a usable shared description fails; strict also- fails when the model did not place every line once and in order, so it can be asked again.+ Shared is what every part repeats, so an action in it is played again in every part: words of Shared+ that also stand in a part, or in the description after its opening sentence, are story and are left+ out, and a Shared that holds a spoken line or a whole part is the story itself and fails, as does an+ answer in another language than the description. Strict also fails on such words, on a part with+ nothing of its own and on lines not placed once and in order, so the model can be asked again;+ otherwise a misplaced line is placed by speaking time and an empty later part carries on quietly."""body, sound, lines = part_source(value['text'])shared, events = split_answer(text, len(value['seconds']))- shared = ' '.join(re.sub(r'\[L\d+\]', '', shared).split())+ if re.search(r'\[L\d+\]', shared) or wide(body) != wide(shared + ' '.join(events)):+ raise ValueError('invalid_output')+ used = set()+ def once(match):+ # A marker written twice stays where it first stands.+ if match.group(0) in used:+ return ''+ used.add(match.group(0))+ return match.group(0)+ events = [' '.join(re.sub(r'\[L\d+\]', once, event).split()) for event in events]+ keys = [plain(re.sub(r'\[L\d+\]', '', event)) for event in events]+ if any(len(event) > 4000 for event in events) or any(key and (key in plain(shared) or key == plain(body))+ for key in keys):+ raise ValueError('invalid_output')+ # Clauses after the opening sentence, each with whether it stands in the first half of the description.+ told_at, story = 0, []+ for index, sentence in enumerate(sentences(body)):+ for piece in clauses(sentence):+ words = plain(re.sub(r'\[L\d+\]', '', piece))+ if index and action(words):+ story.append((words, told_at < len(body) / 2))+ told_at += len(piece)if not 20 <= len(shared) <= 4000:raise ValueError('invalid_output')- seen, cleaned = {plain(shared), plain(body)}, []- for event in events:- key = plain(re.sub(r'\[L\d+\]', '', event))- if len(event) > 4000 or not key or key in seen or key in plain(shared):+ def placed_twice(piece):+ return action(plain(piece)) and any(plain(piece) in key for key in keys)+ def told(piece):+ return next((early for words, early in story if action(plain(piece))+ and (plain(piece) in words or words in plain(piece))), None)+ if strict and any(placed_twice(piece) for piece in clauses(shared)):+ raise ValueError('invalid_output')+ join = '' if wide(shared) else ' '+ kept = [piece for piece in clauses(shared) if not placed_twice(piece) and told(piece) is None]+ # Story the model left in Shared alone: its first half opens the video, the rest is not repeated.+ opening = [piece for piece in clauses(shared) if not placed_twice(piece) and told(piece)]+ shared = ' '.join(join.join(kept).split()).rstrip(',,;;')+ if opening:+ lead = ' '.join(join.join(opening).split()).rstrip(',,;;。.') + ('。' if join == '' else '.')+ events[0] = join.join([lead[:1].upper() + lead[1:], events[0]]).strip()+ keys[0] = plain(re.sub(r'\[L\d+\]', '', events[0]))+ seen, cleaned = [], []+ for event, key in zip(events, keys):+ if any(key in earlier for earlier in seen):key = ''- seen.add(key)+ seen.append(key)cleaned.append(event if key else ' '.join(re.findall(r'\[L\d+\]', event)))events = cleaned+ if not re.sub(r'\[L\d+\]', '', events[0]).strip() or strict and not all(+ re.sub(r'\[L\d+\]', '', event).strip() for event in events):+ raise ValueError('invalid_output')placed = [int(n) for event in events for n in re.findall(r'\[L(\d+)\]', event)]if placed != list(range(1, len(lines) + 1)) and strict:raise ValueError('invalid_output')@@ -659,14 +733,58 @@ def validate_split(text, value, *, strict=False):else spoken(lines[int(m.group(2)) - 1], shared)), words)parts = []for index, event in enumerate(events):- if index == 0 and not re.sub(r'\[[LP]\d+\]', '', event).strip():- # Nothing sorted into the opening: it is the user's own description, without later lines.- mine = set(re.findall(r'\[[LP](\d+)\]', event))- words = re.sub(r'\[L(\d+)\]', lambda m: m.group(0) if m.group(1) in mine else '', body)- words = restore(' '.join(words.split()))- else:- words = ' '.join(([CONTINUE] if index or value.get('continued') else []) + [shared] +- ([restore(event)] if event else []))+ words = ' '.join(([CONTINUE] if index or value.get('continued') else []) + ([shared] if shared else []) ++ [restore(event) if re.sub(r'\[[LP]\d+\]', '', event).strip() else+ (restore(event) + ' ' + quiet(body)).strip()])+ if sound:+ words = 'integrated_multimodal_description: [Shot 1] ' + words + '\n\n' + sound+ parts.append(words)+ return parts+++def order_split(value):+ """Part texts when the prompt model cannot sort the description: its sentences in order, each in one part.++ Nothing is repeated, so no part plays the story again; a spoken line ends its sentence. A part between+ sentences carries on from the previous one without speech. Shot labels and their times describe the whole video and+ are left out.+ """+ body, sound, lines = part_source(value['text'])+ body = re.sub(r'\[Shot \d+\]\s*|\bAt \d{2}:\d{2}(?:\.\d+)?\s*[:,]?\s*', '', body)+ pieces = sentences(body)+ count = len(value['seconds'])+ if not pieces:+ raise ValueError('invalid_prompt')+ owner = []+ if len(pieces) <= count:+ # A short story is spread over the video instead of ending early.+ owner = [index * count // len(pieces) for index in range(len(pieces))]+ else:+ # Each part takes the sentences whose middle falls in its share of the video, at least one each.+ # Spoken lines take time: a sentence weighs as much as its words with the lines put back.+ sizes = [len(re.sub(r'\[L(\d+)\]', lambda m: lines[int(m.group(1)) - 1], piece)) for piece in pieces]+ total, ends = sum(sizes), []+ clock = 0.+ for length in value['seconds']:+ clock += length+ ends.append(clock / sum(value['seconds']))+ done = 0+ for index, size in enumerate(sizes):+ middle = (done + size / 2) / total+ done += size+ part = next(n for n, end in enumerate(ends) if middle <= end or n == count - 1)+ low = owner[-1] if owner else 0+ # Room for every later part to get one sentence, and no part skipped while sentences remain.+ part = max(low, count - (len(pieces) - index), min(part, low + 1 if owner else 0))+ owner.append(part)+ events = [[] for _ in range(count)]+ for piece, part in zip(pieces, owner):+ events[part].append(piece)+ parts = []+ for index, event in enumerate(events):+ # A part with no sentence of its own goes on quietly instead of inventing speech.+ words = re.sub(r'\[L(\d+)\]', lambda m: lines[int(m.group(1)) - 1], ' '.join(event)) or quiet(body)+ words = ' '.join(([CONTINUE] if index or value.get('continued') else []) + [words])if sound:words = 'integrated_multimodal_description: [Shot 1] ' + words + '\n\n' + soundparts.append(words)@@ -727,4 +845,5 @@ def validate_parts(parts, value):SPLIT_RETRY = ('Answer again with exactly one "Shared:" line and the "Part N:" lines, every [L] marker exactly once '- 'and in order, and no marker in Shared.')+ 'and in order, and no marker in Shared. Shared holds no action, and every part has something of its '+ 'own that no other part repeats.')diff --git a/freevideo_engine/prompt_vlm/worker.py b/freevideo_engine/prompt_vlm/worker.pyindex 3d8bfbe..90fe5d7 100644--- a/freevideo_engine/prompt_vlm/worker.py+++ b/freevideo_engine/prompt_vlm/worker.py@@ -2,6 +2,7 @@import jsonimport osfrom pathlib import Path+import reimport sysimport timeimport traceback@@ -331,8 +332,8 @@ def load_model(folder, torch, stats, checkpoint):return model, device-def generate(torch, model, inputs, max_new_tokens, started, on_token):- """Greedy decoding; on_token(timing) sees first-output and decode speed."""+def generate(torch, model, inputs, max_new_tokens, started, on_token, stop=None):+ """Greedy decoding; on_token(timing) sees first-output and decode speed, and stop(ids) may end it early."""from transformers import StoppingCriteria, StoppingCriteriaListprompt = inputs.input_ids.shape[-1]timing = {}@@ -351,7 +352,7 @@ def generate(torch, model, inputs, max_new_tokens, started, on_token):timing.update(output_tokens=tokens, decode_seconds=now - self.first,decode_tokens_per_second=(tokens - 1) / (now - self.first) if now > self.first else None)on_token(timing)- return False+ return bool(stop is not None and tokens % 16 == 0 and stop(input_ids[0, prompt:]))with torch.inference_mode():output = model.generate(**inputs, max_new_tokens=max_new_tokens, do_sample=False, use_cache=True,stopping_criteria=StoppingCriteriaList([Progress()]))@@ -500,8 +501,17 @@ def split(folder, value, stats):if time.monotonic() - shown[0] > .5:checkpoint('decode', 'splitting')shown[0] = time.monotonic()- ids, _ = generate(torch, model, inputs.to(device), limit, loaded, on_token)- answers = [processor.decode(ids, skip_special_tokens=True)] if len(ids) < limit else []+ def ended(ids):+ # A model that starts the answer over after the last part would repeat it until the limit.+ text = processor.decode(ids, skip_special_tokens=True)+ last = re.search(r'(?m)^Part %d:' % count, text)+ again = last and re.search(r'(?m)^(?:Part \d+:|Shared:)', text[last.end():])+ return text[:last.end() + again.start()].rstrip() if again else None+ def answer(ids):+ text = ended(ids)+ return [text] if text is not None else [processor.decode(ids, skip_special_tokens=True)] if len(ids) < limit else []+ ids, _ = generate(torch, model, inputs.to(device), limit, loaded, on_token, ended)+ answers = answer(ids)stats.update(split_seconds=time.monotonic() - loaded, output_tokens=len(ids))parts = Nonetry:@@ -512,10 +522,9 @@ def split(folder, value, stats):split_user_text(value) + '\n\n' + SPLIT_RETRY)}]}]retry_inputs = tokenize(processor, retry)if retry_inputs.input_ids.shape[-1] + limit <= stats.get('kv_positions', 10 ** 9):- ids, _ = generate(torch, model, retry_inputs.to(device), limit, time.monotonic(), lambda timing: None)+ ids, _ = generate(torch, model, retry_inputs.to(device), limit, time.monotonic(), lambda timing: None, ended)stats.update(split_retry=True, retry_output_tokens=len(ids), split_seconds=time.monotonic() - loaded)- if len(ids) < limit:- answers.insert(0, processor.decode(ids, skip_special_tokens=True))+ answers[:0] = answer(ids)for strict in (True, False):for answer in answers:try:diff --git a/web/generation_progress.js b/web/generation_progress.jsindex 896d5a8..51b7ff7 100644--- a/web/generation_progress.js+++ b/web/generation_progress.js@@ -98,7 +98,7 @@ const STALL_SECONDS_BY_PHASE = Object.freeze({latent_upscale: 180, sample_finalize: 180, decode: 180, save: 180,dependencies: 180, recovery: 180, other: 180,// A long video's part descriptions: the server waits at most 10 min for the model and- // writes for at most 15 min, then falls back to the whole description on its own.+ // writes for at most 15 min, then splits the description by sentence on its own.describe: 1500,});// Normal stage sentences keep the sampling step or a download's size and speed.@@ -780,8 +780,8 @@ export function createGenerationProgress(t, now = () => Date.now(), {compact = fautomatic: compatibilityText,notice: trimStatus([...trims.values()], t),// A long video whose part descriptions could not be written: the server's own words stay in the report.- split: splitFailed === 'model_missing' ? t('Prompt model not installed. Every part uses the same description.', '未安装提示词模型,各段用同一段描述')- : t('Every part uses the same description.', '各段用同一段描述')}[level] || (!ended ? progressText : '');+ split: splitFailed === 'model_missing' ? t('Prompt model not installed. The description is split across the parts by sentence.', '未安装提示词模型,描述按句子顺序分到各段')+ : t('The description is split across the parts by sentence.', '描述按句子顺序分到各段')}[level] || (!ended ? progressText : '');if (status.textContent !== explanation) status.textContent = explanation;status.hidden = !explanation; status.dataset.level = level;const announcements = [];