当前位置: 首页 > article >正文

OpenAI Whisper API (InvalidRequestError)

题意: OpenAI Whisper API(无效请求错误)

问题背景:

I'm trying to use OpenAI Whisper API to transcribe my audio files. When I run it by opening my local audio files from disk, it worked perfectly. Now I'm developing a FastAPI endpoint to receive an audio file from the client and transcribe it.

“我正在尝试使用 OpenAI Whisper API 来转录我的音频文件。当我通过从磁盘打开本地音频文件运行它时,它工作得非常好。现在我正在开发一个 FastAPI 端点,用于接收客户端的音频文件并进行转录。”

However, when I try to use the same file received by FastAPI endpoint directly, it will reject the file, claiming the file received is in invalid format.

“然而,当我尝试直接使用由 FastAPI 端点接收到的相同文件时,API 会拒绝该文件,并声称接收到的文件格式无效。”

I tried to read and write the received file to the disk directly from the endpoint. Then opening the file from disk and using it in Whisper API, it works without any issues. Below is the code that shows it.

“我尝试直接从端点读取和写入接收到的文件到磁盘。然后从磁盘打开文件并在 Whisper API 中使用,这样没有任何问题。下面是展示此过程的代码。”

@app.post("/audio")
async def summarise_audio(file:UploadFile):
    audio =await file.read()

    with open("testy.wav",'wb') as f:
        f.write(audio)
    x = open("testy.wav",'rb')
    transcript = openai.Audio.transcribe("whisper-1",x) # worked
    # transcript = openai.Audio.transcribe("whisper-1",file.file) # did not work 
    return transcript

How would I go to solve this problem, could there be an issue with the file format received by FastAPI endpoint?

“我该如何解决这个问题?FastAPI 端点接收到的文件格式可能存在问题吗?”

问题解决:

Okay, after spending about 12 hours on this problem, I found a workaround for OpenAI Whisper API for it to accept the file.

“好吧,在这个问题上花了大约 12 个小时后,我找到了一个解决方法,使 OpenAI Whisper API 能够接受该文件。”

Granted I am not well versed in file reading and binary content, so if anyone has better solution than me, I would love to see the solution.

“我承认我不太擅长文件读取和二进制内容处理,所以如果有人有比我更好的解决方案,我很想看到这个解决方案。”

import io
@app.post("/audio")
async def summarise_audio(file:UploadFile):
    audio =await file.read()
    
    buffer = io.BytesIO(audio)

    buffer.name = 'testy.wav'
    transcript = openai.Audio.transcribe("whisper-1",buffer) # worked
    
    return transcript

I have to read the file content and then convert it into a file-like buffer using io.BytesIO. Here, passing in the buffer directly to OpenAI Whisper API would not work as the buffer does not have a file name. So we have to specify a name for the buffer before passing it into the OpenAI Whisper API.

“我必须读取文件内容,然后使用 `io.BytesIO` 将其转换为类似文件的缓冲区。这里直接将缓冲区传递给 OpenAI Whisper API 是行不通的,因为缓冲区没有文件名。因此,在将其传递给 OpenAI Whisper API 之前,我们必须为缓冲区指定一个文件名。”


http://www.kler.cn/a/286191.html

相关文章:

  • 【C++ 数学 括号匹配】2116. 判断一个括号字符串是否有效|2037
  • 论文阅读笔记 —— 英文论文常见缩写及含义
  • 如何在 ACP 中建模复合罐
  • STM32CubeMX6.13.0打开后不显示界面,但是任务管理器显示该程序正在运行
  • Springboot使用AOP时,需不需要引入AspectJ?
  • python 使用Whisper模型进行语音翻译
  • iPhone备忘录不小心删除了怎么办?
  • 深度学习100问16:sigmoid函数是什么
  • python源码 PBOCMaster MAC的计算函数及计算过程 2des
  • 数据结构(6.4_1)——最小生成树
  • Vue 中 Axios 配置指南
  • 使用物联网卡访问萤石云的常见问题
  • Vue——认识day06_class与style绑定
  • TESSY创建单元测试或集成测试工程
  • Spring 源码解读:手动实现自动装配与@Qualifier
  • 低代码技术助力移动端开发:简化开发流程,实现快速创新
  • 算法设计与分析:实验五 图论——桥问题
  • 每日错题(2024年9月1日)
  • 经验笔记:Apache Kafka
  • python3.10安装
  • 【C++ Primer Plus习题】8.4
  • 六、vue进阶知识点
  • VastBase——VPatch版本控制
  • 使用docker file创建镜像(thirty-seven day)
  • 存储系统总结
  • MATLAB中save_system的用法