From 25a8097eaf28269d84af9463efb03ffe63e705ee Mon Sep 17 00:00:00 2001 From: hengstchon Date: Thu, 3 Oct 2024 17:20:53 +0200 Subject: [PATCH 1/2] feat: implement language detection for Microsoft Voice TTS --- app/components/chat.tsx | 8 +++++- app/constant.ts | 60 +++++++++++++++++++++++++++++++++++++++++ package.json | 1 + yarn.lock | 5 ++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 3d5b6a4f2c4..0473c1f3126 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1,3 +1,4 @@ +import { eld } from "eld"; import { useDebouncedCallback } from "use-debounce"; import React, { useState, @@ -104,6 +105,7 @@ import { REQUEST_TIMEOUT_MS, UNFINISHED_INPUT, ServiceProvider, + MS_EDGE_TTS_VOICES, } from "../constant"; import { Avatar } from "./emoji"; import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask"; @@ -1214,7 +1216,11 @@ function _Chat() { const { markdownToTxt } = require("markdown-to-txt"); const textContent = markdownToTxt(text); if (config.ttsConfig.engine !== DEFAULT_TTS_ENGINE) { - const edgeVoiceName = accessStore.edgeVoiceName(); + const detectLang = eld.detect(text).language; + const edgeVoiceName = + detectLang in MS_EDGE_TTS_VOICES + ? MS_EDGE_TTS_VOICES[detectLang as keyof typeof MS_EDGE_TTS_VOICES] + : accessStore.edgeVoiceName(); const tts = new MsEdgeTTS(); await tts.setMetadata( edgeVoiceName, diff --git a/app/constant.ts b/app/constant.ts index 9774bb594dd..0d4f437158a 100644 --- a/app/constant.ts +++ b/app/constant.ts @@ -279,6 +279,66 @@ export const DEFAULT_TTS_VOICES = [ "nova", "shimmer", ]; +export enum MS_EDGE_TTS_VOICES { + am = "am-ET-MekdesNeural", + ar = "ar-AE-FatimaNeural", + az = "az-AZ-BanuNeural", + be = "en-US-AlloyMultilingualNeural", + bg = "bg-BG-KalinaNeural", + bn = "bn-IN-TanishaaNeural", + ca = "ca-ES-JoanaNeural", + cs = "cs-CZ-VlastaNeural", + da = "da-DK-ChristelNeural", + de = "de-DE-ConradNeural", + el = "el-GR-AthinaNeural", + en = "en-US-AndrewNeural", + es = "es-ES-ElviraNeural", + et = "et-EE-AnuNeural", + eu = "eu-ES-AinhoaNeural", + fa = "fa-IR-DilaraNeural", + fi = "fi-FI-SelmaNeural", + fr = "fr-FR-DeniseNeural", + gu = "gu-IN-DhwaniNeural", + he = "he-IL-HilaNeural", + hi = "hi-IN-AaravNeural", + hr = "hr-HR-GabrijelaNeural", + hu = "hu-HU-NoemiNeural", + hy = "hy-AM-AnahitNeural", + is = "is-IS-GudrunNeural", + it = "it-IT-ElsaNeural", + ja = "ja-JP-NanamiNeural", + ka = "ka-GE-EkaNeural", + kn = "kn-IN-SapnaNeural", + ko = "ko-KR-SunHiNeural", + lo = "lo-LA-KeomanyNeural", + lt = "lt-LT-OnaNeural", + lv = "lv-LV-EveritaNeural", + ml = "ml-IN-SobhanaNeural", + mr = "mr-IN-AarohiNeural", + ms = "ms-MY-YasminNeural", + nl = "nl-NL-FennaNeural", + no = "nb-NO-PernilleNeural", + or = "or-IN-SubhasiniNeural", + pa = "pa-IN-OjasNeural", + pl = "pl-PL-AgnieszkaNeural", + pt = "pt-PT-RaquelNeural", + ro = "ro-RO-AlinaNeural", + ru = "ru-RU-SvetlanaNeural", + sk = "sk-SK-ViktoriaNeural", + sl = "sl-SI-PetraNeural", + sq = "sq-AL-AnilaNeural", + sr = "sr-Latn-RS-NicholasNeural", + sv = "sv-SE-SofieNeural", + ta = "ta-IN-PallaviNeural", + te = "te-IN-ShrutiNeural", + th = "th-TH-PremwadeeNeural", + tl = "en-US-AlloyMultilingualNeural", + tr = "tr-TR-EmelNeural", + uk = "uk-UA-PolinaNeural", + ur = "ur-IN-GulNeural", + vi = "vi-VN-HoaiMyNeural", + zh = "zh-CN-YunxiNeural", +} const openaiModels = [ "gpt-3.5-turbo", diff --git a/package.json b/package.json index 5bca3b327f6..79fb6791910 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@vercel/analytics": "^0.1.11", "@vercel/speed-insights": "^1.0.2", "axios": "^1.7.5", + "eld": "^1.0.0", "emoji-picker-react": "^4.9.2", "fuse.js": "^7.0.0", "heic2any": "^0.0.4", diff --git a/yarn.lock b/yarn.lock index 9f8aa9f610c..9a444b4353d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3959,6 +3959,11 @@ eastasianwidth@^0.2.0: resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb" integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA== +eld@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/eld/-/eld-1.0.0.tgz#507cf2229fe3d809430fdfc3689edafeb53aafc1" + integrity sha512-+wehmSoVbWYIsQ0wDUN6b1QCGNvnfT49KD/W6QavCEq8D5/ANx+B+qy5m2sKOTh3MyBqMEDTUxc1wcajSAo/2Q== + electron-to-chromium@^1.4.284: version "1.4.345" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.345.tgz#c90b7183b39245cddf0e990337469063bfced6f0" From 554901daee2c4e981443594ff6b60967f3c00578 Mon Sep 17 00:00:00 2001 From: hengstchon Date: Thu, 3 Oct 2024 17:53:06 +0200 Subject: [PATCH 2/2] fix: eld module --- app/global.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/global.d.ts b/app/global.d.ts index 897871fec37..bcb86663acb 100644 --- a/app/global.d.ts +++ b/app/global.d.ts @@ -41,3 +41,9 @@ declare interface Window { }; }; } + +declare module "eld" { + export const eld: { + detect: (text: string) => { language: string }; + }; +}