Error when passing audio file to curl command for Whisper Open AI model

160 Views Asked by At

Here is my main.py file

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

app = Flask(__name__)

# Create a pipeline for automatic speech recognition (ASR)
asr = pipeline("automatic-speech-recognition", model="openai/whisper-large")

@app.route('/recognize', methods=['POST'])
def recognize():
    try:
        # Get the audio file from the request
        audio_file = request.files['audio']

        # Perform automatic speech recognition on the audio
        transcription = asr(audio_file.read())

        # Return the transcription as JSON response
        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)

I start my app with python3 main.py command.

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 -

Then from another terminal window I ssh to the same server where my app is running and I try to use curl to pass a flac audio file but I get an error.

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

I tried using a file from internet but got the same output :

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

Can someone help me to sort this out please ?

1

There are 1 best solutions below

0
Tomas.R On

It worked when I used an audio file with .wav format

curl -X POST -F "audio=@/home/ubuntu/audio.wav" http://localhost:5000/recognize
{"transcription":{"text":" I have a dream that one day this nation will rise up and live out the true meaning of its creed."}}