Fetches HPR's own metadata, audio, and official transcripts live; falls back to local speech recognition (tiny model, matching HPR's own hpr-get-and-transcribe.bash tooling) only when needed, and to correlation against vendored reference clips or sibling episodes otherwise. License-aware, resumable, and never guesses -- flags anything it isn't confident about for manual review rather than producing a plausible-looking wrong cut.
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
"""
|
|
Persistent, resumable progress tracking -- see docs/DESIGN.md, "Testing &
|
|
resumability strategy". A half-hour batch run WILL get interrupted
|
|
(Ctrl+C, laptop sleep, SSH drop); this module is what makes that cost at
|
|
most the one in-flight episode, never the whole run.
|
|
|
|
Backed by SQLite (one row per episode, written with a plain commit after
|
|
each episode finishes -- SQLite's own durability guarantees give us the
|
|
atomicity here, no separate temp-file dance needed). Re-running a batch
|
|
skips anything already logged under the current marker-library/engine
|
|
version; changing either re-processes only the episodes whose cached
|
|
result came from an older version.
|
|
"""
|
|
import json
|
|
import os
|
|
import sqlite3
|
|
import time
|
|
|
|
from . import config
|
|
|
|
_SCHEMA = """
|
|
CREATE TABLE IF NOT EXISTS results (
|
|
episode INTEGER PRIMARY KEY,
|
|
status TEXT NOT NULL, -- 'done' or 'error'
|
|
result_json TEXT NOT NULL,
|
|
markers_version INTEGER NOT NULL,
|
|
asr_engine TEXT,
|
|
asr_model TEXT,
|
|
updated_at REAL NOT NULL
|
|
);
|
|
"""
|
|
|
|
|
|
def connect(cache_dir=None):
|
|
cache_dir = cache_dir or config.DEFAULT_CACHE_DIR
|
|
os.makedirs(cache_dir, exist_ok=True)
|
|
conn = sqlite3.connect(os.path.join(cache_dir, "ledger.db"))
|
|
conn.execute(_SCHEMA)
|
|
conn.commit()
|
|
return conn
|
|
|
|
|
|
def is_done(conn, ep_id, settings):
|
|
row = conn.execute(
|
|
"SELECT markers_version, asr_engine, asr_model FROM results WHERE episode = ? AND status = 'done'",
|
|
(ep_id,),
|
|
).fetchone()
|
|
if row is None:
|
|
return False
|
|
markers_version, asr_engine, asr_model = row
|
|
return (
|
|
markers_version == config.MARKERS_VERSION
|
|
and asr_engine == settings.asr_engine
|
|
and asr_model == settings.asr_model
|
|
)
|
|
|
|
|
|
def record(conn, ep_id, result_dict, settings, status="done"):
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO results (episode, status, result_json, markers_version, asr_engine, asr_model, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(episode) DO UPDATE SET
|
|
status=excluded.status, result_json=excluded.result_json,
|
|
markers_version=excluded.markers_version, asr_engine=excluded.asr_engine,
|
|
asr_model=excluded.asr_model, updated_at=excluded.updated_at
|
|
""",
|
|
(ep_id, status, json.dumps(result_dict), config.MARKERS_VERSION,
|
|
settings.asr_engine, settings.asr_model, time.time()),
|
|
)
|
|
conn.commit() # one durable write per episode -- the atomic unit of work
|
|
|
|
|
|
def get(conn, ep_id):
|
|
row = conn.execute("SELECT result_json FROM results WHERE episode = ?", (ep_id,)).fetchone()
|
|
return json.loads(row[0]) if row else None
|
|
|
|
|
|
def all_results(conn):
|
|
for (result_json,) in conn.execute("SELECT result_json FROM results ORDER BY episode"):
|
|
yield json.loads(result_json)
|