You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用curl向Whisper OpenAI模型的Flask接口传输音频文件时出错

音频转写API的curl请求错误排查与解决

问题背景

基于Flask和Whisper模型搭建了自动语音转写API,服务启动正常,但通过curl上传音频文件时持续报错。

代码文件(main.py)

from flask import Flask, request, jsonify
from transformers import pipeline

app = Flask(__name__)

# 创建自动语音识别管道
asr = pipeline("automatic-speech-recognition", model="openai/whisper-large")

@app.route('/recognize', methods=['POST'])
def recognize():
    try:
        # 从请求中获取音频文件
        audio_file = request.files['audio']

        # 执行语音转写
        transcription = asr(audio_file.read())

        # 返回JSON格式的转写结果
        response = {"transcription": transcription}
        return jsonify(response)

    except Exception as e:
        return jsonify({"error": str(e)})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

服务启动日志

执行启动命令python3 main.py后,日志如下:

python3 main.py 

 * Serving Flask app 'main'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://10.16.4.81:5000
Press CTRL+C to quit
127.0.0.1 - - [15/Sep/2023 11:12:53] "POST /recognize HTTP/1.1" 200 -
127.0.0.1 - - [15/Sep/2023 11:15:01] "POST /recognize HTTP/1.1" 200 -
127.0.0.1 - - [15/Sep/2023 11:15:55] "POST /recognize HTTP/1.1" 200 -
127.0.0.1 - - [15/Sep/2023 11:17:48] "POST /recognize HTTP/1.1" 200 -
127.0.0.1 - - [15/Sep/2023 11:25:05] "POST /recognize HTTP/1.1" 200 -

错误现象

  1. 上传本地FLAC文件时报错:
curl -X POST -F "audio=@/home/ubuntu/speech.flac" http://10.16.4.81:5000/recognize
curl: (26) Failed to open/read local data from file/application
  1. 尝试直接上传在线FLAC文件时同样报错:
curl -X POST -F "audio=@https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac" http://localhost:5000/recognize
curl: (26) Failed to open/read local data from file/application

解决方案

1. 本地文件上传问题修复

  • 验证文件路径与权限:
    执行以下命令确认文件存在且当前用户有读取权限:
    ls -l /home/ubuntu/speech.flac
    # 尝试读取文件内容,验证权限
    head -20 /home/ubuntu/speech.flac
    
    如果文件不存在,检查路径拼写;如果权限不足,执行sudo chmod +r /home/ubuntu/speech.flac添加读取权限。
  • 处理特殊路径:如果文件路径包含空格或特殊字符,需将路径用单引号包裹,例如:
    curl -X POST -F "audio=@'/home/ubuntu/my speech.flac'" http://10.16.4.81:5000/recognize
    

2. 在线文件上传的正确方式

curl的-F @语法仅支持本地文件,无法直接引用远程URL。可以通过以下两种方式处理:

  • 管道直接传输:先下载远程文件,通过管道直接发送给API:
    curl -X POST -F "audio=@-" http://10.16.4.81:5000/recognize < <(curl -L https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac)
    
  • 先下载再上传:将远程文件下载到本地后,再执行上传命令:
    # 下载文件到当前目录
    curl -L -O https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac
    # 上传本地文件
    curl -X POST -F "audio=@mlk.flac" http://10.16.4.81:5000/recognize
    

3. 服务端额外验证(可选)

可以在Flask代码中添加日志,确认文件是否被正确接收:

@app.route('/recognize', methods=['POST'])
def recognize():
    try:
        audio_file = request.files['audio']
        # 添加日志打印文件信息
        print(f"Received file: {audio_file.filename}, Content-Type: {audio_file.content_type}")
        transcription = asr(audio_file.read())
        response = {"transcription": transcription}
        return jsonify(response)
    except Exception as e:
        return jsonify({"error": str(e)})

重启服务后,观察终端日志是否有文件信息输出,进一步确认请求是否到达服务端。

内容的提问来源于stack exchange,提问作者Tomas.R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 02:04:54