When I try to send voice messages I always get invalid url error with. I am using whisper to convert the audio to text but for some reason I cannot seem to pass the file to the whisper. It worked when I used this in java script but not in typescript for some reason
当我尝试发送语音消息时,总是收到无效的url错误。我正在使用Whisper将音频转换为文本,但由于某种原因,我似乎无法将文件传递给Whisper。当我在Java脚本中使用它而不是在TypeScrip中使用它时,由于某种原因,它起作用了
async function createFile(path: string): Promise<File> {
const response = await fetch(path);
const data = await response.blob();
// Extract file name from the path
const fileName = path.split('/').pop() || 'unknown';
// Extract file extension and determine MIME type
const fileExtension = fileName.split('.').pop()?.toLowerCase() || '';
const mimeTypes: Record<string, string> = {
'mp3': 'audio/mpeg',
// Add more mappings as needed
};
const fileType = mimeTypes[fileExtension] || 'application/octet-stream';
const metadata = {
type: fileType
};
return new File([data], fileName, metadata);
}
async function sendAudioForTranscription(file_path:string) {
try {
// const audioData = fs.createReadStream(file_path);
const audioFile = await createFile(file_path)
const response = await openai.createTranscription(audioFile, "whisper-1");
const transcribed = response.data.text;
return transcribed;
} catch (error) {
console.error("Error transcribing the audio:", error);
return null;
}
}
I am new to this so any help would be appreciated. This is the error
我是新来的,所以任何帮助都将不胜感激。这就是错误
Error transcribing the audio: TypeError: Failed to parse URL from src\audio_files\[email protected]_B161BC6FA04DB01B8B31F5E0F83EDAD5.mp3
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[cause]: TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:405:5)
at new URL (node:internal/url:778:13)
at new Request (node:internal/deps/undici/undici:7132:25)
at fetch2 (node:internal/deps/undici/undici:10715:25)
at Object.fetch (node:internal/deps/undici/undici:11574:18)
at fetch (node:internal/process/pre_execution:270:25)
at C:\Users\Ammad Ali\Documents\Documents\alex-whatsapp-bot\build\openai\transcript.js:28:32
at C:\Users\Ammad Ali\Documents\Documents\alex-whatsapp-bot\build\openai\transcript.js:8:71
at new Promise (<anonymous>)
at __awaiter (C:\Users\Ammad Ali\Documents\Documents\alex-whatsapp-bot\build\openai\transcript.js:4:12)
at createFile (C:\Users\Ammad Ali\Documents\Documents\alex-whatsapp-bot\build\openai\transcript.js:27:12)
at Object.<anonymous> (C:\Users\Ammad Ali\Documents\Documents\alex-whatsapp-bot\build\openai\transcript.js:49:37)
at Generator.next (<anonymous>)
at C:\Users\Ammad Ali\Documents\Documents\alex-whatsapp-bot\build\openai\transcript.js:8:71
at new Promise (<anonymous>) {
input: 'src\\audio_files\\[email protected]_B161BC6FA04DB01B8B31F5E0F83EDAD5.mp3',
code: 'ERR_INVALID_URL'
}
}
To get a response back in voice message
要在语音消息中获得回复
更多回答
The url is invalid. How are you calling sendAudioForTranscription
该URL无效。您如何调用sendAudioForTranscription
@mplungjan sorry I don't understand. what do you mean?
@mplungjan抱歉,我不明白。什么意思?
sendAudioForTranscription(file_path:string) {
is getting src\audio_files\[email protected]_B161BC6FA04DB01B8B31F5E0F83EDAD5.mp3
which should be src/audio_files/[email protected]_B161BC6FA04DB01B8B31F5E0F83EDAD5.mp3
or perhaps even the @
would need to be %40
SendAudioForTranscription(FILE_PATH:STRING){正在获取源\AUDIO_FILES\[电子邮件protected]_B161BC6FA04DB01B8B31F5E0F83EDAD5.mp3,它应该是SRC/AUDIO_FILES/[Email protected]_B161BC6FA04DB01B8B31F5E0F83EDAD5.mp3,甚至@需要是%40
优秀答案推荐
我是一名优秀的程序员,十分优秀!