-
Notifications
You must be signed in to change notification settings - Fork 59.8k
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
feat: implement language detection for Microsoft Voice TTS #5582
base: main
Are you sure you want to change the base?
Conversation
@hengstchon is attempting to deploy a commit to the NextChat Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThis pull request introduces modifications to enhance the text-to-speech functionality in the Changes
Possibly related PRs
Suggested reviewers
Poem
π Recent review detailsConfiguration used: CodeRabbit UI β Files ignored due to path filters (1)
π Files selected for processing (4)
π§ Files skipped from review as they are similar to previous changes (3)
π Additional comments (2)app/components/chat.tsx (2)
The import statement is correctly placed with other constant imports.
Improve language detection accuracy. The language detection has two potential issues:
Apply this diff to fix the issues: const { markdownToTxt } = require("markdown-to-txt");
-const textContent = markdownToTxt(text);
+let detectLang = 'default';
+try {
+ const detectionResult = eld.detect(text);
+ if (detectionResult && detectionResult.language) {
+ detectLang = detectionResult.language;
+ }
+} catch (error) {
+ console.error('Language detection failed:', error);
+}
+const textContent = markdownToTxt(text);
const edgeVoiceName =
detectLang in MS_EDGE_TTS_VOICES
? MS_EDGE_TTS_VOICES[detectLang as keyof typeof MS_EDGE_TTS_VOICES]
: accessStore.edgeVoiceName();
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
π§Ή Outside diff range and nitpick comments (4)
app/constant.ts (2)
273-332
: Approve the addition of MS_EDGE_TTS_VOICES enumeration with a suggestion.The new
MS_EDGE_TTS_VOICES
enumeration is a well-structured addition that supports the implementation of language detection for Microsoft Voice TTS. It provides a comprehensive mapping of language codes to specific TTS voices, which aligns with the PR objectives.Consider adding a comment above the enumeration to briefly explain its purpose and any special cases (e.g., fallback voices for certain languages). This would enhance code readability and maintainability. For example:
// Mapping of language codes to Microsoft Edge TTS voice names. // Note: Some languages use fallback voices (e.g., 'be' and 'tl' use 'en-US-AlloyMultilingualNeural'). export enum MS_EDGE_TTS_VOICES { // ... (existing enum entries) }
273-332
: Consider updating TTS-related constants for consistency.The addition of
MS_EDGE_TTS_VOICES
enhances the TTS capabilities of the application. To fully integrate this new feature:
- Consider adding a new constant for the default Edge TTS voice, similar to
DEFAULT_TTS_VOICE
.- Update
DEFAULT_TTS_ENGINES
to include "Edge-TTS" if it's not already present.- If applicable, add a constant for Edge TTS models, similar to
DEFAULT_TTS_MODELS
.These changes would ensure consistency across all TTS-related constants and make it easier to use the new Edge TTS voices throughout the application.
app/components/chat.tsx (2)
Line range hint
1211-1212
: Use 'import' Instead of 'require' for ES6 ModulesUsing
require
inside a function is not recommended in ES6 modules and may cause issues with bundlers. Replacerequire
withimport
at the top level.Apply this change:
-const { markdownToTxt } = require("markdown-to-txt"); +import { markdownToTxt } from "markdown-to-txt";
Line range hint
1210-1230
: Avoid Re-initializing Constants in 'openaiSpeech' FunctionVariables like
api
andconfig
are re-initialized every timeopenaiSpeech
is called. Consider moving these initializations outside the function if they remain constant to optimize performance.Refactor the code as follows:
-async function openaiSpeech(text: string) { - if (speechStatus) { - ttsPlayer.stop(); - setSpeechStatus(false); - } else { - var api: ClientApi; - api = new ClientApi(ModelProvider.GPT); - const config = useAppConfig.getState(); +const api: ClientApi = new ClientApi(ModelProvider.GPT); +const config = useAppConfig.getState(); +async function openaiSpeech(text: string) { + if (speechStatus) { + ttsPlayer.stop(); + setSpeechStatus(false); + } else { setSpeechLoading(true); ttsPlayer.init(); let audioBuffer: ArrayBuffer;
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
β Files ignored due to path filters (1)
yarn.lock
is excluded by!**/yarn.lock
,!**/*.lock
π Files selected for processing (3)
- app/components/chat.tsx (3 hunks)
- app/constant.ts (1 hunks)
- package.json (1 hunks)
π Additional comments (3)
package.json (1)
28-28
: New dependency added: eld@1.0.0The addition of the
eld
package aligns with the PR objective of implementing language detection for Microsoft Voice TTS. This package will enable the application to identify the language of input text, which is crucial for selecting the appropriate Microsoft TTS voice.A few points to consider:
- The version
^1.0.0
allows for compatible updates. This is generally good practice, but ensure that the package maintains backwards compatibility in minor updates.- As this is a new dependency, make sure to update any relevant documentation or setup instructions for other developers.
- Verify that this package is actively maintained and compatible with the project's Node.js version.
To ensure the package is being used correctly, let's check its usage in the codebase:
β Verification successful
Dependency
eld@^1.0.0
is properly integratedThe
eld
package is correctly imported inapp/components/chat.tsx
, aligning with the PR objective of implementing language detection for Microsoft Voice TTS. This confirms that the addition ofeld
is necessary and appropriately utilized within the codebase.π Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for imports or requires of the 'eld' package rg --type typescript --type javascript 'import.*from.*eld|require\(.*eld.*\)'Length of output: 114
Script:
#!/bin/bash # Search for imports or requires of the 'eld' package using correct file types rg --type ts --type js 'import.*from.*eld|require\(.*eld.*\)'Length of output: 114
app/constant.ts (1)
273-332
: Summary of changes and impactThe addition of the
MS_EDGE_TTS_VOICES
enumeration toapp/constant.ts
is a well-structured and valuable enhancement that supports the implementation of language detection for Microsoft Voice TTS. This change:
- Provides a comprehensive mapping of language codes to specific TTS voices.
- Aligns with the existing structure and purpose of the file.
- Enhances the application's TTS capabilities without disrupting existing functionality.
To fully leverage this addition, consider updating related TTS constants and adding a brief explanatory comment as suggested in previous comments.
app/components/chat.tsx (1)
108-108
: Import 'MS_EDGE_TTS_VOICES' CorrectlyThe import statement for
MS_EDGE_TTS_VOICES
is correctly added from the'../constant'
module, aligning with its usage in the code.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add Error Handling for Language Detection
The call to eld.detect(text).language
may return undefined
or throw an error if the language cannot be detected. To ensure stability, add error handling for cases where language detection fails.
Apply the following changes to handle potential errors:
const { markdownToTxt } = require("markdown-to-txt");
const textContent = markdownToTxt(text);
+let detectLang = 'default';
+try {
+ const detectionResult = eld.detect(text);
+ if (detectionResult && detectionResult.language) {
+ detectLang = detectionResult.language;
+ }
+} catch (error) {
+ console.error('Language detection failed:', error);
+}
const edgeVoiceName =
detectLang in MS_EDGE_TTS_VOICES
? MS_EDGE_TTS_VOICES[detectLang as keyof typeof MS_EDGE_TTS_VOICES]
: accessStore.edgeVoiceName();
Committable suggestion was skipped due to low confidence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
π§Ή Outside diff range and nitpick comments (1)
app/global.d.ts (1)
38-42
: LGTM! Consider adding a comment for clarity.The new module declaration for "eld" is correctly implemented and aligns with the PR objectives. It provides proper type information for the language detection feature.
Consider adding a brief comment above the module declaration to explain its purpose:
+// Declaration for the 'eld' package used for language detection declare module "eld" { export const eld: { detect: (text: string) => { language: string }; }; }
Your build has completed! |
π» εζ΄η±»ε | Change Type
π εζ΄θ―΄ζ | Description of Change
This update introduces the use of the eld package to detect the language of the input text. Based on the detected language, the corresponding Microsoft TTS voice is selected. If the detected language is not supported, the application falls back to the default voice configuration.
π θ‘₯ε δΏ‘ζ― | Additional Information
You can find the list of languages supported by the eld package here.
For a comprehensive list of Microsoft TTS voices, please refer to the documentation here.
Summary by CodeRabbit
New Features
Bug Fixes
Chores
eld
package.