"""CPU speech recognition and stateful shot direction.""" import base64 import http.client import io import json import logging import threading import wave from camera_direction import camera_direction import numpy as np import onnx_asr import onnxruntime as ort from huggingface_hub import hf_hub_download, snapshot_download from llama_cpp import Llama, LlamaGrammar ASR_REPO = "FluidVoice-Labs/fluid-speech-pro-onnx" ASR_REV = "fa588e9f87f1b9ad0ca2e2d8f7eaf84dc3803581" QWEN = "unsloth/Qwen3-4B-Instruct-2507-GGUF" QWEN_REV = "a06e946bb6b655725eafa393f4a9745d460374c9" _options = ort.SessionOptions() _options.intra_op_num_threads = 2 _options.inter_op_num_threads = 1 _dir = snapshot_download(ASR_REPO, revision=ASR_REV, allow_patterns=["*int8*", "vocab.txt", "config.json"]) asr = onnx_asr.load_model("nemo-conformer-tdt", _dir, quantization="int8", sess_options=_options) llm = Llama(model_path=hf_hub_download(QWEN, "Qwen3-4B-Instruct-2507-Q4_K_M.gguf", revision=QWEN_REV), n_ctx=2048, n_threads=2, n_threads_batch=2, verbose=False) model_lock = threading.Lock() asr_lock = threading.Lock() OPS = ("append", "redo_last", "clarify") # A 45-second spoken cue plus the microphone's short lead-in and word-ending buffer. MAX_AUDIO_FRAMES = 46 * 16_000 MAX_ENCODED_BYTES = 4 * ((44 + 2 * MAX_AUDIO_FRAMES + 2) // 3) INTENT = """Decide what the speaker wants to do on the timeline. append adds a following clip. redo_last makes another version of the last completed clip. clarify means either timing is plausible. Respect explicit timing, negation and self-corrections. Distinguish a character repeating a movement from the director asking to reshoot. "Garde cette prise, ensuite il refait le mouvement" is append. "Pas la suite, le même passage en plan large" is redo_last. "Un peu moins de neige" with no timing is clarify. When a fixed operation is provided, use it. In all cases, write the resulting full visual shot in English, preserving established details and applying the visual change. Omit timeline commands from the visual prompt.""" SYSTEM = INTENT + """ Reply in four fields separated by vertical bars: operation|target|2-7 word English description of the change|updated complete English shot (at most 45 words). Camera speed and subject speed are distinct. An elliptical change refers to the last directed element.""" ANGLE_GUIDE = " French contre-plongée means looking up from below; plongée means looking down from above. Preserve both camera travel and angle in the shot." def angle_guide(cue): return ANGLE_GUIDE if "plong" in cue.casefold() else "" GRAMMAR = ('root ::= operation "|" target "|" text "|" text\n' 'operation ::= "append" | "redo_last" | "clarify"\n' 'target ::= "scene" | "subject" | "camera" | "weather" | "subject+weather" | "camera+weather" | "subject+camera"\n' 'text ::= [^\\n|]+') def transcribe(encoded): if not isinstance(encoded, str) or not 40 < len(encoded) <= MAX_ENCODED_BYTES: raise ValueError("audio") with wave.open(io.BytesIO(base64.b64decode(encoded, validate=True)), "rb") as wav: if (wav.getframerate(), wav.getnchannels(), wav.getsampwidth()) != (16000, 1, 2): raise ValueError("audio") if not 2400 <= wav.getnframes() <= MAX_AUDIO_FRAMES: raise ValueError("duration") samples = np.frombuffer(wav.readframes(wav.getnframes()), dtype="