62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_cors import CORS
|
|
import os
|
|
import tempfile
|
|
import subprocess
|
|
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
|
|
WHISPER_CPP_PATH = "/whisper.cpp"
|
|
MODEL_PATH = "/whisper.cpp/models/ggml-base.en.bin"
|
|
|
|
@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']
|
|
|
|
# Save the uploaded file to a temporary location
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_audio:
|
|
audio_file.save(tmp_audio.name)
|
|
tmp_path = tmp_audio.name
|
|
|
|
try:
|
|
# Run whisper.cpp to transcribe the audio
|
|
cmd = [
|
|
WHISPER_CPP_PATH,
|
|
"-m", MODEL_PATH,
|
|
"-f", tmp_path,
|
|
"--output-txt",
|
|
"--output-file", os.path.join(tempfile.gettempdir(), "output")
|
|
]
|
|
|
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
|
|
if result.returncode != 0:
|
|
return jsonify({
|
|
"error": "Transcription failed",
|
|
"stderr": result.stderr
|
|
}), 500
|
|
|
|
# Read the output file
|
|
output_file = os.path.join(tempfile.gettempdir(), "output.txt")
|
|
with open(output_file, 'r') as f:
|
|
transcription = f.read()
|
|
|
|
return jsonify({
|
|
"transcription": transcription.strip()
|
|
})
|
|
|
|
finally:
|
|
# Clean up temporary files
|
|
if os.path.exists(tmp_path):
|
|
os.unlink(tmp_path)
|
|
output_file = os.path.join(tempfile.gettempdir(), "output.txt")
|
|
if os.path.exists(output_file):
|
|
os.unlink(output_file)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=4002)
|