-
Notifications
You must be signed in to change notification settings - Fork 59.3k
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: cache tts audio 缓存tts语音 #5650
Open
Dakai
wants to merge
7
commits into
ChatGPTNextWeb:main
Choose a base branch
from
Dakai:cache_audio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−18
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a90090c
feat: add PlayIcon and StopPlayIcon to chat component
Dakai 853212c
feat: add arrayBufferToWav utility for audio processing
Dakai ad1ce92
style: update audio element styling in chat module
Dakai e64a4f2
Merge remote-tracking branch 'upstream/main' into cache_audio
Dakai 39f5d26
follow the rabbit to fix potential issues
Dakai afd75e0
style: fix formatting issues in chat.tsx file
Dakai 85e70f5
style: update max-width in chat message item
Dakai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,7 +117,7 @@ import { MultimodalContent } from "../client/api"; | |
|
||
const localStorage = safeLocalStorage(); | ||
import { ClientApi } from "../client/api"; | ||
import { createTTSPlayer } from "../utils/audio"; | ||
import { createTTSPlayer, arrayBufferToWav } from "../utils/audio"; | ||
import { MsEdgeTTS, OUTPUT_FORMAT } from "../utils/ms_edge_tts"; | ||
|
||
const ttsPlayer = createTTSPlayer(); | ||
|
@@ -1121,6 +1121,14 @@ function _Chat() { | |
); | ||
}; | ||
|
||
const updateMessageAudio = (msgId?: string, audio_url?: string) => { | ||
chatStore.updateCurrentSession((session) => { | ||
session.messages = session.messages.map((m) => | ||
m.id === msgId ? { ...m, audio_url } : m, | ||
); | ||
}); | ||
}; | ||
|
||
const onDelete = (msgId: string) => { | ||
deleteMessage(msgId); | ||
}; | ||
|
@@ -1197,7 +1205,7 @@ function _Chat() { | |
const accessStore = useAccessStore(); | ||
const [speechStatus, setSpeechStatus] = useState(false); | ||
const [speechLoading, setSpeechLoading] = useState(false); | ||
async function openaiSpeech(text: string) { | ||
async function openaiSpeech(text: string): Promise<string | undefined> { | ||
if (speechStatus) { | ||
ttsPlayer.stop(); | ||
setSpeechStatus(false); | ||
|
@@ -1227,16 +1235,22 @@ function _Chat() { | |
}); | ||
} | ||
setSpeechStatus(true); | ||
ttsPlayer | ||
.play(audioBuffer, () => { | ||
setSpeechStatus(false); | ||
}) | ||
.catch((e) => { | ||
console.error("[OpenAI Speech]", e); | ||
showToast(prettyObject(e)); | ||
try { | ||
const waveFile = arrayBufferToWav(audioBuffer); | ||
lloydzhou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const audioFile = new Blob([waveFile], { type: "audio/wav" }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wav格式体积比较大,后面可以尝试增加mp3格式进行保存? |
||
|
||
const audioUrl: string = await uploadImageRemote(audioFile); | ||
await ttsPlayer.play(audioBuffer, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不应该在保存音频之后再播放。这样会增加延迟。体验不好 |
||
setSpeechStatus(false); | ||
}) | ||
.finally(() => setSpeechLoading(false)); | ||
}); | ||
return audioUrl; | ||
} catch (e) { | ||
console.error("[Speech Error]", e); | ||
showToast(prettyObject(e)); | ||
setSpeechStatus(false); | ||
} finally { | ||
setSpeechLoading(false); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1793,9 +1807,12 @@ function _Chat() { | |
<SpeakIcon /> | ||
) | ||
} | ||
onClick={() => | ||
openaiSpeech(getMessageTextContent(message)) | ||
} | ||
onClick={async () => { | ||
const url = await openaiSpeech( | ||
getMessageTextContent(message), | ||
); | ||
updateMessageAudio(message.id, url); | ||
}} | ||
/> | ||
)} | ||
</> | ||
|
@@ -1830,7 +1847,11 @@ function _Chat() { | |
))} | ||
</div> | ||
)} | ||
<div className={styles["chat-message-item"]}> | ||
<div | ||
className={`${styles["chat-message-item"]} ${ | ||
message.audio_url ? styles["audio-message"] : "" | ||
}`} | ||
> | ||
<Markdown | ||
key={message.streaming ? "loading" : "done"} | ||
content={getMessageTextContent(message)} | ||
|
@@ -1879,6 +1900,16 @@ function _Chat() { | |
})} | ||
</div> | ||
)} | ||
{message.audio_url && ( | ||
<audio | ||
preload="auto" | ||
controls | ||
className={styles["chat-message-item-audio"]} | ||
> | ||
<source type="audio/mp3" src={message.audio_url} /> | ||
Sorry, your browser does not support HTML5 audio. | ||
</audio> | ||
)} | ||
</div> | ||
|
||
<div className={styles["chat-message-action-date"]}> | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这里需要多测试一下不同的模型。因为不同的client/platform/xxxx.ts里面处理消息的逻辑可能是不一致的。要测一下不同的模型,这里加了一个字段会不会有影响。
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.
应该去扩展ChatMessage的类型 而不是RequestMessage
RequestMessage是模型参数
audio_url属于扩展属性