Files
teams-whisper/app/db.py
T
pinokioandClaude Opus 5.5 4ff3e6fe14 Add Pinokio launcher for Teams meeting report pipeline
Pipeline code copied into app/; llama.py now uses launcher-local
llama.cpp (app/bin) and Qwen GGUF (app/models) instead of LM Studio.
install.js fetches ffmpeg, 7zip, llama.cpp CUDA 13, sherpa-onnx
diarization models, bge-m3 and Qwen3.6-35B-A3B, then pre-builds the
uv script environments. start.js serves the GUI on fixed port 18765
(hardcoded in the browser extension). reset.js keeps app/out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 21:29:33 +01:00

135 lines
6.0 KiB
Python

"""SQLite index of meetings (out/meetings.db) + shared helpers."""
import json, re, sqlite3
from pathlib import Path
HERE = Path(__file__).parent
OUT = HERE / "out"
DB = OUT / "meetings.db"
SCHEMA = """
CREATE TABLE IF NOT EXISTS recordings(
name TEXT PRIMARY KEY, source TEXT, started_at TEXT, ended_at TEXT,
chunks INTEGER DEFAULT 0, bytes INTEGER DEFAULT 0, duration_s REAL, speakers INTEGER,
status TEXT, error TEXT, flow_run_id TEXT, updated_at TEXT,
transcribe_device TEXT, transcribe_s REAL);
CREATE VIRTUAL TABLE IF NOT EXISTS lines USING fts5(name UNINDEXED, t UNINDEXED, speaker, text, tokenize='unicode61 remove_diacritics 2');
CREATE TABLE IF NOT EXISTS shots(name TEXT, t REAL, t_end REAL, file TEXT, kind TEXT, title TEXT, app TEXT, caption TEXT, text TEXT, entities TEXT, PRIMARY KEY(name, t));
CREATE VIRTUAL TABLE IF NOT EXISTS shot_text USING fts5(name UNINDEXED, t UNINDEXED, title, text, tokenize='unicode61 remove_diacritics 2');
CREATE TABLE IF NOT EXISTS topics(name TEXT, idx INTEGER, title TEXT, start_s REAL, end_s REAL, json TEXT, PRIMARY KEY(name, idx));
CREATE TABLE IF NOT EXISTS call_events(name TEXT, t REAL, kind TEXT, who_key TEXT, who TEXT, text TEXT);
CREATE INDEX IF NOT EXISTS call_events_name ON call_events(name, kind, t);
CREATE TABLE IF NOT EXISTS speaker_map(name TEXT, label TEXT, person TEXT, source TEXT, overlap_s REAL, PRIMARY KEY(name, label));
CREATE TABLE IF NOT EXISTS memory(name TEXT, idx INTEGER, kind TEXT, t REAL, meeting TEXT, date TEXT, topic TEXT, anchor TEXT, text TEXT, vec BLOB);
CREATE INDEX IF NOT EXISTS memory_name ON memory(name);
"""
STATUSES = ["recording", "queued", "audio", "transcribing", "diarizing", "identifying", "shots", "docs", "report", "memory", "done", "error"]
EVENT_KINDS = {"meta", "active", "present", "share_start", "share_stop", "caption", "chat", "hand"}
def connect() -> sqlite3.Connection:
OUT.mkdir(exist_ok=True)
c = sqlite3.connect(DB, isolation_level=None, timeout=30, check_same_thread=False)
c.execute("PRAGMA journal_mode=WAL")
c.row_factory = sqlite3.Row
c.executescript(SCHEMA)
return c
def upsert_recording(name: str, **f):
c = connect()
cols = ["name", *f]
sets = ",".join(f"{k}=excluded.{k}" for k in f) or "name=name"
c.execute(f"INSERT INTO recordings({','.join(cols)}) VALUES({','.join('?' * len(cols))}) "
f"ON CONFLICT(name) DO UPDATE SET {sets}", [name, *f.values()])
def set_status(name: str, status: str, error: str | None = None):
c = connect()
c.execute("INSERT INTO recordings(name,status,error,updated_at) VALUES(?,?,?,datetime('now')) "
"ON CONFLICT(name) DO UPDATE SET status=excluded.status,error=excluded.error,updated_at=excluded.updated_at",
(name, status, error))
def replace_lines(name, rows):
c = connect()
c.execute("BEGIN")
c.execute("DELETE FROM lines WHERE name=?", (name,))
c.executemany("INSERT INTO lines(name,t,speaker,text) VALUES(?,?,?,?)", [(name, t, s, x) for t, s, x in rows])
c.execute("COMMIT")
def replace_shots(name, rows):
c = connect()
c.execute("BEGIN")
c.execute("DELETE FROM shots WHERE name=?", (name,))
c.execute("DELETE FROM shot_text WHERE name=?", (name,))
for r in rows:
c.execute("INSERT INTO shots(name,t,t_end,file,kind,title,app,caption,text,entities) VALUES(?,?,?,?,?,?,?,?,?,?)",
(name, r["t"], r.get("t_end"), r["file"], r.get("kind"), r.get("title"), r.get("app"),
r.get("summary"), r.get("text"), json.dumps(r.get("entities") or {}, ensure_ascii=False)))
c.execute("INSERT INTO shot_text(name,t,title,text) VALUES(?,?,?,?)",
(name, r["t"], r.get("title") or "", f"{r.get('text') or ''}\n{r.get('summary') or ''}"))
c.execute("COMMIT")
def replace_topics(name, topics):
c = connect()
c.execute("BEGIN")
c.execute("DELETE FROM topics WHERE name=?", (name,))
c.executemany("INSERT INTO topics(name,idx,title,start_s,end_s,json) VALUES(?,?,?,?,?,?)",
[(name, i, t["title"], t.get("start_s"), t.get("end_s"), json.dumps(t, ensure_ascii=False)) for i, t in enumerate(topics)])
c.execute("COMMIT")
def replace_memory(name, rows):
c = connect()
c.execute("BEGIN")
c.execute("DELETE FROM memory WHERE name=?", (name,))
c.executemany("INSERT INTO memory(name,idx,kind,t,meeting,date,topic,anchor,text,vec) VALUES(?,?,?,?,?,?,?,?,?,?)", rows)
c.execute("COMMIT")
def out_dir(name) -> Path:
d = OUT / name
d.mkdir(parents=True, exist_ok=True)
return d
def media(name):
def first(*exts):
for e in exts:
p = OUT / f"{name}{e}"
if p.exists(): return p
return first(".webm", ".wav"), first(".webm", ".mp4")
def add_events(name, rows):
connect().executemany("INSERT INTO call_events(name,t,kind,who_key,who,text) VALUES(?,?,?,?,?,?)",
[(name, *r) for r in rows])
def events(name, *kinds):
q = "SELECT * FROM call_events WHERE name=?"
if kinds: q += f" AND kind IN ({','.join('?' * len(kinds))})"
return connect().execute(q + " ORDER BY t", (name, *kinds)).fetchall()
def speaker_names(name) -> dict:
return {r["label"]: r["person"] for r in connect().execute(
"SELECT label,person FROM speaker_map WHERE name=? AND person<>'' AND person IS NOT NULL", (name,))}
def set_speaker_map(name, label, person, source, overlap_s=None):
connect().execute(
"INSERT INTO speaker_map(name,label,person,source,overlap_s) VALUES(?,?,?,?,?) "
"ON CONFLICT(name,label) DO UPDATE SET person=excluded.person,source=excluded.source,overlap_s=excluded.overlap_s "
"WHERE speaker_map.source != 'manual' OR excluded.source = 'manual'", (name, label, person, source, overlap_s))
def norm_name(raw: str) -> str:
"""Port of norm() in ext/teams.js: 'Rossi, MARIO (IG/S-32)' -> 'Mario Rossi'."""
raw = (raw or "").strip()
m = re.match(r"^([^,]+),\s*([^(]+?)\s*(\(.*)?$", raw)
if not m: return raw
return " ".join(w.title() if w.isupper() else w for w in f"{m[2]} {m[1]}".split())