admin.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. #!/usr/bin/env python3
  2. """
  3. admin.py — Speaker Admin Web Server
  4. Local web interface for managing speaker names, voice recordings,
  5. and test recording playback.
  6. Runs on port 8001 alongside bridge.py.
  7. Access at: http://localhost:8001
  8. """
  9. import asyncio
  10. import json
  11. import shutil
  12. from pathlib import Path
  13. from fastapi import FastAPI, HTTPException, UploadFile, File
  14. from fastapi.responses import HTMLResponse, FileResponse
  15. from pydantic import BaseModel
  16. import uvicorn
  17. import websockets
  18. SPEAKERS_FILE = Path(__file__).parent / "speakers.json"
  19. RECORDINGS_DIR = Path(__file__).parent / "recordings"
  20. TEST_RECORDINGS_DIR = Path(__file__).parent / "test_recordings"
  21. RECORDINGS_DIR.mkdir(exist_ok=True)
  22. TEST_RECORDINGS_DIR.mkdir(exist_ok=True)
  23. ALLOWED_AUDIO_EXTS = {".wav", ".mp3", ".m4a", ".ogg", ".flac", ".webm", ".aiff"}
  24. WS_URL = "ws://localhost:8000/asr"
  25. app = FastAPI(title="Speaker Admin")
  26. # ── Speaker data helpers ──────────────────────────────────────────────────────
  27. def _load() -> dict[str, str]:
  28. if SPEAKERS_FILE.exists():
  29. try:
  30. return json.loads(SPEAKERS_FILE.read_text(encoding="utf-8"))
  31. except Exception:
  32. pass
  33. return {}
  34. def _save(data: dict[str, str]) -> None:
  35. SPEAKERS_FILE.write_text(
  36. json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8"
  37. )
  38. def _recording_path(sid: str) -> Path | None:
  39. for ext in (".wav", ".mp3", ".m4a", ".ogg", ".webm", ".flac"):
  40. p = RECORDINGS_DIR / f"{sid}{ext}"
  41. if p.exists():
  42. return p
  43. return None
  44. # ── Speaker API ───────────────────────────────────────────────────────────────
  45. class NameBody(BaseModel):
  46. name: str
  47. class AddBody(BaseModel):
  48. id: str
  49. name: str
  50. @app.get("/api/speakers")
  51. def api_list():
  52. speakers = _load()
  53. return {"speakers": [
  54. {"id": k, "name": v, "has_recording": _recording_path(k) is not None}
  55. for k, v in sorted(speakers.items())
  56. ]}
  57. @app.post("/api/speakers")
  58. def api_add(body: AddBody):
  59. speakers = _load()
  60. sid = body.id.strip()
  61. if not sid:
  62. raise HTTPException(400, "Speaker ID cannot be empty")
  63. if sid in speakers:
  64. raise HTTPException(400, f"'{sid}' already exists")
  65. speakers[sid] = body.name.strip()
  66. _save(speakers)
  67. return {"ok": True, "id": sid, "name": speakers[sid]}
  68. @app.put("/api/speakers/{sid}")
  69. def api_update(sid: str, body: NameBody):
  70. name = body.name.strip()
  71. if not name:
  72. raise HTTPException(400, "Name cannot be empty")
  73. speakers = _load()
  74. speakers[sid] = name
  75. _save(speakers)
  76. return {"ok": True}
  77. @app.delete("/api/speakers/{sid}")
  78. def api_delete(sid: str):
  79. speakers = _load()
  80. speakers.pop(sid, None)
  81. _save(speakers)
  82. rec = _recording_path(sid)
  83. if rec:
  84. rec.unlink()
  85. return {"ok": True}
  86. @app.post("/api/speakers/{sid}/recording")
  87. async def api_upload(sid: str, file: UploadFile = File(...)):
  88. suffix = Path(file.filename or "audio.wav").suffix.lower() or ".wav"
  89. rec = _recording_path(sid)
  90. if rec:
  91. rec.unlink()
  92. out = RECORDINGS_DIR / f"{sid}{suffix}"
  93. with out.open("wb") as f:
  94. shutil.copyfileobj(file.file, f)
  95. speakers = _load()
  96. if sid not in speakers:
  97. speakers[sid] = sid
  98. _save(speakers)
  99. size_kb = round(out.stat().st_size / 1024)
  100. return {"ok": True, "file": out.name, "kb": size_kb}
  101. @app.get("/api/speakers/{sid}/recording")
  102. def api_playback(sid: str):
  103. rec = _recording_path(sid)
  104. if not rec:
  105. raise HTTPException(404, "No recording found")
  106. return FileResponse(rec)
  107. # ── Test playback state ───────────────────────────────────────────────────────
  108. _playback_task: asyncio.Task | None = None
  109. _playback_status: dict = {
  110. "state": "idle", # idle | loading | playing | done | error
  111. "file": None,
  112. "progress": 0, # 0–100
  113. "elapsed": 0.0, # seconds streamed so far
  114. "duration": 0.0, # total file duration in seconds
  115. "error": None,
  116. }
  117. BRIDGE_INJECT_URL = "http://127.0.0.1:8002/inject"
  118. async def _stream_file(filepath: Path, speed: float) -> None:
  119. global _playback_status
  120. try:
  121. import miniaudio
  122. import httpx
  123. except ImportError as e:
  124. _playback_status.update({"state": "error", "error": f"Missing package: {e}"})
  125. return
  126. try:
  127. _playback_status["state"] = "loading"
  128. info = miniaudio.get_file_info(str(filepath))
  129. duration = info.duration
  130. _playback_status.update({
  131. "state": "playing", "duration": round(duration, 1),
  132. "elapsed": 0.0, "progress": 0,
  133. })
  134. chunk_frames = 4096
  135. chunk_secs = chunk_frames / 16000
  136. elapsed = 0.0
  137. stream = miniaudio.stream_file(
  138. str(filepath),
  139. output_format=miniaudio.SampleFormat.SIGNED16,
  140. nchannels=1,
  141. sample_rate=16000,
  142. frames_to_read=chunk_frames,
  143. )
  144. async with httpx.AsyncClient() as client:
  145. for chunk in stream:
  146. await client.post(BRIDGE_INJECT_URL, content=bytes(chunk))
  147. elapsed += chunk_secs
  148. _playback_status["elapsed"] = round(elapsed, 1)
  149. _playback_status["progress"] = (
  150. min(99, round(elapsed / duration * 100)) if duration else 0
  151. )
  152. await asyncio.sleep(chunk_secs / speed)
  153. _playback_status.update({
  154. "state": "done", "progress": 100, "elapsed": round(duration, 1),
  155. })
  156. except asyncio.CancelledError:
  157. _playback_status.update({
  158. "state": "idle", "file": None, "progress": 0, "elapsed": 0.0,
  159. })
  160. except Exception as exc:
  161. _playback_status.update({"state": "error", "error": str(exc), "progress": 0})
  162. print(f"[Playback] {exc}")
  163. # ── Test recording API ────────────────────────────────────────────────────────
  164. @app.post("/api/test/upload")
  165. async def api_test_upload(file: UploadFile = File(...)):
  166. suffix = Path(file.filename or "recording.wav").suffix.lower()
  167. if suffix not in ALLOWED_AUDIO_EXTS:
  168. raise HTTPException(400, f"Unsupported format '{suffix}'")
  169. # Sanitise filename — replace spaces with underscores
  170. stem = Path(file.filename).stem[:80].replace(" ", "_")
  171. out = TEST_RECORDINGS_DIR / f"{stem}{suffix}"
  172. try:
  173. with out.open("wb") as f:
  174. shutil.copyfileobj(file.file, f)
  175. except OSError as e:
  176. raise HTTPException(500, f"Could not save file: {e}")
  177. return {"ok": True, "filename": out.name, "mb": round(out.stat().st_size / 1024 / 1024, 1)}
  178. @app.get("/api/test/files")
  179. def api_test_list():
  180. files = []
  181. for p in sorted(TEST_RECORDINGS_DIR.iterdir()):
  182. if p.suffix.lower() in ALLOWED_AUDIO_EXTS:
  183. files.append({
  184. "filename": p.name,
  185. "mb": round(p.stat().st_size / 1024 / 1024, 1),
  186. })
  187. return {"files": files}
  188. @app.delete("/api/test/files/{filename:path}")
  189. def api_test_delete(filename: str):
  190. p = TEST_RECORDINGS_DIR / Path(filename).name
  191. try:
  192. if p.exists():
  193. p.unlink()
  194. except OSError as e:
  195. raise HTTPException(500, f"Could not delete: {e}")
  196. return {"ok": True}
  197. class PlaybackBody(BaseModel):
  198. filename: str
  199. speed: float = 1.0
  200. @app.post("/api/test/start")
  201. async def api_test_start(body: PlaybackBody):
  202. global _playback_task
  203. if _playback_task and not _playback_task.done():
  204. raise HTTPException(409, "Playback already running — stop it first")
  205. p = TEST_RECORDINGS_DIR / Path(body.filename).name
  206. if not p.exists():
  207. raise HTTPException(404, "File not found")
  208. speed = max(0.25, min(8.0, body.speed))
  209. _playback_status.update({"state": "starting", "file": p.name, "progress": 0, "error": None})
  210. _playback_task = asyncio.create_task(_stream_file(p, speed))
  211. return {"ok": True}
  212. @app.post("/api/test/stop")
  213. async def api_test_stop():
  214. global _playback_task
  215. if _playback_task and not _playback_task.done():
  216. _playback_task.cancel()
  217. try:
  218. await _playback_task
  219. except asyncio.CancelledError:
  220. pass
  221. _playback_status.update({"state": "idle", "file": None, "progress": 0, "elapsed": 0.0})
  222. return {"ok": True}
  223. @app.get("/api/test/status")
  224. def api_test_status():
  225. return _playback_status
  226. # ── Web UI ────────────────────────────────────────────────────────────────────
  227. HTML = """<!DOCTYPE html>
  228. <html lang="en">
  229. <head>
  230. <meta charset="UTF-8">
  231. <meta name="viewport" content="width=device-width, initial-scale=1">
  232. <title>Meeting Transcription for the Deaf</title>
  233. <style>
  234. *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  235. body { font-family: system-ui, sans-serif; background: #f0f4f8; color: #1e293b; }
  236. header {
  237. background: #1e3a5f; color: white; padding: 16px 24px;
  238. display: flex; align-items: center; gap: 16px;
  239. }
  240. header h1 { font-size: 1.2rem; font-weight: 600; flex: 1; }
  241. header small { opacity: .7; font-size: .8rem; }
  242. .toolbar {
  243. background: white; padding: 12px 24px;
  244. display: flex; gap: 12px; align-items: center;
  245. border-bottom: 1px solid #e2e8f0;
  246. }
  247. .toolbar input[type=search] {
  248. flex: 1; max-width: 340px; padding: 8px 12px;
  249. border: 1px solid #cbd5e1; border-radius: 6px; font-size: .95rem;
  250. }
  251. .count { color: #64748b; font-size: .9rem; margin-left: auto; }
  252. .btn {
  253. display: inline-flex; align-items: center; gap: 6px;
  254. padding: 8px 16px; border-radius: 6px; border: none;
  255. cursor: pointer; font-size: .9rem; font-weight: 500; transition: filter .15s;
  256. }
  257. .btn:hover:not(:disabled) { filter: brightness(.92); }
  258. .btn:disabled { opacity: .45; cursor: not-allowed; }
  259. .btn-primary { background: #2563eb; color: white; }
  260. .btn-danger { background: #dc2626; color: white; }
  261. .btn-ghost { background: #e2e8f0; color: #334155; }
  262. .btn-sm { padding: 4px 10px; font-size: .82rem; }
  263. table { width: 100%; border-collapse: collapse; background: white; }
  264. th {
  265. text-align: left; padding: 10px 16px; font-size: .8rem;
  266. font-weight: 600; text-transform: uppercase; letter-spacing: .05em;
  267. color: #64748b; background: #f8fafc; border-bottom: 1px solid #e2e8f0;
  268. }
  269. td { padding: 10px 16px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
  270. tr:hover td { background: #f8fafc; }
  271. tr.hidden { display: none; }
  272. .sid { font-family: monospace; font-size: .85rem; color: #475569; }
  273. .name-cell { display: flex; align-items: center; gap: 8px; }
  274. .name-display { cursor: pointer; flex: 1; }
  275. .name-display:hover { text-decoration: underline; }
  276. .name-input {
  277. flex: 1; padding: 4px 8px; border: 1px solid #2563eb;
  278. border-radius: 4px; font-size: .95rem; outline: none;
  279. }
  280. .rec-badge {
  281. display: inline-flex; align-items: center; gap: 4px;
  282. font-size: .75rem; padding: 2px 8px; border-radius: 999px;
  283. font-weight: 500;
  284. }
  285. .rec-yes { background: #dcfce7; color: #166534; }
  286. .rec-no { background: #f1f5f9; color: #94a3b8; }
  287. .actions { display: flex; gap: 6px; }
  288. /* Modal */
  289. .modal-bg {
  290. display: none; position: fixed; inset: 0;
  291. background: rgba(0,0,0,.45); z-index: 100;
  292. align-items: center; justify-content: center;
  293. }
  294. .modal-bg.open { display: flex; }
  295. .modal {
  296. background: white; border-radius: 10px; padding: 28px;
  297. width: 420px; max-width: 95vw; box-shadow: 0 20px 60px rgba(0,0,0,.25);
  298. }
  299. .modal h2 { font-size: 1.1rem; margin-bottom: 16px; }
  300. .field { margin-bottom: 14px; }
  301. .field label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: 4px; }
  302. .field input {
  303. width: 100%; padding: 8px 10px; border: 1px solid #cbd5e1;
  304. border-radius: 6px; font-size: .95rem;
  305. }
  306. .field input:focus { outline: 2px solid #2563eb; border-color: transparent; }
  307. .modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
  308. /* Upload drop zone */
  309. .upload-area {
  310. border: 2px dashed #cbd5e1; border-radius: 8px; padding: 16px;
  311. text-align: center; cursor: pointer; transition: border-color .2s;
  312. color: #64748b; font-size: .9rem;
  313. }
  314. .upload-area.drag { border-color: #2563eb; background: #eff6ff; }
  315. .upload-area input[type=file] { display: none; }
  316. /* Audio player */
  317. .audio-player { width: 180px; height: 32px; }
  318. /* Toast */
  319. #toast {
  320. position: fixed; bottom: 24px; right: 24px;
  321. background: #1e293b; color: white; padding: 10px 18px;
  322. border-radius: 8px; font-size: .9rem; transform: translateY(80px);
  323. transition: transform .25s; z-index: 200; pointer-events: none;
  324. }
  325. #toast.show { transform: translateY(0); }
  326. #toast.error { background: #dc2626; }
  327. .container { max-width: 1100px; margin: 0 auto; padding: 24px; }
  328. audio { vertical-align: middle; }
  329. /* ── Test Playback card ─────────────────────────────── */
  330. .pb-card {
  331. background: white; border-radius: 8px; margin-top: 20px;
  332. border: 1px solid #e2e8f0; overflow: hidden;
  333. }
  334. .pb-card-header {
  335. display: flex; justify-content: space-between; align-items: center;
  336. padding: 14px 20px; cursor: pointer; font-weight: 600; font-size: 1rem;
  337. background: #f8fafc; border-bottom: 1px solid #e2e8f0; user-select: none;
  338. }
  339. .pb-card-header:hover { background: #f1f5f9; }
  340. #pb-body { padding: 20px; }
  341. .pb-hint { color: #64748b; font-size: .88rem; margin-bottom: 16px; line-height: 1.5; }
  342. .pb-layout { display: flex; gap: 20px; flex-wrap: wrap; margin-bottom: 16px; }
  343. .pb-upload { flex: 1; min-width: 260px; }
  344. .pb-controls { min-width: 210px; display: flex; flex-direction: column; gap: 12px; }
  345. .pb-select {
  346. width: 100%; padding: 7px 10px; border: 1px solid #cbd5e1;
  347. border-radius: 6px; font-size: .9rem; background: white;
  348. }
  349. .pb-label { font-size: .85rem; font-weight: 500; display: block; margin-bottom: 4px; }
  350. .pb-status-bar { margin-top: 12px; }
  351. .pb-status-row {
  352. display: flex; justify-content: space-between;
  353. font-size: .85rem; margin-bottom: 5px; min-height: 18px;
  354. }
  355. .pb-progress-track {
  356. height: 8px; background: #e2e8f0; border-radius: 999px; overflow: hidden;
  357. }
  358. .pb-progress-fill {
  359. height: 100%; background: #2563eb; border-radius: 999px;
  360. width: 0%; transition: width .8s linear;
  361. }
  362. .pb-note { font-size: .78rem; color: #94a3b8; margin-top: 10px; }
  363. .pb-error { color: #dc2626; }
  364. </style>
  365. </head>
  366. <body>
  367. <header>
  368. <div>
  369. <h1>&#127908; Speaker Admin</h1>
  370. <small>Meeting Transcription for the Deaf — Speaker Name &amp; Voice Library</small>
  371. </div>
  372. </header>
  373. <div class="toolbar">
  374. <input type="search" id="search" placeholder="Search by ID or name…" oninput="filterTable()">
  375. <button class="btn btn-primary" onclick="openAddModal()">&#43; Add Speaker</button>
  376. <span class="count" id="count"></span>
  377. </div>
  378. <div class="container">
  379. <!-- Speaker table -->
  380. <table id="table">
  381. <thead>
  382. <tr>
  383. <th>Speaker ID</th>
  384. <th>Friendly Name</th>
  385. <th>Voice Sample</th>
  386. <th>Actions</th>
  387. </tr>
  388. </thead>
  389. <tbody id="tbody"></tbody>
  390. </table>
  391. <!-- Test Playback card -->
  392. <div class="pb-card">
  393. <div class="pb-card-header" onclick="togglePbCard()">
  394. <span>&#127911; Test Recording Playback</span>
  395. <span id="pb-chevron">&#9660;</span>
  396. </div>
  397. <div id="pb-body">
  398. <p class="pb-hint">
  399. Upload a full church service recording (WAV, MP3, FLAC, OGG, M4A) to test the
  400. transcription pipeline offline. The file streams to WhisperLiveKit exactly as a
  401. live microphone would — results appear on the e-ink display in real time.
  402. </p>
  403. <div class="pb-layout">
  404. <!-- Upload drop zone -->
  405. <div class="pb-upload">
  406. <div class="upload-area" id="test-drop"
  407. ondragover="pbDragOver(event)" ondragleave="pbDragLeave()"
  408. ondrop="pbDrop(event)"
  409. onclick="document.getElementById('test-file-input').click()">
  410. <input type="file" id="test-file-input" accept="audio/*"
  411. onchange="pbUpload(this.files[0])">
  412. <div>&#8679; Drop a recording here, or click to browse</div>
  413. <div style="font-size:.78rem;margin-top:4px;color:#94a3b8">
  414. WAV &middot; MP3 &middot; FLAC &middot; OGG &middot; M4A
  415. </div>
  416. </div>
  417. <div id="test-upload-status" style="min-height:20px;font-size:.85rem;margin-top:8px"></div>
  418. </div>
  419. <!-- Playback controls -->
  420. <div class="pb-controls">
  421. <div>
  422. <span class="pb-label">Playback Speed</span>
  423. <select id="pb-speed" class="pb-select">
  424. <option value="0.5">0.5&times; (half speed)</option>
  425. <option value="1">1&times; real-time</option>
  426. <option value="2">2&times; faster</option>
  427. <option value="4" selected>4&times; quick review</option>
  428. <option value="8">8&times; very fast</option>
  429. </select>
  430. </div>
  431. <button id="pb-stop-btn" class="btn btn-danger" onclick="pbStop()" disabled>
  432. &#9632; Stop Playback
  433. </button>
  434. </div>
  435. </div>
  436. <!-- Uploaded test files -->
  437. <table id="test-table">
  438. <thead>
  439. <tr>
  440. <th>Recording File</th>
  441. <th style="width:80px">Size</th>
  442. <th style="width:140px">Actions</th>
  443. </tr>
  444. </thead>
  445. <tbody id="test-tbody">
  446. <tr id="test-empty-row">
  447. <td colspan="3" style="color:#94a3b8;padding:16px 16px">
  448. No recordings uploaded yet
  449. </td>
  450. </tr>
  451. </tbody>
  452. </table>
  453. <!-- Status bar -->
  454. <div class="pb-status-bar">
  455. <div class="pb-status-row">
  456. <span id="pb-status-text" style="color:#64748b">Idle</span>
  457. <span id="pb-time-text" style="color:#64748b"></span>
  458. </div>
  459. <div class="pb-progress-track">
  460. <div class="pb-progress-fill" id="pb-fill"></div>
  461. </div>
  462. </div>
  463. <p class="pb-note">
  464. While a test recording plays, the live microphone in bridge.py continues to run.
  465. Keep the room quiet during testing to avoid mixing live audio with the recording.
  466. </p>
  467. </div>
  468. </div>
  469. </div><!-- /container -->
  470. <!-- Add speaker modal -->
  471. <div class="modal-bg" id="add-modal" onclick="closeAddModal(event)">
  472. <div class="modal">
  473. <h2>Add Speaker</h2>
  474. <div class="field">
  475. <label>Speaker ID
  476. <span style="color:#64748b;font-weight:normal;font-size:.8rem">
  477. (e.g. SPEAKER_00, or any unique key)
  478. </span>
  479. </label>
  480. <input id="new-id" placeholder="SPEAKER_00">
  481. </div>
  482. <div class="field">
  483. <label>Friendly Name</label>
  484. <input id="new-name" placeholder="Pastor John">
  485. </div>
  486. <div class="modal-actions">
  487. <button class="btn btn-ghost" onclick="closeAddModal()">Cancel</button>
  488. <button class="btn btn-primary" onclick="addSpeaker()">Add</button>
  489. </div>
  490. </div>
  491. </div>
  492. <!-- Voice sample upload modal -->
  493. <div class="modal-bg" id="upload-modal" onclick="closeUploadModal(event)">
  494. <div class="modal">
  495. <h2 id="upload-title">Upload Voice Sample</h2>
  496. <p style="color:#64748b;font-size:.85rem;margin-bottom:12px">
  497. Upload a 10–60 second clear speech recording.<br>
  498. Supported: WAV, MP3, M4A, OGG, FLAC, WebM
  499. </p>
  500. <div class="upload-area" id="drop-zone"
  501. ondragover="onDragOver(event)" ondragleave="onDragLeave(event)"
  502. ondrop="onDrop(event)" onclick="document.getElementById('file-input').click()">
  503. <input type="file" id="file-input" accept="audio/*" onchange="uploadFile(this.files[0])">
  504. <div>&#127926; Drag &amp; drop audio here, or click to browse</div>
  505. </div>
  506. <div id="upload-status" style="margin-top:10px;font-size:.85rem;color:#166534"></div>
  507. <div class="modal-actions">
  508. <button class="btn btn-ghost" onclick="closeUploadModal()">Close</button>
  509. </div>
  510. </div>
  511. </div>
  512. <div id="toast"></div>
  513. <script>
  514. let speakers = [];
  515. let uploadTarget = null;
  516. // ── Speaker table ─────────────────────────────────────────────────────────────
  517. async function load() {
  518. const res = await fetch('/api/speakers');
  519. const data = await res.json();
  520. speakers = data.speakers;
  521. render();
  522. }
  523. function render() {
  524. const tbody = document.getElementById('tbody');
  525. tbody.innerHTML = '';
  526. speakers.forEach(s => tbody.appendChild(makeRow(s)));
  527. document.getElementById('count').textContent =
  528. `${speakers.length} speaker${speakers.length !== 1 ? 's' : ''}`;
  529. filterTable();
  530. }
  531. function makeRow(s) {
  532. const tr = document.createElement('tr');
  533. tr.dataset.id = s.id;
  534. tr.dataset.name = s.name.toLowerCase();
  535. const recHtml = s.has_recording
  536. ? `<span class="rec-badge rec-yes">&#9654; Recorded</span>
  537. <audio class="audio-player" controls preload="none"
  538. src="/api/speakers/${encodeURIComponent(s.id)}/recording"></audio>`
  539. : `<span class="rec-badge rec-no">No sample</span>`;
  540. tr.innerHTML = `
  541. <td class="sid">${esc(s.id)}</td>
  542. <td>
  543. <div class="name-cell">
  544. <span class="name-display" onclick="startEdit(this, '${esc(s.id)}')"
  545. title="Click to edit">${esc(s.name)}</span>
  546. <input class="name-input" style="display:none"
  547. onblur="saveEdit(this,'${esc(s.id)}')"
  548. onkeydown="nameKeydown(event,this,'${esc(s.id)}')">
  549. </div>
  550. </td>
  551. <td>${recHtml}</td>
  552. <td>
  553. <div class="actions">
  554. <button class="btn btn-ghost btn-sm"
  555. onclick="openUploadModal('${esc(s.id)}', '${esc(s.name)}')">
  556. &#127926; ${s.has_recording ? 'Replace' : 'Upload'}
  557. </button>
  558. <button class="btn btn-danger btn-sm"
  559. onclick="deleteSpeaker('${esc(s.id)}')">&#128465;</button>
  560. </div>
  561. </td>`;
  562. return tr;
  563. }
  564. function esc(str) {
  565. return String(str)
  566. .replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
  567. .replace(/"/g,'&quot;').replace(/'/g,'&#39;');
  568. }
  569. function filterTable() {
  570. const q = document.getElementById('search').value.toLowerCase().trim();
  571. let visible = 0;
  572. document.querySelectorAll('#tbody tr').forEach(tr => {
  573. const match = !q || tr.dataset.id.includes(q) || tr.dataset.name.includes(q);
  574. tr.classList.toggle('hidden', !match);
  575. if (match) visible++;
  576. });
  577. document.getElementById('count').textContent =
  578. q ? `${visible} of ${speakers.length} speakers` : `${speakers.length} speakers`;
  579. }
  580. function startEdit(span, id) {
  581. const input = span.nextElementSibling;
  582. input.value = span.textContent;
  583. span.style.display = 'none';
  584. input.style.display = '';
  585. input.focus();
  586. input.select();
  587. }
  588. function nameKeydown(e, input, id) {
  589. if (e.key === 'Enter') { input.blur(); }
  590. if (e.key === 'Escape') { cancelEdit(input); }
  591. }
  592. function cancelEdit(input) {
  593. const span = input.previousElementSibling;
  594. input.style.display = 'none';
  595. span.style.display = '';
  596. }
  597. async function saveEdit(input, id) {
  598. const name = input.value.trim();
  599. const span = input.previousElementSibling;
  600. if (!name || name === span.textContent) { cancelEdit(input); return; }
  601. const res = await fetch(`/api/speakers/${encodeURIComponent(id)}`, {
  602. method: 'PUT',
  603. headers: {'Content-Type': 'application/json'},
  604. body: JSON.stringify({name})
  605. });
  606. if (res.ok) {
  607. span.textContent = name;
  608. const tr = input.closest('tr');
  609. tr.dataset.name = name.toLowerCase();
  610. toast('Saved');
  611. } else {
  612. toast('Save failed', true);
  613. }
  614. cancelEdit(input);
  615. }
  616. function openAddModal() {
  617. const nums = speakers
  618. .filter(s => /^SPEAKER_\\d+$/.test(s.id))
  619. .map(s => parseInt(s.id.split('_')[1]));
  620. const next = nums.length ? Math.max(...nums) + 1 : 0;
  621. document.getElementById('new-id').value = `SPEAKER_${String(next).padStart(2,'0')}`;
  622. document.getElementById('new-name').value = '';
  623. document.getElementById('add-modal').classList.add('open');
  624. setTimeout(() => document.getElementById('new-name').focus(), 50);
  625. }
  626. function closeAddModal(e) {
  627. if (!e || e.target === document.getElementById('add-modal'))
  628. document.getElementById('add-modal').classList.remove('open');
  629. }
  630. async function addSpeaker() {
  631. const id = document.getElementById('new-id').value.trim();
  632. const name = document.getElementById('new-name').value.trim();
  633. if (!id || !name) { toast('ID and name are required', true); return; }
  634. const res = await fetch('/api/speakers', {
  635. method: 'POST',
  636. headers: {'Content-Type': 'application/json'},
  637. body: JSON.stringify({id, name})
  638. });
  639. if (res.ok) {
  640. closeAddModal();
  641. toast(`Added ${name}`);
  642. await load();
  643. } else {
  644. const err = await res.json().catch(() => ({detail:'Error'}));
  645. toast(err.detail || 'Failed', true);
  646. }
  647. }
  648. async function deleteSpeaker(id) {
  649. const s = speakers.find(x => x.id === id);
  650. if (!confirm(`Remove "${s?.name || id}" from the speaker list?`)) return;
  651. const res = await fetch(`/api/speakers/${encodeURIComponent(id)}`, {method:'DELETE'});
  652. if (res.ok) { toast('Removed'); await load(); }
  653. else { toast('Delete failed', true); }
  654. }
  655. function openUploadModal(id, name) {
  656. uploadTarget = id;
  657. document.getElementById('upload-title').textContent = `Voice Sample — ${name}`;
  658. document.getElementById('upload-status').textContent = '';
  659. document.getElementById('file-input').value = '';
  660. document.getElementById('upload-modal').classList.add('open');
  661. }
  662. function closeUploadModal(e) {
  663. if (!e || e.target === document.getElementById('upload-modal')) {
  664. document.getElementById('upload-modal').classList.remove('open');
  665. uploadTarget = null;
  666. load();
  667. }
  668. }
  669. function onDragOver(e) { e.preventDefault(); document.getElementById('drop-zone').classList.add('drag'); }
  670. function onDragLeave() { document.getElementById('drop-zone').classList.remove('drag'); }
  671. function onDrop(e) { e.preventDefault(); onDragLeave(); uploadFile(e.dataTransfer.files[0]); }
  672. async function uploadFile(file) {
  673. if (!file || !uploadTarget) return;
  674. const status = document.getElementById('upload-status');
  675. status.style.color = '#2563eb';
  676. status.textContent = `Uploading ${file.name} (${Math.round(file.size/1024)} KB)…`;
  677. const form = new FormData();
  678. form.append('file', file);
  679. const res = await fetch(`/api/speakers/${encodeURIComponent(uploadTarget)}/recording`, {
  680. method: 'POST', body: form
  681. });
  682. if (res.ok) {
  683. const data = await res.json();
  684. status.style.color = '#166534';
  685. status.textContent = `✓ Saved — ${data.file} (${data.kb} KB)`;
  686. toast('Recording saved');
  687. } else {
  688. status.style.color = '#dc2626';
  689. status.textContent = 'Upload failed';
  690. toast('Upload failed', true);
  691. }
  692. }
  693. // ── Toast ─────────────────────────────────────────────────────────────────────
  694. let toastTimer;
  695. function toast(msg, error = false) {
  696. const el = document.getElementById('toast');
  697. el.textContent = msg;
  698. el.className = 'show' + (error ? ' error' : '');
  699. clearTimeout(toastTimer);
  700. toastTimer = setTimeout(() => el.className = '', 2500);
  701. }
  702. // ── Test Playback ─────────────────────────────────────────────────────────────
  703. let pbExpanded = true;
  704. let pbPollTimer = null;
  705. let pbFiles = [];
  706. function togglePbCard() {
  707. pbExpanded = !pbExpanded;
  708. document.getElementById('pb-body').style.display = pbExpanded ? '' : 'none';
  709. document.getElementById('pb-chevron').textContent = pbExpanded ? '▼' : '▶';
  710. }
  711. async function loadTestFiles() {
  712. try {
  713. const res = await fetch('/api/test/files');
  714. const data = await res.json();
  715. pbFiles = data.files;
  716. } catch { pbFiles = []; }
  717. renderTestFiles();
  718. }
  719. function renderTestFiles() {
  720. const tbody = document.getElementById('test-tbody');
  721. const empty = document.getElementById('test-empty-row');
  722. tbody.innerHTML = '';
  723. if (pbFiles.length === 0) {
  724. tbody.appendChild(empty);
  725. return;
  726. }
  727. pbFiles.forEach(f => tbody.appendChild(makeTestRow(f)));
  728. }
  729. function makeTestRow(f) {
  730. const tr = document.createElement('tr');
  731. tr.innerHTML = `
  732. <td style="font-family:monospace;font-size:.88rem">${esc(f.filename)}</td>
  733. <td style="color:#64748b;white-space:nowrap">${f.mb} MB</td>
  734. <td>
  735. <div class="actions">
  736. <button class="btn btn-primary btn-sm"
  737. onclick="pbStart('${esc(f.filename)}')">&#9654; Play</button>
  738. <button class="btn btn-danger btn-sm"
  739. onclick="pbDeleteFile('${esc(f.filename)}')">&#128465;</button>
  740. </div>
  741. </td>`;
  742. return tr;
  743. }
  744. function pbDragOver(e) {
  745. e.preventDefault();
  746. document.getElementById('test-drop').classList.add('drag');
  747. }
  748. function pbDragLeave() {
  749. document.getElementById('test-drop').classList.remove('drag');
  750. }
  751. function pbDrop(e) {
  752. e.preventDefault();
  753. pbDragLeave();
  754. pbUpload(e.dataTransfer.files[0]);
  755. }
  756. async function pbUpload(file) {
  757. if (!file) return;
  758. const status = document.getElementById('test-upload-status');
  759. status.style.color = '#2563eb';
  760. status.textContent = `Uploading ${file.name} (${(file.size/1024/1024).toFixed(1)} MB)…`;
  761. const form = new FormData();
  762. form.append('file', file);
  763. const res = await fetch('/api/test/upload', { method: 'POST', body: form });
  764. if (res.ok) {
  765. const d = await res.json();
  766. status.style.color = '#166534';
  767. status.textContent = `✓ ${d.filename} (${d.mb} MB)`;
  768. toast('Recording uploaded');
  769. document.getElementById('test-file-input').value = '';
  770. await loadTestFiles();
  771. } else {
  772. const err = await res.json().catch(() => ({detail: 'Upload failed'}));
  773. status.style.color = '#dc2626';
  774. status.textContent = err.detail || 'Upload failed';
  775. toast(err.detail || 'Upload failed', true);
  776. }
  777. }
  778. async function pbStart(filename) {
  779. const speed = parseFloat(document.getElementById('pb-speed').value);
  780. const res = await fetch('/api/test/start', {
  781. method: 'POST',
  782. headers: {'Content-Type': 'application/json'},
  783. body: JSON.stringify({ filename, speed }),
  784. });
  785. if (res.ok) {
  786. document.getElementById('pb-stop-btn').disabled = false;
  787. toast(`Playing at ${speed}×`);
  788. pbStartPoll();
  789. } else {
  790. const err = await res.json().catch(() => ({detail: 'Failed to start'}));
  791. toast(err.detail || 'Failed to start', true);
  792. }
  793. }
  794. async function pbStop() {
  795. const res = await fetch('/api/test/stop', { method: 'POST' });
  796. pbStopPoll();
  797. if (res.ok) toast('Playback stopped');
  798. setPbIdle();
  799. }
  800. async function pbDeleteFile(filename) {
  801. if (!confirm(`Delete "${filename}"?`)) return;
  802. await fetch(`/api/test/files/${encodeURIComponent(filename)}`, { method: 'DELETE' });
  803. toast('Deleted');
  804. await loadTestFiles();
  805. }
  806. function pbStartPoll() {
  807. pbStopPoll();
  808. pbPollTimer = setInterval(pbPoll, 1000);
  809. pbPoll();
  810. }
  811. function pbStopPoll() {
  812. if (pbPollTimer) { clearInterval(pbPollTimer); pbPollTimer = null; }
  813. }
  814. async function pbPoll() {
  815. let data;
  816. try {
  817. const res = await fetch('/api/test/status');
  818. data = await res.json();
  819. } catch { return; }
  820. const fill = document.getElementById('pb-fill');
  821. const statusEl = document.getElementById('pb-status-text');
  822. const timeEl = document.getElementById('pb-time-text');
  823. fill.style.width = (data.progress || 0) + '%';
  824. const elapsed = data.elapsed ? fmtTime(data.elapsed) : '';
  825. const duration = data.duration ? fmtTime(data.duration) : '';
  826. if (data.state === 'loading' || data.state === 'starting') {
  827. statusEl.className = '';
  828. statusEl.textContent = 'Loading file…';
  829. timeEl.textContent = '';
  830. } else if (data.state === 'playing') {
  831. statusEl.className = '';
  832. statusEl.textContent = `Playing: ${data.file}`;
  833. timeEl.textContent = elapsed && duration ? `${elapsed} / ${duration}` : '';
  834. } else if (data.state === 'done') {
  835. statusEl.className = '';
  836. statusEl.textContent = `Done: ${data.file}`;
  837. timeEl.textContent = duration;
  838. fill.style.width = '100%';
  839. pbStopPoll();
  840. document.getElementById('pb-stop-btn').disabled = true;
  841. } else if (data.state === 'error') {
  842. statusEl.className = 'pb-error';
  843. statusEl.textContent = `Error: ${data.error}`;
  844. timeEl.textContent = '';
  845. pbStopPoll();
  846. document.getElementById('pb-stop-btn').disabled = true;
  847. } else {
  848. pbStopPoll();
  849. setPbIdle();
  850. }
  851. }
  852. function setPbIdle() {
  853. document.getElementById('pb-stop-btn').disabled = true;
  854. document.getElementById('pb-fill').style.width = '0%';
  855. const statusEl = document.getElementById('pb-status-text');
  856. statusEl.className = '';
  857. statusEl.textContent = 'Idle';
  858. document.getElementById('pb-time-text').textContent = '';
  859. }
  860. function fmtTime(secs) {
  861. const s = Math.floor(secs);
  862. const m = Math.floor(s / 60);
  863. return `${m}:${String(s % 60).padStart(2, '0')}`;
  864. }
  865. // ── Keyboard shortcuts ────────────────────────────────────────────────────────
  866. document.addEventListener('keydown', e => {
  867. if (e.key === 'Escape') { closeAddModal(); closeUploadModal(); }
  868. if ((e.ctrlKey || e.metaKey) && e.key === 'f') {
  869. e.preventDefault();
  870. document.getElementById('search').focus();
  871. }
  872. });
  873. // ── Boot ──────────────────────────────────────────────────────────────────────
  874. load();
  875. loadTestFiles();
  876. </script>
  877. </body>
  878. </html>
  879. """
  880. @app.get("/", response_class=HTMLResponse)
  881. def index():
  882. return HTML
  883. # ── Entry point ───────────────────────────────────────────────────────────────
  884. if __name__ == "__main__":
  885. print("[Admin] Speaker admin running at http://localhost:8001")
  886. uvicorn.run(app, host="0.0.0.0", port=8001, log_level="warning")