Skip to content

Commit 5a205d2

Browse files
Add multi-language support for speech input on web. (#586)
* Add multi-language support for speech input on web. * Action PR comments.
1 parent cfff195 commit 5a205d2

File tree

8 files changed

+1151
-13
lines changed

8 files changed

+1151
-13
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include $(AZURE_ENV_FILE)
2020
help: ## 💬 This help message :)
2121
@grep -E '[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-23s\033[0m %s\n", $$1, $$2}'
2222

23-
ci: lint unittest functionaltest build-frontend ## 🚀 Continuous Integration (called by Github Actions)
23+
ci: lint unittest functionaltest build-frontend unittest-frontend ## 🚀 Continuous Integration (called by Github Actions)
2424

2525
lint: ## 🧹 Lint the code
2626
@echo -e "\e[34m$@\e[0m" || true
@@ -42,6 +42,10 @@ build-frontend: ## 🏗️ Build the Frontend webapp
4242
@echo -e "\e[34m$@\e[0m" || true
4343
@cd code/frontend && npm install && npm run build
4444

45+
unittest-frontend: ## 🏗️ Unit test the Frontend webapp
46+
@echo -e "\e[34m$@\e[0m" || true
47+
@cd code/frontend && npm install && npm run build && npm run test
48+
4549
azd-login: ## 🔑 Login to Azure with azd and a SPN
4650
@echo -e "\e[34m$@\e[0m" || true
4751
@azd auth login --client-id ${AZURE_CLIENT_ID} --client-secret ${AZURE_CLIENT_SECRET} --tenant-id ${AZURE_TENANT_ID}

code/frontend/.mocharc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"loader": "ts-node/esm",
3+
"extensions": [
4+
"ts"
5+
],
6+
"spec": [
7+
"./**/test/*.test.ts"
8+
]
9+
}

code/frontend/package-lock.json

Lines changed: 1083 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/frontend/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"dev": "tsc && vite",
88
"build": "tsc && vite build",
9-
"watch": "tsc && vite build --watch"
9+
"watch": "tsc && vite build --watch",
10+
"test": "mocha"
1011
},
1112
"dependencies": {
1213
"@babel/traverse": "^7.24.0",
@@ -29,13 +30,18 @@
2930
"uuid": "^9.0.1"
3031
},
3132
"devDependencies": {
33+
"@types/chai": "^4.3.14",
3234
"@types/lodash-es": "^4.17.12",
35+
"@types/mocha": "^10.0.6",
3336
"@types/node": "^20.12.2",
3437
"@types/react": "^18.2.73",
3538
"@types/react-dom": "^18.2.23",
3639
"@types/uuid": "^9.0.8",
3740
"@vitejs/plugin-react": "^4.2.1",
41+
"chai": "^5.1.0",
42+
"mocha": "^10.4.0",
3843
"prettier": "^3.2.5",
44+
"ts-node": "^10.9.2",
3945
"typescript": "^5.4.3",
4046
"vite": "^5.2.7"
4147
}

code/frontend/src/pages/chat/Chat.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import {
66
SquareRegular,
77
} from "@fluentui/react-icons";
88
import {
9-
SpeechConfig,
10-
AudioConfig,
119
SpeechRecognizer,
1210
ResultReason,
11+
AutoDetectSourceLanguageResult,
1312
} from "microsoft-cognitiveservices-speech-sdk";
1413
import ReactMarkdown from "react-markdown";
1514
import remarkGfm from "remark-gfm";
@@ -18,12 +17,12 @@ import { v4 as uuidv4 } from "uuid";
1817

1918
import styles from "./Chat.module.css";
2019
import Azure from "../../assets/Azure.svg";
20+
import { multiLingualSpeechRecognizer } from "../../util/SpeechToText";
2121

2222
import {
2323
ChatMessage,
2424
ConversationRequest,
25-
conversationApi,
26-
customConversationApi,
25+
customConversationApi,
2726
Citation,
2827
ToolMessageContent,
2928
ChatResponse,
@@ -168,12 +167,12 @@ const Chat = () => {
168167
"Azure Speech subscription key or region is not defined."
169168
);
170169
} else {
171-
const speechConfig = SpeechConfig.fromSubscription(
172-
subscriptionKey,
173-
serviceRegion
174-
);
175-
const audioConfig = AudioConfig.fromDefaultMicrophoneInput();
176-
const recognizer = new SpeechRecognizer(speechConfig, audioConfig);
170+
const recognizer = multiLingualSpeechRecognizer(subscriptionKey, serviceRegion, [
171+
"en-US",
172+
"fr-FR",
173+
"de-DE",
174+
"it-IT"
175+
]);
177176
recognizerRef.current = recognizer; // Store the recognizer in the ref
178177

179178
recognizerRef.current.recognized = (s, e) => {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
SpeechConfig,
3+
AudioConfig,
4+
SpeechRecognizer,
5+
AutoDetectSourceLanguageConfig,
6+
} from "microsoft-cognitiveservices-speech-sdk";
7+
8+
9+
export const multiLingualSpeechRecognizer = (
10+
subscriptionKey: string,
11+
serviceRegion: string,
12+
languages: string[]
13+
) => {
14+
const speechConfig = SpeechConfig.fromSubscription(
15+
subscriptionKey,
16+
serviceRegion
17+
);
18+
const audioConfig = AudioConfig.fromDefaultMicrophoneInput();
19+
const autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig.fromLanguages(languages)
20+
return SpeechRecognizer.FromConfig(speechConfig, autoDetectSourceLanguageConfig, audioConfig);
21+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from "chai";
2+
import { it } from "mocha";
3+
import { multiLingualSpeechRecognizer } from "../src/util/SpeechToText.js";
4+
5+
describe("SpeechToText", () => {
6+
it("creates a speech recognizer with multiple languages", async () => {
7+
const key = "key";
8+
const region = "region";
9+
const languages = ["en-US", "fr-FR", "de-DE", "it-IT"];
10+
const recognizer = multiLingualSpeechRecognizer(key, region, languages);
11+
12+
expect(recognizer.properties.getProperty("SpeechServiceConnection_Key")).to.equal(key);
13+
expect(recognizer.properties.getProperty("SpeechServiceConnection_Region")).to.equal(region);
14+
expect(recognizer.properties.getProperty("SpeechServiceConnection_AutoDetectSourceLanguages")).to.equal(languages.join(","));
15+
});
16+
});

code/frontend/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"path": "./tsconfig.node.json"
3232
}
3333
]
34-
}
34+
}

0 commit comments

Comments
 (0)