admin.py 41 KB

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