This commit is contained in:
Kar
2025-06-15 22:21:26 +05:30
parent 301b58b644
commit be2296e442
2 changed files with 69 additions and 63 deletions

66
app.py
View File

@@ -1,59 +1,61 @@
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
import tempfile
import subprocess
app = Flask(__name__)
CORS(app)
# 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
WHISPER_CPP_PATH = "/whisper.cpp/main"
MODEL_PATH = "/whisper.cpp/models/ggml-base.en.bin"
@app.route('/stt', methods=['POST'])
@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)
# 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 (absolute paths)
result = subprocess.run(
[
WHISPER_CPP_PATH,
"-m", MODEL_PATH,
"-f", tmp_path,
"--output-txt"
],
capture_output=True,
text=True
)
# 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",
"details": result.stderr
"stderr": result.stderr
}), 500
# Read output
with open(tmp_path + ".txt", 'r') as f:
# 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({"text": transcription.strip()})
return jsonify({
"transcription": transcription.strip()
})
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
# Clean up
# Clean up temporary files
if os.path.exists(tmp_path):
os.remove(tmp_path)
if os.path.exists(tmp_path + ".txt"):
os.remove(tmp_path + ".txt")
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)
app.run(host='0.0.0.0', port=5000)