Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web demo refactor #408

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions demo/web/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore artifacts:
build
coverage
1 change: 1 addition & 0 deletions demo/web/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
51 changes: 6 additions & 45 deletions demo/web/index.html
Original file line number Diff line number Diff line change
@@ -1,57 +1,18 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<script src="node_modules/@picovoice/web-voice-processor/dist/iife/index.js"></script>
<script src="node_modules/@picovoice/cheetah-web/dist/iife/index.js"></script>
<script src="models/cheetahModel.js"></script>
<script type="application/javascript">
let cheetah = null;

function writeMessage(message) {
console.log(message);
document.getElementById("status").innerHTML = message;
}

function cheetahErrorCallback(error) {
writeMessage(error);
}

function cheetahTranscriptionCallback(cheetahTranscript) {
document.getElementById("result").innerHTML += cheetahTranscript.transcript;
if (cheetahTranscript.isEndpoint) {
document.getElementById("result").innerHTML += "<br>";
}
}

async function startCheetah(accessKey) {
writeMessage("Cheetah is loading. Please wait...");
try {
cheetah = await CheetahWeb.CheetahWorker.create(
accessKey,
cheetahTranscriptionCallback,
cheetahModel,
{ enableAutomaticPunctuation: true }
);

writeMessage("Cheetah worker ready!");

writeMessage(
"WebVoiceProcessor initializing. Microphone permissions requested ..."
);
await window.WebVoiceProcessor.WebVoiceProcessor.subscribe(cheetah);
writeMessage("WebVoiceProcessor ready and listening!");
} catch (err) {
cheetahErrorCallback(err);
}
}
</script>
<script src="scripts/cheetah.js"></script>
</head>
<body>
<h1>Cheetah Web Demo</h1>
<p>This demo uses Cheetah for Web and the WebVoiceProcessor to:</p>
<ol>
<li>
Create an instance of Cheetah with the model file provided or default model file.
Create an instance of Cheetah with the model file provided or default
model file.
</li>
<li>
Acquire microphone (& ask permission) data stream and convert to voice
Expand All @@ -78,8 +39,8 @@ <h1>Cheetah Web Demo</h1>
onclick="startCheetah(document.getElementById('accessKey').value)"
/>
<hr />
<div id="status" style="white-space: pre;"></div>
<br>
<div id="status" style="white-space: pre"></div>
<br />
<div id="result"></div>
</body>
</html>
3 changes: 2 additions & 1 deletion demo/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@picovoice/web-voice-processor": "~4.0.8"
},
"devDependencies": {
"http-server": "^14.0.0"
"http-server": "^14.0.0",
"prettier": "3.5.1"
}
}
39 changes: 39 additions & 0 deletions demo/web/scripts/cheetah.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
let cheetah = null;

function writeMessage(message) {
console.log(message);
document.getElementById("status").innerHTML = message;
}

function cheetahErrorCallback(error) {
writeMessage(error);
}

function cheetahTranscriptionCallback(cheetahTranscript) {
document.getElementById("result").innerHTML += cheetahTranscript.transcript;
if (cheetahTranscript.isEndpoint) {
document.getElementById("result").innerHTML += "<br>";
}
}

async function startCheetah(accessKey) {
writeMessage("Cheetah is loading. Please wait...");
try {
cheetah = await CheetahWeb.CheetahWorker.create(
accessKey,
cheetahTranscriptionCallback,
cheetahModel,
{ enableAutomaticPunctuation: true },
);

writeMessage("Cheetah worker ready!");

writeMessage(
"WebVoiceProcessor initializing. Microphone permissions requested ...",
);
await window.WebVoiceProcessor.WebVoiceProcessor.subscribe(cheetah);
writeMessage("WebVoiceProcessor ready and listening!");
} catch (err) {
cheetahErrorCallback(err);
}
}
22 changes: 12 additions & 10 deletions demo/web/scripts/run_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ const fs = require("fs");
const path = require("path");
const testData = require("../../../resources/.test/test_data.json");

availableLanguages = testData["tests"]["language_tests"].map((x) => x["language"]);
availableLanguages = testData["tests"]["language_tests"].map(
(x) => x["language"],
);

const language = process.argv.slice(2)[0];
if (!language) {
console.error(
`Choose the language you would like to run the demo in with "yarn start [language]".\nAvailable languages are ${availableLanguages.join(
", "
)}`
", ",
)}`,
);
process.exit(1);
}

if (!availableLanguages.includes(language)) {
console.error(
`'${language}' is not an available demo language.\nAvailable languages are ${availableLanguages.join(
", "
)}`
", ",
)}`,
);
process.exit(1);
}
Expand All @@ -40,7 +42,7 @@ const modelDir = path.join(rootDir, "lib", "common");
const modelName = `cheetah_params${suffix}.pv`;
fs.copyFileSync(
path.join(modelDir, modelName),
path.join(outputDirectory, modelName)
path.join(outputDirectory, modelName),
);

fs.writeFileSync(
Expand All @@ -53,12 +55,12 @@ fs.writeFileSync(
(function () {
if (typeof module !== "undefined" && typeof module.exports !== "undefined")
module.exports = cheetahModel;
})();`
})();`,
);

const command = (process.platform === "win32") ? "npx.cmd" : "npx";
const command = process.platform === "win32" ? "npx.cmd" : "npx";

child_process.execSync(`${command} http-server -a localhost -p 5000`, {
shell: true,
stdio: 'inherit'
});
stdio: "inherit",
});
5 changes: 5 additions & 0 deletions demo/web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ portfinder@^1.0.28:
debug "^3.2.7"
mkdirp "^0.5.6"

prettier@3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.5.1.tgz#22fac9d0b18c0b92055ac8fb619ac1c7bef02fb7"
integrity sha512-hPpFQvHwL3Qv5AdRvBFMhnKo4tYxp0ReXiPn2bxkiohEX6mBeBwEpBSQTkD458RaaDKQMYSp4hX4UtfUTA5wDw==

qs@^6.4.0:
version "6.11.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.0.tgz#fd0d963446f7a65e1367e01abd85429453f0c37a"
Expand Down