Initial commit: Nat20 Notes — TTRPG session transcription & summarization
This commit is contained in:
48
backend/app/routers/notes.py
Normal file
48
backend/app/routers/notes.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from .. import database as db, jobs
|
||||
from ..pipeline.turns import load_turns
|
||||
from ..pipeline.summarize import summarize_session
|
||||
|
||||
router = APIRouter(prefix="/api/sessions/{session_id}/notes", tags=["notes"])
|
||||
|
||||
|
||||
@router.post("/generate")
|
||||
def generate_notes(session_id: str):
|
||||
session = db.get_conn().execute("SELECT * FROM sessions WHERE id = ?", (session_id,)).fetchone()
|
||||
if not session:
|
||||
raise HTTPException(404, "Session not found")
|
||||
if not session["transcript_path"]:
|
||||
raise HTTPException(400, "Session hasn't been transcribed yet")
|
||||
|
||||
settings = db.get_settings()
|
||||
speaker_map = {r["raw_label"]: r["display_name"] for r in
|
||||
db.get_conn().execute("SELECT raw_label, display_name FROM speakers WHERE session_id = ?", (session_id,))
|
||||
if r["display_name"]}
|
||||
|
||||
job_id = db.create_job(session_id, "summarize")
|
||||
|
||||
def run(progress_cb):
|
||||
turns = load_turns(Path(session["transcript_path"]), speaker_map)
|
||||
dm_notes, player_recap = summarize_session(turns, settings, progress_cb=progress_cb)
|
||||
with db.tx() as conn:
|
||||
conn.execute(
|
||||
"INSERT INTO notes (session_id, dm_notes, player_recap, generated_at) VALUES (?, ?, ?, ?) "
|
||||
"ON CONFLICT(session_id) DO UPDATE SET dm_notes=excluded.dm_notes, "
|
||||
"player_recap=excluded.player_recap, generated_at=excluded.generated_at",
|
||||
(session_id, dm_notes, player_recap, db.now()),
|
||||
)
|
||||
conn.execute("UPDATE sessions SET status = 'complete' WHERE id = ?", (session_id,))
|
||||
|
||||
jobs.submit(job_id, run)
|
||||
return {"job_id": job_id}
|
||||
|
||||
|
||||
@router.get("")
|
||||
def get_notes(session_id: str):
|
||||
row = db.get_conn().execute("SELECT * FROM notes WHERE session_id = ?", (session_id,)).fetchone()
|
||||
if not row:
|
||||
raise HTTPException(404, "Notes not generated yet")
|
||||
return dict(row)
|
||||
Reference in New Issue
Block a user