Spaces:
Running
Running
File size: 6,400 Bytes
009fd18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | import "./styles.css";
import { ConversationController } from "./app/controller";
import { AppStore } from "./app/store";
import {
CALIBRATION_INSTRUCTIONS,
CALIBRATION_PROMPT,
createInitialState,
} from "./app/types";
import { TransformersAsrAdapter } from "./adapters/asr";
import { WebLLMChatAdapter } from "./adapters/llm";
import { PocketTTSAdapter } from "./adapters/tts";
import { BrowserAudioCaptureService } from "./services/audio-capture";
import { BrowserCapabilitiesService } from "./services/capabilities";
import { LocalPersistenceService } from "./services/persistence";
import { BrowserPlaybackService } from "./services/playback";
const store = new AppStore(createInitialState());
const controller = new ConversationController(store, {
capabilities: new BrowserCapabilitiesService(),
audioCapture: new BrowserAudioCaptureService(),
asr: new TransformersAsrAdapter(store.getState().config.asrModelId),
llm: new WebLLMChatAdapter(store.getState().config.llmModelId),
tts: new PocketTTSAdapter(store.getState().config.ttsModelId),
playback: new BrowserPlaybackService(),
persistence: new LocalPersistenceService(),
});
type ViewName = "calibration" | "assistant";
let currentView: ViewName = "calibration";
const calibrationView = document.querySelector<HTMLElement>("#calibration-view");
const assistantView = document.querySelector<HTMLElement>("#assistant-view");
const calibrationButton =
document.querySelector<HTMLButtonElement>("#calibration-button");
const calibrationCopy =
document.querySelector<HTMLParagraphElement>("#calibration-copy");
const calibrationPrompt =
document.querySelector<HTMLParagraphElement>("#calibration-prompt");
const calibrationFill =
document.querySelector<HTMLDivElement>("#calibration-fill");
const calibrationTimer =
document.querySelector<HTMLParagraphElement>("#calibration-timer");
const benchmarkSummary =
document.querySelector<HTMLParagraphElement>("#benchmark-summary");
const calibrationError =
document.querySelector<HTMLDivElement>("#calibration-error");
const transcriptLog = document.querySelector<HTMLDivElement>("#transcript-log");
const transcriptEmpty =
document.querySelector<HTMLDivElement>("#transcript-empty");
const recordButton = document.querySelector<HTMLButtonElement>("#record-button");
const assistantError =
document.querySelector<HTMLDivElement>("#assistant-error");
if (
!calibrationView ||
!assistantView ||
!calibrationButton ||
!calibrationCopy ||
!calibrationPrompt ||
!calibrationFill ||
!calibrationTimer ||
!benchmarkSummary ||
!calibrationError ||
!transcriptLog ||
!transcriptEmpty ||
!recordButton ||
!assistantError
) {
throw new Error("The demo shell is missing required DOM nodes.");
}
const isAssistantActivePhase = (phase: ReturnType<typeof store.getState>["phase"]) =>
phase === "arming" ||
phase === "listening" ||
phase === "thinking" ||
phase === "speaking";
const render = () => {
const state = store.getState();
calibrationView.hidden = currentView !== "calibration";
assistantView.hidden = currentView !== "assistant";
calibrationCopy.textContent = state.runtimeReady
? "Calibration is complete. Continue when you're ready to open the assistant."
: state.phase === "benchmarking"
? "Calibration is running now. Keep reading the prompt clearly until the timer ends."
: CALIBRATION_INSTRUCTIONS;
calibrationPrompt.textContent = CALIBRATION_PROMPT;
calibrationFill.style.width = `${Math.round(state.calibrationProgress * 100)}%`;
calibrationTimer.textContent = state.phase === "benchmarking"
? state.calibrationRecording
? `${state.calibrationSecondsRemaining ?? 0}s remaining`
: "Finalizing calibration..."
: state.runtimeReady
? "Calibration complete."
: "Calibration required.";
benchmarkSummary.hidden = !state.benchmarkSummary;
benchmarkSummary.textContent = state.benchmarkSummary ?? "";
calibrationButton.disabled = state.phase === "benchmarking";
calibrationButton.textContent = state.phase === "benchmarking"
? "Calibrating..."
: state.runtimeReady
? "Continue"
: "Calibrate";
transcriptLog.replaceChildren();
if (state.turns.length === 0) {
transcriptEmpty.hidden = false;
transcriptEmpty.textContent = state.runtimeReady
? 'Press Record and say "Hello, my name is _____."'
: "Complete calibration first.";
transcriptLog.append(transcriptEmpty);
} else {
transcriptEmpty.hidden = true;
for (const turn of state.turns) {
const element = document.createElement("article");
element.className = "turn";
element.dataset.role = turn.role;
const header = document.createElement("div");
header.className = "turn-header";
header.innerHTML = `<span>${turn.role}</span><span>${turn.audioStatus}</span>`;
const body = document.createElement("div");
body.className = "turn-body";
body.textContent = turn.transcript || "…";
element.append(header, body);
transcriptLog.append(element);
}
}
recordButton.disabled = !state.runtimeReady || state.phase === "benchmarking";
recordButton.textContent =
state.phase === "listening" || state.phase === "arming"
? "Stop Recording"
: state.phase === "thinking" || state.phase === "speaking"
? "Stop"
: "Record";
calibrationError.hidden =
currentView !== "calibration" || !state.errorMessage;
calibrationError.textContent =
currentView === "calibration" ? state.errorMessage ?? "" : "";
assistantError.hidden = currentView !== "assistant" || !state.errorMessage;
assistantError.textContent =
currentView === "assistant" ? state.errorMessage ?? "" : "";
};
calibrationButton.addEventListener("click", () => {
controller.setConsentAccepted(true);
if (store.getState().runtimeReady) {
currentView = "assistant";
render();
return;
}
void controller.runBenchmark();
});
recordButton.addEventListener("click", () => {
controller.setConsentAccepted(true);
void controller.start();
});
window.addEventListener("beforeunload", () => {
void controller.dispose();
});
store.subscribe(() => {
render();
});
void controller.bootstrap().then(() => {
render();
});
if (import.meta.hot) {
import.meta.hot.accept();
import.meta.hot.dispose(() => {
void controller.dispose();
});
}
|