admin.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. Also serves the fullscreen display
  6. page for tablets / TVs and the SSE stream that feeds it.
  7. Runs on port 8001 alongside bridge.py.
  8. Access at:
  9. http://localhost:8001 ← speaker admin
  10. http://[PC-IP]:8001/display ← fullscreen display for tablets
  11. """
  12. import asyncio
  13. import json
  14. import shutil
  15. import threading
  16. from contextlib import asynccontextmanager
  17. from pathlib import Path
  18. import paho.mqtt.client as mqtt
  19. from fastapi import FastAPI, HTTPException, UploadFile, File
  20. from fastapi.responses import HTMLResponse, FileResponse, StreamingResponse
  21. from pydantic import BaseModel
  22. import uvicorn
  23. SPEAKERS_FILE = Path(__file__).parent / "speakers.json"
  24. RECORDINGS_DIR = Path(__file__).parent / "recordings"
  25. TEST_RECORDINGS_DIR = Path(__file__).parent / "test_recordings"
  26. RECORDINGS_DIR.mkdir(exist_ok=True)
  27. TEST_RECORDINGS_DIR.mkdir(exist_ok=True)
  28. ALLOWED_AUDIO_EXTS = {".wav", ".mp3", ".m4a", ".ogg", ".flac", ".webm", ".aiff"}
  29. MQTT_HOST = "localhost"
  30. MQTT_PORT = 1883
  31. MQTT_TOPIC_TEXT = "display/text"
  32. MQTT_TOPIC_CLEAR = "display/clear"
  33. # ── SSE broadcast state ───────────────────────────────────────────────────────
  34. _sse_clients: set[asyncio.Queue] = set()
  35. _sse_lock = threading.Lock()
  36. _event_loop: asyncio.AbstractEventLoop | None = None
  37. def _broadcast(data: str) -> None:
  38. if _event_loop is None:
  39. return
  40. with _sse_lock:
  41. for q in list(_sse_clients):
  42. try:
  43. _event_loop.call_soon_threadsafe(q.put_nowait, data)
  44. except Exception:
  45. pass
  46. # ── MQTT subscriber ───────────────────────────────────────────────────────────
  47. def _build_mqtt_subscriber() -> mqtt.Client:
  48. client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
  49. def on_connect(client, userdata, flags, rc, props):
  50. if rc == 0:
  51. client.subscribe([(MQTT_TOPIC_TEXT, 0), (MQTT_TOPIC_CLEAR, 0)])
  52. print("[Admin] MQTT subscribed to display/text and display/clear")
  53. else:
  54. print(f"[Admin] MQTT connect failed: {rc}")
  55. def on_message(client, userdata, msg):
  56. payload = msg.payload.decode("utf-8", errors="replace")
  57. if msg.topic == MQTT_TOPIC_CLEAR:
  58. _broadcast("event: clear\ndata: {}\n\n")
  59. else:
  60. _broadcast(f"event: text\ndata: {payload}\n\n")
  61. client.on_connect = on_connect
  62. client.on_message = on_message
  63. client.reconnect_delay_set(min_delay=1, max_delay=30)
  64. client.connect_async(MQTT_HOST, MQTT_PORT)
  65. client.loop_start()
  66. return client
  67. # ── App lifecycle ─────────────────────────────────────────────────────────────
  68. _mqtt_sub: mqtt.Client | None = None
  69. @asynccontextmanager
  70. async def lifespan(app: FastAPI):
  71. global _mqtt_sub, _event_loop
  72. _event_loop = asyncio.get_running_loop()
  73. _mqtt_sub = _build_mqtt_subscriber()
  74. yield
  75. if _mqtt_sub:
  76. _mqtt_sub.loop_stop()
  77. _mqtt_sub.disconnect()
  78. app = FastAPI(title="Speaker Admin", lifespan=lifespan)
  79. # ── Speaker data helpers ──────────────────────────────────────────────────────
  80. def _load() -> dict[str, str]:
  81. if SPEAKERS_FILE.exists():
  82. try:
  83. return json.loads(SPEAKERS_FILE.read_text(encoding="utf-8"))
  84. except Exception:
  85. pass
  86. return {}
  87. def _save(data: dict[str, str]) -> None:
  88. SPEAKERS_FILE.write_text(
  89. json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8"
  90. )
  91. def _recording_path(sid: str) -> Path | None:
  92. for ext in (".wav", ".mp3", ".m4a", ".ogg", ".webm", ".flac"):
  93. p = RECORDINGS_DIR / f"{sid}{ext}"
  94. if p.exists():
  95. return p
  96. return None
  97. # ── Speaker API ───────────────────────────────────────────────────────────────
  98. class NameBody(BaseModel):
  99. name: str | None = None
  100. role: str | None = None
  101. location: str | None = None
  102. class AddBody(BaseModel):
  103. id: str
  104. name: str = ""
  105. role: str = ""
  106. location: str = ""
  107. @app.get("/api/speakers")
  108. def api_list():
  109. speakers = _load()
  110. result = []
  111. for k, v in sorted(speakers.items()):
  112. if isinstance(v, dict):
  113. entry = {"id": k, "name": v.get("name", ""), "role": v.get("role", ""),
  114. "location": v.get("location", ""), "has_recording": _recording_path(k) is not None}
  115. else:
  116. entry = {"id": k, "name": str(v), "role": "", "location": "",
  117. "has_recording": _recording_path(k) is not None}
  118. result.append(entry)
  119. return {"speakers": result}
  120. @app.post("/api/speakers")
  121. def api_add(body: AddBody):
  122. speakers = _load()
  123. sid = body.id.strip()
  124. if not sid:
  125. raise HTTPException(400, "Speaker ID cannot be empty")
  126. if sid in speakers:
  127. raise HTTPException(400, f"'{sid}' already exists")
  128. speakers[sid] = {"name": body.name.strip(), "role": body.role.strip(), "location": body.location.strip()}
  129. _save(speakers)
  130. return {"ok": True, "id": sid, "name": body.name}
  131. @app.put("/api/speakers/{sid}")
  132. def api_update(sid: str, body: NameBody):
  133. speakers = _load()
  134. entry = speakers.get(sid, {})
  135. if not isinstance(entry, dict):
  136. entry = {"name": str(entry), "role": "", "location": ""}
  137. if body.name is not None:
  138. entry["name"] = body.name.strip()
  139. if body.role is not None:
  140. entry["role"] = body.role.strip()
  141. if body.location is not None:
  142. entry["location"] = body.location.strip()
  143. speakers[sid] = entry
  144. _save(speakers)
  145. return {"ok": True}
  146. @app.delete("/api/speakers/{sid}")
  147. def api_delete(sid: str):
  148. speakers = _load()
  149. speakers.pop(sid, None)
  150. _save(speakers)
  151. rec = _recording_path(sid)
  152. if rec:
  153. rec.unlink()
  154. return {"ok": True}
  155. @app.post("/api/speakers/{sid}/recording")
  156. async def api_upload(sid: str, file: UploadFile = File(...)):
  157. suffix = Path(file.filename or "audio.wav").suffix.lower() or ".wav"
  158. rec = _recording_path(sid)
  159. if rec:
  160. rec.unlink()
  161. out = RECORDINGS_DIR / f"{sid}{suffix}"
  162. with out.open("wb") as f:
  163. shutil.copyfileobj(file.file, f)
  164. speakers = _load()
  165. if sid not in speakers:
  166. speakers[sid] = sid
  167. _save(speakers)
  168. size_kb = round(out.stat().st_size / 1024)
  169. return {"ok": True, "file": out.name, "kb": size_kb}
  170. @app.get("/api/speakers/{sid}/recording")
  171. def api_playback(sid: str):
  172. rec = _recording_path(sid)
  173. if not rec:
  174. raise HTTPException(404, "No recording found")
  175. return FileResponse(rec)
  176. # ── Test playback state ───────────────────────────────────────────────────────
  177. _playback_task: asyncio.Task | None = None
  178. _playback_status: dict = {
  179. "state": "idle", # idle | loading | playing | done | error
  180. "file": None,
  181. "progress": 0, # 0–100
  182. "elapsed": 0.0, # seconds streamed so far
  183. "duration": 0.0, # total file duration in seconds
  184. "error": None,
  185. }
  186. BRIDGE_INJECT_URL = "http://127.0.0.1:8002/inject"
  187. async def _stream_file(filepath: Path, speed: float) -> None:
  188. global _playback_status
  189. try:
  190. import miniaudio
  191. import httpx
  192. except ImportError as e:
  193. _playback_status.update({"state": "error", "error": f"Missing package: {e}"})
  194. return
  195. try:
  196. _playback_status["state"] = "loading"
  197. info = miniaudio.get_file_info(str(filepath))
  198. duration = info.duration
  199. _playback_status.update({
  200. "state": "playing", "duration": round(duration, 1),
  201. "elapsed": 0.0, "progress": 0,
  202. })
  203. chunk_frames = 4096
  204. chunk_secs = chunk_frames / 16000
  205. elapsed = 0.0
  206. stream = miniaudio.stream_file(
  207. str(filepath),
  208. output_format=miniaudio.SampleFormat.SIGNED16,
  209. nchannels=1,
  210. sample_rate=16000,
  211. frames_to_read=chunk_frames,
  212. )
  213. async with httpx.AsyncClient() as client:
  214. for chunk in stream:
  215. await client.post(BRIDGE_INJECT_URL, content=bytes(chunk))
  216. elapsed += chunk_secs
  217. _playback_status["elapsed"] = round(elapsed, 1)
  218. _playback_status["progress"] = (
  219. min(99, round(elapsed / duration * 100)) if duration else 0
  220. )
  221. await asyncio.sleep(chunk_secs / speed)
  222. _playback_status.update({
  223. "state": "done", "progress": 100, "elapsed": round(duration, 1),
  224. })
  225. except asyncio.CancelledError:
  226. _playback_status.update({
  227. "state": "idle", "file": None, "progress": 0, "elapsed": 0.0,
  228. })
  229. except Exception as exc:
  230. _playback_status.update({"state": "error", "error": str(exc), "progress": 0})
  231. print(f"[Playback] {exc}")
  232. # ── Test recording API ────────────────────────────────────────────────────────
  233. @app.post("/api/test/upload")
  234. async def api_test_upload(file: UploadFile = File(...)):
  235. suffix = Path(file.filename or "recording.wav").suffix.lower()
  236. if suffix not in ALLOWED_AUDIO_EXTS:
  237. raise HTTPException(400, f"Unsupported format '{suffix}'")
  238. stem = Path(file.filename).stem[:80].replace(" ", "_")
  239. out = TEST_RECORDINGS_DIR / f"{stem}{suffix}"
  240. try:
  241. with out.open("wb") as f:
  242. shutil.copyfileobj(file.file, f)
  243. except OSError as e:
  244. raise HTTPException(500, f"Could not save file: {e}")
  245. return {"ok": True, "filename": out.name, "mb": round(out.stat().st_size / 1024 / 1024, 1)}
  246. @app.get("/api/test/files")
  247. def api_test_list():
  248. files = []
  249. for p in sorted(TEST_RECORDINGS_DIR.iterdir()):
  250. if p.suffix.lower() in ALLOWED_AUDIO_EXTS:
  251. files.append({
  252. "filename": p.name,
  253. "mb": round(p.stat().st_size / 1024 / 1024, 1),
  254. })
  255. return {"files": files}
  256. @app.delete("/api/test/files/{filename:path}")
  257. def api_test_delete(filename: str):
  258. p = TEST_RECORDINGS_DIR / Path(filename).name
  259. try:
  260. if p.exists():
  261. p.unlink()
  262. except OSError as e:
  263. raise HTTPException(500, f"Could not delete: {e}")
  264. return {"ok": True}
  265. class PlaybackBody(BaseModel):
  266. filename: str
  267. speed: float = 1.0
  268. @app.post("/api/test/start")
  269. async def api_test_start(body: PlaybackBody):
  270. global _playback_task
  271. if _playback_task and not _playback_task.done():
  272. raise HTTPException(409, "Playback already running — stop it first")
  273. p = TEST_RECORDINGS_DIR / Path(body.filename).name
  274. if not p.exists():
  275. raise HTTPException(404, "File not found")
  276. speed = max(0.25, min(8.0, body.speed))
  277. _playback_status.update({"state": "starting", "file": p.name, "progress": 0, "error": None})
  278. _playback_task = asyncio.create_task(_stream_file(p, speed))
  279. return {"ok": True}
  280. @app.post("/api/test/stop")
  281. async def api_test_stop():
  282. global _playback_task
  283. if _playback_task and not _playback_task.done():
  284. _playback_task.cancel()
  285. try:
  286. await _playback_task
  287. except asyncio.CancelledError:
  288. pass
  289. _playback_status.update({"state": "idle", "file": None, "progress": 0, "elapsed": 0.0})
  290. return {"ok": True}
  291. @app.get("/api/test/status")
  292. def api_test_status():
  293. return _playback_status
  294. # ── SSE display stream ────────────────────────────────────────────────────────
  295. @app.get("/api/display/stream")
  296. async def display_stream():
  297. q: asyncio.Queue[str] = asyncio.Queue(maxsize=50)
  298. with _sse_lock:
  299. _sse_clients.add(q)
  300. async def generator():
  301. try:
  302. yield ": heartbeat\n\n"
  303. while True:
  304. try:
  305. data = await asyncio.wait_for(q.get(), timeout=25)
  306. yield data
  307. except asyncio.TimeoutError:
  308. yield ": heartbeat\n\n"
  309. except (asyncio.CancelledError, GeneratorExit):
  310. pass
  311. finally:
  312. with _sse_lock:
  313. _sse_clients.discard(q)
  314. return StreamingResponse(
  315. generator(),
  316. media_type="text/event-stream",
  317. headers={
  318. "Cache-Control": "no-cache",
  319. "Connection": "keep-alive",
  320. "X-Accel-Buffering": "no",
  321. },
  322. )
  323. # ── Web UI (admin) ────────────────────────────────────────────────────────────
  324. HTML = """<!DOCTYPE html>
  325. <html lang="en">
  326. <head>
  327. <meta charset="UTF-8">
  328. <meta name="viewport" content="width=device-width, initial-scale=1">
  329. <title>Meeting Transcription for the Deaf</title>
  330. <style>
  331. *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  332. body { font-family: system-ui, sans-serif; background: #f0f4f8; color: #1e293b; }
  333. header {
  334. background: #1e3a5f; color: white; padding: 16px 24px;
  335. display: flex; align-items: center; gap: 16px;
  336. }
  337. header h1 { font-size: 1.2rem; font-weight: 600; flex: 1; }
  338. header small { opacity: .7; font-size: .8rem; }
  339. .toolbar {
  340. background: white; padding: 12px 24px;
  341. display: flex; gap: 12px; align-items: center;
  342. border-bottom: 1px solid #e2e8f0;
  343. }
  344. .toolbar input[type=search] {
  345. flex: 1; max-width: 340px; padding: 8px 12px;
  346. border: 1px solid #cbd5e1; border-radius: 6px; font-size: .95rem;
  347. }
  348. .count { color: #64748b; font-size: .9rem; margin-left: auto; }
  349. .btn {
  350. display: inline-flex; align-items: center; gap: 6px;
  351. padding: 8px 16px; border-radius: 6px; border: none;
  352. cursor: pointer; font-size: .9rem; font-weight: 500; transition: filter .15s;
  353. text-decoration: none;
  354. }
  355. .btn:hover:not(:disabled) { filter: brightness(.92); }
  356. .btn:disabled { opacity: .45; cursor: not-allowed; }
  357. .btn-primary { background: #2563eb; color: white; }
  358. .btn-danger { background: #dc2626; color: white; }
  359. .btn-ghost { background: #e2e8f0; color: #334155; }
  360. .btn-display { background: #059669; color: white; }
  361. .btn-sm { padding: 4px 10px; font-size: .82rem; }
  362. table { width: 100%; border-collapse: collapse; background: white; }
  363. th {
  364. text-align: left; padding: 10px 16px; font-size: .8rem;
  365. font-weight: 600; text-transform: uppercase; letter-spacing: .05em;
  366. color: #64748b; background: #f8fafc; border-bottom: 1px solid #e2e8f0;
  367. }
  368. td { padding: 10px 16px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
  369. tr:hover td { background: #f8fafc; }
  370. tr.hidden { display: none; }
  371. .sid { font-family: monospace; font-size: .85rem; color: #475569; }
  372. .name-cell { display: flex; align-items: center; gap: 8px; }
  373. .name-display { cursor: pointer; flex: 1; }
  374. .name-display:hover { text-decoration: underline; }
  375. .name-input {
  376. flex: 1; padding: 4px 8px; border: 1px solid #2563eb;
  377. border-radius: 4px; font-size: .95rem; outline: none;
  378. }
  379. .rec-badge {
  380. display: inline-flex; align-items: center; gap: 4px;
  381. font-size: .75rem; padding: 2px 8px; border-radius: 999px;
  382. font-weight: 500;
  383. }
  384. .rec-yes { background: #dcfce7; color: #166534; }
  385. .rec-no { background: #f1f5f9; color: #94a3b8; }
  386. .actions { display: flex; gap: 6px; }
  387. /* Modal */
  388. .modal-bg {
  389. display: none; position: fixed; inset: 0;
  390. background: rgba(0,0,0,.45); z-index: 100;
  391. align-items: center; justify-content: center;
  392. }
  393. .modal-bg.open { display: flex; }
  394. .modal {
  395. background: white; border-radius: 10px; padding: 28px;
  396. width: 420px; max-width: 95vw; box-shadow: 0 20px 60px rgba(0,0,0,.25);
  397. }
  398. .modal h2 { font-size: 1.1rem; margin-bottom: 16px; }
  399. .field { margin-bottom: 14px; }
  400. .field label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: 4px; }
  401. .field input {
  402. width: 100%; padding: 8px 10px; border: 1px solid #cbd5e1;
  403. border-radius: 6px; font-size: .95rem;
  404. }
  405. .field input:focus { outline: 2px solid #2563eb; border-color: transparent; }
  406. .modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
  407. /* Upload drop zone */
  408. .upload-area {
  409. border: 2px dashed #cbd5e1; border-radius: 8px; padding: 16px;
  410. text-align: center; cursor: pointer; transition: border-color .2s;
  411. color: #64748b; font-size: .9rem;
  412. }
  413. .upload-area.drag { border-color: #2563eb; background: #eff6ff; }
  414. .upload-area input[type=file] { display: none; }
  415. /* Audio player */
  416. .audio-player { width: 180px; height: 32px; }
  417. /* Toast */
  418. #toast {
  419. position: fixed; bottom: 24px; right: 24px;
  420. background: #1e293b; color: white; padding: 10px 18px;
  421. border-radius: 8px; font-size: .9rem; transform: translateY(80px);
  422. transition: transform .25s; z-index: 200; pointer-events: none;
  423. }
  424. #toast.show { transform: translateY(0); }
  425. #toast.error { background: #dc2626; }
  426. .container { max-width: 1100px; margin: 0 auto; padding: 24px; }
  427. audio { vertical-align: middle; }
  428. /* ── Test Playback card ─────────────────────────────── */
  429. .pb-card {
  430. background: white; border-radius: 8px; margin-top: 20px;
  431. border: 1px solid #e2e8f0; overflow: hidden;
  432. }
  433. .pb-card-header {
  434. display: flex; justify-content: space-between; align-items: center;
  435. padding: 14px 20px; cursor: pointer; font-weight: 600; font-size: 1rem;
  436. background: #f8fafc; border-bottom: 1px solid #e2e8f0; user-select: none;
  437. }
  438. .pb-card-header:hover { background: #f1f5f9; }
  439. #pb-body { padding: 20px; }
  440. .pb-hint { color: #64748b; font-size: .88rem; margin-bottom: 16px; line-height: 1.5; }
  441. .pb-layout { display: flex; gap: 20px; flex-wrap: wrap; margin-bottom: 16px; }
  442. .pb-upload { flex: 1; min-width: 260px; }
  443. .pb-controls { min-width: 210px; display: flex; flex-direction: column; gap: 12px; }
  444. .pb-select {
  445. width: 100%; padding: 7px 10px; border: 1px solid #cbd5e1;
  446. border-radius: 6px; font-size: .9rem; background: white;
  447. }
  448. .pb-label { font-size: .85rem; font-weight: 500; display: block; margin-bottom: 4px; }
  449. .pb-status-bar { margin-top: 12px; }
  450. .pb-status-row {
  451. display: flex; justify-content: space-between;
  452. font-size: .85rem; margin-bottom: 5px; min-height: 18px;
  453. }
  454. .pb-progress-track {
  455. height: 8px; background: #e2e8f0; border-radius: 999px; overflow: hidden;
  456. }
  457. .pb-progress-fill {
  458. height: 100%; background: #2563eb; border-radius: 999px;
  459. width: 0%; transition: width .8s linear;
  460. }
  461. .pb-note { font-size: .78rem; color: #94a3b8; margin-top: 10px; }
  462. .pb-error { color: #dc2626; }
  463. </style>
  464. </head>
  465. <body>
  466. <header>
  467. <div style="flex:1">
  468. <h1>&#127908; Speaker Admin</h1>
  469. <small>Meeting Transcription for the Deaf — Speaker Name &amp; Voice Library</small>
  470. </div>
  471. <a href="/display" target="_blank" class="btn btn-display">&#128065; View Display</a>
  472. </header>
  473. <div class="toolbar">
  474. <input type="search" id="search" placeholder="Search by ID or name…" oninput="filterTable()">
  475. <button class="btn btn-primary" onclick="openAddModal()">&#43; Add Speaker</button>
  476. <span class="count" id="count"></span>
  477. </div>
  478. <div class="container">
  479. <!-- Speaker table -->
  480. <table id="table">
  481. <thead>
  482. <tr>
  483. <th>Speaker ID</th>
  484. <th>Initials</th>
  485. <th>Name</th>
  486. <th>Locality</th>
  487. <th>Voice Sample</th>
  488. <th>Actions</th>
  489. </tr>
  490. </thead>
  491. <tbody id="tbody"></tbody>
  492. </table>
  493. <!-- Test Playback card -->
  494. <div class="pb-card">
  495. <div class="pb-card-header" onclick="togglePbCard()">
  496. <span>&#127911; Test Recording Playback</span>
  497. <span id="pb-chevron">&#9660;</span>
  498. </div>
  499. <div id="pb-body">
  500. <p class="pb-hint">
  501. Upload a full church service recording (WAV, MP3, FLAC, OGG, M4A) to test the
  502. transcription pipeline offline. The file streams to WhisperLiveKit exactly as a
  503. live microphone would — results appear on the
  504. <a href="/display" target="_blank">display page</a> in real time.
  505. </p>
  506. <div class="pb-layout">
  507. <!-- Upload drop zone -->
  508. <div class="pb-upload">
  509. <div class="upload-area" id="test-drop"
  510. ondragover="pbDragOver(event)" ondragleave="pbDragLeave()"
  511. ondrop="pbDrop(event)"
  512. onclick="document.getElementById('test-file-input').click()">
  513. <input type="file" id="test-file-input" accept="audio/*"
  514. onchange="pbUpload(this.files[0])">
  515. <div>&#8679; Drop a recording here, or click to browse</div>
  516. <div style="font-size:.78rem;margin-top:4px;color:#94a3b8">
  517. WAV &middot; MP3 &middot; FLAC &middot; OGG &middot; M4A
  518. </div>
  519. </div>
  520. <div id="test-upload-status" style="min-height:20px;font-size:.85rem;margin-top:8px"></div>
  521. </div>
  522. <!-- Playback controls -->
  523. <div class="pb-controls">
  524. <div>
  525. <span class="pb-label">Playback Speed</span>
  526. <select id="pb-speed" class="pb-select">
  527. <option value="0.5">0.5&times; (half speed)</option>
  528. <option value="1">1&times; real-time</option>
  529. <option value="2">2&times; faster</option>
  530. <option value="4" selected>4&times; quick review</option>
  531. <option value="8">8&times; very fast</option>
  532. </select>
  533. </div>
  534. <button id="pb-stop-btn" class="btn btn-danger" onclick="pbStop()" disabled>
  535. &#9632; Stop Playback
  536. </button>
  537. </div>
  538. </div>
  539. <!-- Uploaded test files -->
  540. <table id="test-table">
  541. <thead>
  542. <tr>
  543. <th>Recording File</th>
  544. <th style="width:80px">Size</th>
  545. <th style="width:140px">Actions</th>
  546. </tr>
  547. </thead>
  548. <tbody id="test-tbody">
  549. <tr id="test-empty-row">
  550. <td colspan="3" style="color:#94a3b8;padding:16px 16px">
  551. No recordings uploaded yet
  552. </td>
  553. </tr>
  554. </tbody>
  555. </table>
  556. <!-- Status bar -->
  557. <div class="pb-status-bar">
  558. <div class="pb-status-row">
  559. <span id="pb-status-text" style="color:#64748b">Idle</span>
  560. <span id="pb-time-text" style="color:#64748b"></span>
  561. </div>
  562. <div class="pb-progress-track">
  563. <div class="pb-progress-fill" id="pb-fill"></div>
  564. </div>
  565. </div>
  566. <p class="pb-note">
  567. While a test recording plays, the live microphone in bridge.py continues to run.
  568. Keep the room quiet during testing to avoid mixing live audio with the recording.
  569. </p>
  570. </div>
  571. </div>
  572. </div><!-- /container -->
  573. <!-- Add speaker modal -->
  574. <div class="modal-bg" id="add-modal" onclick="closeAddModal(event)">
  575. <div class="modal">
  576. <h2>Add Speaker</h2>
  577. <div class="field">
  578. <label>Speaker ID
  579. <span style="color:#64748b;font-weight:normal;font-size:.8rem">
  580. (e.g. SPK_00, or any unique key)
  581. </span>
  582. </label>
  583. <input id="new-id" placeholder="SPK_00">
  584. </div>
  585. <div class="field">
  586. <label>Initials</label>
  587. <input id="new-initials" placeholder="J.B.B">
  588. </div>
  589. <div class="field">
  590. <label>Name / Role</label>
  591. <input id="new-name" placeholder="John Brown">
  592. </div>
  593. <div class="field">
  594. <label>Locality</label>
  595. <input id="new-location" placeholder="Sydney">
  596. </div>
  597. <div class="modal-actions">
  598. <button class="btn btn-ghost" onclick="closeAddModal()">Cancel</button>
  599. <button class="btn btn-primary" onclick="addSpeaker()">Add</button>
  600. </div>
  601. </div>
  602. </div>
  603. <!-- Voice sample upload modal -->
  604. <div class="modal-bg" id="upload-modal" onclick="closeUploadModal(event)">
  605. <div class="modal">
  606. <h2 id="upload-title">Upload Voice Sample</h2>
  607. <p style="color:#64748b;font-size:.85rem;margin-bottom:12px">
  608. Upload a 10–60 second clear speech recording.<br>
  609. Supported: WAV, MP3, M4A, OGG, FLAC, WebM
  610. </p>
  611. <div class="upload-area" id="drop-zone"
  612. ondragover="onDragOver(event)" ondragleave="onDragLeave(event)"
  613. ondrop="onDrop(event)" onclick="document.getElementById('file-input').click()">
  614. <input type="file" id="file-input" accept="audio/*" onchange="uploadFile(this.files[0])">
  615. <div>&#127926; Drag &amp; drop audio here, or click to browse</div>
  616. </div>
  617. <div id="upload-status" style="margin-top:10px;font-size:.85rem;color:#166534"></div>
  618. <div class="modal-actions">
  619. <button class="btn btn-ghost" onclick="closeUploadModal()">Close</button>
  620. </div>
  621. </div>
  622. </div>
  623. <div id="toast"></div>
  624. <script>
  625. let speakers = [];
  626. let uploadTarget = null;
  627. // ── Speaker table ─────────────────────────────────────────────────────────────
  628. async function load() {
  629. const res = await fetch('/api/speakers');
  630. const data = await res.json();
  631. speakers = data.speakers;
  632. render();
  633. }
  634. function render() {
  635. const tbody = document.getElementById('tbody');
  636. tbody.innerHTML = '';
  637. speakers.forEach(s => tbody.appendChild(makeRow(s)));
  638. document.getElementById('count').textContent =
  639. `${speakers.length} speaker${speakers.length !== 1 ? 's' : ''}`;
  640. filterTable();
  641. }
  642. function mkEdit(id, field, value) {
  643. return `<div class="name-cell">
  644. <span class="name-display" onclick="startEdit(this,'${id}','${field}')"
  645. title="Click to edit">${value}</span>
  646. <input class="name-input" style="display:none"
  647. onblur="saveEdit(this,'${id}','${field}')"
  648. onkeydown="editKeydown(event,this,'${id}','${field}')">
  649. </div>`;
  650. }
  651. function makeRow(s) {
  652. const tr = document.createElement('tr');
  653. tr.dataset.id = s.id;
  654. tr.dataset.name = (s.name + ' ' + s.role + ' ' + s.location).toLowerCase();
  655. const recHtml = s.has_recording
  656. ? `<span class="rec-badge rec-yes">&#9654; Recorded</span>
  657. <audio class="audio-player" controls preload="none"
  658. src="/api/speakers/${encodeURIComponent(s.id)}/recording"></audio>`
  659. : `<span class="rec-badge rec-no">No sample</span>`;
  660. tr.innerHTML = `
  661. <td class="sid">${esc(s.id)}</td>
  662. <td>${mkEdit(esc(s.id), 'name', esc(s.name))}</td>
  663. <td>${mkEdit(esc(s.id), 'role', esc(s.role))}</td>
  664. <td>${mkEdit(esc(s.id), 'location', esc(s.location))}</td>
  665. <td>${recHtml}</td>
  666. <td>
  667. <div class="actions">
  668. <button class="btn btn-ghost btn-sm"
  669. onclick="openUploadModal('${esc(s.id)}', '${esc(s.name)}')">
  670. &#127926; ${s.has_recording ? 'Replace' : 'Upload'}
  671. </button>
  672. <button class="btn btn-danger btn-sm"
  673. onclick="deleteSpeaker('${esc(s.id)}')">&#128465;</button>
  674. </div>
  675. </td>`;
  676. return tr;
  677. }
  678. function esc(str) {
  679. return String(str)
  680. .replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
  681. .replace(/"/g,'&quot;').replace(/'/g,'&#39;');
  682. }
  683. function filterTable() {
  684. const q = document.getElementById('search').value.toLowerCase().trim();
  685. let visible = 0;
  686. document.querySelectorAll('#tbody tr').forEach(tr => {
  687. const match = !q || tr.dataset.id.includes(q) || tr.dataset.name.includes(q);
  688. tr.classList.toggle('hidden', !match);
  689. if (match) visible++;
  690. });
  691. document.getElementById('count').textContent =
  692. q ? `${visible} of ${speakers.length} speakers` : `${speakers.length} speakers`;
  693. }
  694. function startEdit(span, id, field) {
  695. const input = span.nextElementSibling;
  696. input.value = span.textContent;
  697. span.style.display = 'none';
  698. input.style.display = '';
  699. input.focus();
  700. input.select();
  701. }
  702. function editKeydown(e, input, id, field) {
  703. if (e.key === 'Enter') { input.blur(); }
  704. if (e.key === 'Escape') { cancelEdit(input); }
  705. }
  706. function cancelEdit(input) {
  707. const span = input.previousElementSibling;
  708. input.style.display = 'none';
  709. span.style.display = '';
  710. }
  711. async function saveEdit(input, id, field) {
  712. const value = input.value.trim();
  713. const span = input.previousElementSibling;
  714. if (value === span.textContent) { cancelEdit(input); return; }
  715. const body = {};
  716. body[field] = value;
  717. const res = await fetch(`/api/speakers/${encodeURIComponent(id)}`, {
  718. method: 'PUT',
  719. headers: {'Content-Type': 'application/json'},
  720. body: JSON.stringify(body)
  721. });
  722. if (res.ok) {
  723. span.textContent = value;
  724. toast('Saved');
  725. } else {
  726. toast('Save failed', true);
  727. }
  728. cancelEdit(input);
  729. }
  730. function openAddModal() {
  731. const nums = speakers
  732. .filter(s => /^SPEAKER_\\d+$/.test(s.id))
  733. .map(s => parseInt(s.id.split('_')[1]));
  734. const next = nums.length ? Math.max(...nums) + 1 : 0;
  735. document.getElementById('new-id').value = `SPEAKER_${String(next).padStart(2,'0')}`;
  736. document.getElementById('new-initials').value = '';
  737. document.getElementById('new-name').value = '';
  738. document.getElementById('new-location').value = '';
  739. document.getElementById('add-modal').classList.add('open');
  740. setTimeout(() => document.getElementById('new-initials').focus(), 50);
  741. }
  742. function closeAddModal(e) {
  743. if (!e || e.target === document.getElementById('add-modal'))
  744. document.getElementById('add-modal').classList.remove('open');
  745. }
  746. async function addSpeaker() {
  747. const id = document.getElementById('new-id').value.trim();
  748. const name = document.getElementById('new-initials').value.trim();
  749. const role = document.getElementById('new-name').value.trim();
  750. const location = document.getElementById('new-location').value.trim();
  751. if (!id) { toast('Speaker ID is required', true); return; }
  752. const res = await fetch('/api/speakers', {
  753. method: 'POST',
  754. headers: {'Content-Type': 'application/json'},
  755. body: JSON.stringify({id, name, role, location})
  756. });
  757. if (res.ok) {
  758. closeAddModal();
  759. toast(`Added ${name || id}`);
  760. await load();
  761. } else {
  762. const err = await res.json().catch(() => ({detail:'Error'}));
  763. toast(err.detail || 'Failed', true);
  764. }
  765. }
  766. async function deleteSpeaker(id) {
  767. const s = speakers.find(x => x.id === id);
  768. if (!confirm(`Remove "${s?.name || id}" from the speaker list?`)) return;
  769. const res = await fetch(`/api/speakers/${encodeURIComponent(id)}`, {method:'DELETE'});
  770. if (res.ok) { toast('Removed'); await load(); }
  771. else { toast('Delete failed', true); }
  772. }
  773. function openUploadModal(id, name) {
  774. uploadTarget = id;
  775. document.getElementById('upload-title').textContent = `Voice Sample — ${name}`;
  776. document.getElementById('upload-status').textContent = '';
  777. document.getElementById('file-input').value = '';
  778. document.getElementById('upload-modal').classList.add('open');
  779. }
  780. function closeUploadModal(e) {
  781. if (!e || e.target === document.getElementById('upload-modal')) {
  782. document.getElementById('upload-modal').classList.remove('open');
  783. uploadTarget = null;
  784. load();
  785. }
  786. }
  787. function onDragOver(e) { e.preventDefault(); document.getElementById('drop-zone').classList.add('drag'); }
  788. function onDragLeave() { document.getElementById('drop-zone').classList.remove('drag'); }
  789. function onDrop(e) { e.preventDefault(); onDragLeave(); uploadFile(e.dataTransfer.files[0]); }
  790. async function uploadFile(file) {
  791. if (!file || !uploadTarget) return;
  792. const status = document.getElementById('upload-status');
  793. status.style.color = '#2563eb';
  794. status.textContent = `Uploading ${file.name} (${Math.round(file.size/1024)} KB)…`;
  795. const form = new FormData();
  796. form.append('file', file);
  797. const res = await fetch(`/api/speakers/${encodeURIComponent(uploadTarget)}/recording`, {
  798. method: 'POST', body: form
  799. });
  800. if (res.ok) {
  801. const data = await res.json();
  802. status.style.color = '#166534';
  803. status.textContent = `✓ Saved — ${data.file} (${data.kb} KB)`;
  804. toast('Recording saved');
  805. } else {
  806. status.style.color = '#dc2626';
  807. status.textContent = 'Upload failed';
  808. toast('Upload failed', true);
  809. }
  810. }
  811. // ── Toast ─────────────────────────────────────────────────────────────────────
  812. let toastTimer;
  813. function toast(msg, error = false) {
  814. const el = document.getElementById('toast');
  815. el.textContent = msg;
  816. el.className = 'show' + (error ? ' error' : '');
  817. clearTimeout(toastTimer);
  818. toastTimer = setTimeout(() => el.className = '', 2500);
  819. }
  820. // ── Test Playback ─────────────────────────────────────────────────────────────
  821. let pbExpanded = true;
  822. let pbPollTimer = null;
  823. let pbFiles = [];
  824. function togglePbCard() {
  825. pbExpanded = !pbExpanded;
  826. document.getElementById('pb-body').style.display = pbExpanded ? '' : 'none';
  827. document.getElementById('pb-chevron').textContent = pbExpanded ? '▼' : '▶';
  828. }
  829. async function loadTestFiles() {
  830. try {
  831. const res = await fetch('/api/test/files');
  832. const data = await res.json();
  833. pbFiles = data.files;
  834. } catch { pbFiles = []; }
  835. renderTestFiles();
  836. }
  837. function renderTestFiles() {
  838. const tbody = document.getElementById('test-tbody');
  839. const empty = document.getElementById('test-empty-row');
  840. tbody.innerHTML = '';
  841. if (pbFiles.length === 0) {
  842. tbody.appendChild(empty);
  843. return;
  844. }
  845. pbFiles.forEach(f => tbody.appendChild(makeTestRow(f)));
  846. }
  847. function makeTestRow(f) {
  848. const tr = document.createElement('tr');
  849. tr.innerHTML = `
  850. <td style="font-family:monospace;font-size:.88rem">${esc(f.filename)}</td>
  851. <td style="color:#64748b;white-space:nowrap">${f.mb} MB</td>
  852. <td>
  853. <div class="actions">
  854. <button class="btn btn-primary btn-sm"
  855. onclick="pbStart('${esc(f.filename)}')">&#9654; Play</button>
  856. <button class="btn btn-danger btn-sm"
  857. onclick="pbDeleteFile('${esc(f.filename)}')">&#128465;</button>
  858. </div>
  859. </td>`;
  860. return tr;
  861. }
  862. function pbDragOver(e) {
  863. e.preventDefault();
  864. document.getElementById('test-drop').classList.add('drag');
  865. }
  866. function pbDragLeave() {
  867. document.getElementById('test-drop').classList.remove('drag');
  868. }
  869. function pbDrop(e) {
  870. e.preventDefault();
  871. pbDragLeave();
  872. pbUpload(e.dataTransfer.files[0]);
  873. }
  874. async function pbUpload(file) {
  875. if (!file) return;
  876. const status = document.getElementById('test-upload-status');
  877. status.style.color = '#2563eb';
  878. status.textContent = `Uploading ${file.name} (${(file.size/1024/1024).toFixed(1)} MB)…`;
  879. const form = new FormData();
  880. form.append('file', file);
  881. const res = await fetch('/api/test/upload', { method: 'POST', body: form });
  882. if (res.ok) {
  883. const d = await res.json();
  884. status.style.color = '#166534';
  885. status.textContent = `✓ ${d.filename} (${d.mb} MB)`;
  886. toast('Recording uploaded');
  887. document.getElementById('test-file-input').value = '';
  888. await loadTestFiles();
  889. } else {
  890. const err = await res.json().catch(() => ({detail: 'Upload failed'}));
  891. status.style.color = '#dc2626';
  892. status.textContent = err.detail || 'Upload failed';
  893. toast(err.detail || 'Upload failed', true);
  894. }
  895. }
  896. async function pbStart(filename) {
  897. const speed = parseFloat(document.getElementById('pb-speed').value);
  898. const res = await fetch('/api/test/start', {
  899. method: 'POST',
  900. headers: {'Content-Type': 'application/json'},
  901. body: JSON.stringify({ filename, speed }),
  902. });
  903. if (res.ok) {
  904. document.getElementById('pb-stop-btn').disabled = false;
  905. toast(`Playing at ${speed}×`);
  906. pbStartPoll();
  907. } else {
  908. const err = await res.json().catch(() => ({detail: 'Failed to start'}));
  909. toast(err.detail || 'Failed to start', true);
  910. }
  911. }
  912. async function pbStop() {
  913. const res = await fetch('/api/test/stop', { method: 'POST' });
  914. pbStopPoll();
  915. if (res.ok) toast('Playback stopped');
  916. setPbIdle();
  917. }
  918. async function pbDeleteFile(filename) {
  919. if (!confirm(`Delete "${filename}"?`)) return;
  920. await fetch(`/api/test/files/${encodeURIComponent(filename)}`, { method: 'DELETE' });
  921. toast('Deleted');
  922. await loadTestFiles();
  923. }
  924. function pbStartPoll() {
  925. pbStopPoll();
  926. pbPollTimer = setInterval(pbPoll, 1000);
  927. pbPoll();
  928. }
  929. function pbStopPoll() {
  930. if (pbPollTimer) { clearInterval(pbPollTimer); pbPollTimer = null; }
  931. }
  932. async function pbPoll() {
  933. let data;
  934. try {
  935. const res = await fetch('/api/test/status');
  936. data = await res.json();
  937. } catch { return; }
  938. const fill = document.getElementById('pb-fill');
  939. const statusEl = document.getElementById('pb-status-text');
  940. const timeEl = document.getElementById('pb-time-text');
  941. fill.style.width = (data.progress || 0) + '%';
  942. const elapsed = data.elapsed ? fmtTime(data.elapsed) : '';
  943. const duration = data.duration ? fmtTime(data.duration) : '';
  944. if (data.state === 'loading' || data.state === 'starting') {
  945. statusEl.className = '';
  946. statusEl.textContent = 'Loading file…';
  947. timeEl.textContent = '';
  948. } else if (data.state === 'playing') {
  949. statusEl.className = '';
  950. statusEl.textContent = `Playing: ${data.file}`;
  951. timeEl.textContent = elapsed && duration ? `${elapsed} / ${duration}` : '';
  952. } else if (data.state === 'done') {
  953. statusEl.className = '';
  954. statusEl.textContent = `Done: ${data.file}`;
  955. timeEl.textContent = duration;
  956. fill.style.width = '100%';
  957. pbStopPoll();
  958. document.getElementById('pb-stop-btn').disabled = true;
  959. } else if (data.state === 'error') {
  960. statusEl.className = 'pb-error';
  961. statusEl.textContent = `Error: ${data.error}`;
  962. timeEl.textContent = '';
  963. pbStopPoll();
  964. document.getElementById('pb-stop-btn').disabled = true;
  965. } else {
  966. pbStopPoll();
  967. setPbIdle();
  968. }
  969. }
  970. function setPbIdle() {
  971. document.getElementById('pb-stop-btn').disabled = true;
  972. document.getElementById('pb-fill').style.width = '0%';
  973. const statusEl = document.getElementById('pb-status-text');
  974. statusEl.className = '';
  975. statusEl.textContent = 'Idle';
  976. document.getElementById('pb-time-text').textContent = '';
  977. }
  978. function fmtTime(secs) {
  979. const s = Math.floor(secs);
  980. const m = Math.floor(s / 60);
  981. return `${m}:${String(s % 60).padStart(2, '0')}`;
  982. }
  983. // ── Keyboard shortcuts ────────────────────────────────────────────────────────
  984. document.addEventListener('keydown', e => {
  985. if (e.key === 'Escape') { closeAddModal(); closeUploadModal(); }
  986. if ((e.ctrlKey || e.metaKey) && e.key === 'f') {
  987. e.preventDefault();
  988. document.getElementById('search').focus();
  989. }
  990. });
  991. // ── Boot ──────────────────────────────────────────────────────────────────────
  992. load();
  993. loadTestFiles();
  994. </script>
  995. </body>
  996. </html>
  997. """
  998. # ── Display page (fullscreen tablet / TV) ─────────────────────────────────────
  999. DISPLAY_HTML = """<!DOCTYPE html>
  1000. <html lang="en">
  1001. <head>
  1002. <meta charset="UTF-8">
  1003. <meta name="viewport" content="width=device-width, initial-scale=1">
  1004. <title>Live Transcription</title>
  1005. <style>
  1006. *, *::before, *::after { box-sizing: border-box; }
  1007. html, body {
  1008. margin: 0; padding: 0;
  1009. width: 100%; height: 100%;
  1010. background: #0d0d0d;
  1011. color: #eeeeee;
  1012. overflow: hidden;
  1013. }
  1014. #screen {
  1015. display: flex;
  1016. flex-direction: column;
  1017. height: 100vh;
  1018. padding: 5vh 6vw;
  1019. gap: 4vh;
  1020. justify-content: flex-start;
  1021. }
  1022. .display-line {
  1023. font-family: Georgia, 'Times New Roman', serif;
  1024. font-size: clamp(22px, 3vw, 44px);
  1025. line-height: 1.25;
  1026. letter-spacing: 0.02em;
  1027. min-height: 1.25em;
  1028. word-break: break-word;
  1029. transition: opacity 0.15s;
  1030. }
  1031. .display-line.is-speaker {
  1032. font-size: clamp(16px, 2.2vw, 32px);
  1033. color: #f5c518;
  1034. font-weight: 700;
  1035. letter-spacing: 0.1em;
  1036. font-family: system-ui, sans-serif;
  1037. padding-bottom: 1.5vh;
  1038. border-bottom: 2px solid #2a2a2a;
  1039. }
  1040. /* Small status indicator in the corner */
  1041. #conn {
  1042. position: fixed;
  1043. bottom: 12px;
  1044. right: 16px;
  1045. display: flex;
  1046. align-items: center;
  1047. gap: 6px;
  1048. font-family: system-ui, sans-serif;
  1049. font-size: 12px;
  1050. color: #333;
  1051. pointer-events: none;
  1052. }
  1053. #conn-dot {
  1054. width: 8px; height: 8px;
  1055. border-radius: 50%;
  1056. background: #555;
  1057. transition: background 0.4s;
  1058. }
  1059. #conn-dot.live { background: #22c55e; }
  1060. #conn-dot.lost { background: #dc2626; }
  1061. </style>
  1062. </head>
  1063. <body>
  1064. <div id="screen">
  1065. <div class="display-line" id="line0"></div>
  1066. <div class="display-line" id="line1"></div>
  1067. <div class="display-line" id="line2"></div>
  1068. </div>
  1069. <div id="conn">
  1070. <div id="conn-dot"></div>
  1071. <span id="conn-label">connecting</span>
  1072. </div>
  1073. <script>
  1074. const SPEAKER_RE = /^\\[(.+)\\]$/;
  1075. function applyLines(lines) {
  1076. for (let i = 0; i < 3; i++) {
  1077. const el = document.getElementById('line' + i);
  1078. const txt = String(lines[i] || '').trim();
  1079. const m = txt.match(SPEAKER_RE);
  1080. if (m) {
  1081. el.textContent = m[1];
  1082. el.className = 'display-line is-speaker';
  1083. } else {
  1084. el.textContent = txt;
  1085. el.className = 'display-line';
  1086. }
  1087. }
  1088. }
  1089. function setStatus(ok) {
  1090. const dot = document.getElementById('conn-dot');
  1091. const label = document.getElementById('conn-label');
  1092. dot.className = ok ? 'live' : 'lost';
  1093. label.textContent = ok ? 'live' : 'reconnecting…';
  1094. }
  1095. function connect() {
  1096. const es = new EventSource('/api/display/stream');
  1097. es.addEventListener('text', e => {
  1098. try { applyLines(JSON.parse(e.data).lines || []); } catch {}
  1099. });
  1100. es.addEventListener('clear', () => applyLines(['', '', '']));
  1101. es.onopen = () => setStatus(true);
  1102. es.onerror = () => {
  1103. setStatus(false);
  1104. es.close();
  1105. setTimeout(connect, 4000);
  1106. };
  1107. }
  1108. connect();
  1109. </script>
  1110. </body>
  1111. </html>
  1112. """
  1113. @app.get("/", response_class=HTMLResponse)
  1114. def index():
  1115. return HTML
  1116. @app.get("/display", response_class=HTMLResponse)
  1117. def display():
  1118. return DISPLAY_HTML
  1119. # ── Entry point ───────────────────────────────────────────────────────────────
  1120. if __name__ == "__main__":
  1121. print("[Admin] Speaker admin at http://localhost:8001")
  1122. print("[Admin] Display page at http://localhost:8001/display")
  1123. uvicorn.run(app, host="0.0.0.0", port=8001, log_level="warning")