I have a simple Flask endpoint for transcribing audio that a user sends:
@app.route('/transcribe', methods=['POST'])
def endpoint_transcribe():
if 'audio' not in request.files:
return jsonify({"error": "No audio file provided"}), 400
audio_file = request.files['audio']
try:
# save a temporary instance of the file to satisfy the API
audio_file.seek(0)
temp_path = "./temp_audio.webm"
audio_file.save(temp_path)
with open(temp_path, "rb") as file:
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=file
)
# clean up
os.remove(temp_path)
return jsonify(transcript=transcription.text)
except Exception as e:
print(e)
return jsonify({"error": str(e)}), 500
This works fine, but I’m writing and reading the file to disk each time a user makes a request. Is there a better way?