stt-whisper-cpp-ap/app.py

60 lines
1.7 KiB
Python

import os
from flask import Flask, request, jsonify
import subprocess
app = Flask(__name__)
# Use environment variables (set in Dockerfile)
WHISPER_CPP_PATH = os.getenv("WHISPER_CPP_PATH", "/app/whisper.cpp/main") # Absolute path
MODEL_PATH = os.getenv("MODEL_PATH", "/app/whisper.cpp/models/ggml-small.en.bin") # Absolute path
@app.route('/transcribe', methods=['POST'])
def transcribe_audio():
if 'audio' not in request.files:
return jsonify({"error": "No audio file provided"}), 400
audio_file = request.files['audio']
if audio_file.filename == '':
return jsonify({"error": "Empty filename"}), 400
# Save to a temporary file
tmp_path = "/tmp/audio_upload.wav"
audio_file.save(tmp_path)
try:
# Run whisper.cpp (absolute paths)
result = subprocess.run(
[
WHISPER_CPP_PATH,
"-m", MODEL_PATH,
"-f", tmp_path,
"--output-txt"
],
capture_output=True,
text=True
)
if result.returncode != 0:
return jsonify({
"error": "Transcription failed",
"details": result.stderr
}), 500
# Read output
with open(tmp_path + ".txt", 'r') as f:
transcription = f.read()
return jsonify({"text": transcription.strip()})
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
# Clean up
if os.path.exists(tmp_path):
os.remove(tmp_path)
if os.path.exists(tmp_path + ".txt"):
os.remove(tmp_path + ".txt")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)