diff --git a/Dockerfile b/Dockerfile index 4f95b12..b8bcff2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,6 @@ -# Base image FROM ubuntu:22.04 -# Install system dependencies +# Install dependencies RUN apt-get update && apt-get install -y \ python3 \ python3-pip \ @@ -16,7 +15,7 @@ RUN apt-get update && apt-get install -y \ # Set working directory WORKDIR /app -# Clone whisper.cpp and compile +# Clone and build whisper.cpp RUN git clone https://github.com/ggerganov/whisper.cpp.git && \ cd whisper.cpp && \ make @@ -35,12 +34,13 @@ COPY app.py . # Create uploads directory RUN mkdir -p uploads -# Environment variables (adjust paths if needed) +# Correct paths (critical fix!) ENV WHISPER_CPP_PATH="/app/whisper.cpp/main" ENV MODEL_PATH="/app/whisper.cpp/models/ggml-small.en.bin" -# Expose Flask port -EXPOSE 4002 +# Verify the binary exists (debugging step) +RUN ls -lh /app/whisper.cpp/main && \ + ls -lh /app/whisper.cpp/models/ggml-small.en.bin -# Run the API +EXPOSE 5000 CMD ["python3", "app.py"] diff --git a/app.py b/app.py index 3959378..fe56f2b 100644 --- a/app.py +++ b/app.py @@ -1,18 +1,12 @@ -from flask import Flask, request, jsonify import os +from flask import Flask, request, jsonify import subprocess -from werkzeug.utils import secure_filename app = Flask(__name__) -# Configure upload folder (adjust as needed) -UPLOAD_FOLDER = "./uploads" -os.makedirs(UPLOAD_FOLDER, exist_ok=True) -app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER - -# Path to whisper.cpp executable & model (update paths as needed) -WHISPER_CPP_PATH = "./whisper.cpp/main" # Path to compiled whisper.cpp binary -MODEL_PATH = "./whisper.cpp/models/ggml-small.en.bin" # Path to model +# 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(): @@ -23,37 +17,43 @@ def transcribe_audio(): if audio_file.filename == '': return jsonify({"error": "Empty filename"}), 400 - # Save the uploaded file - filename = secure_filename(audio_file.filename) - filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) - audio_file.save(filepath) + # Save to a temporary file + tmp_path = "/tmp/audio_upload.wav" + audio_file.save(tmp_path) try: - # Run whisper.cpp to transcribe - command = [ - WHISPER_CPP_PATH, - "-m", MODEL_PATH, - "-f", filepath, - "--output-txt" # Output as text - ] - result = subprocess.run(command, capture_output=True, text=True) + # 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 + return jsonify({ + "error": "Transcription failed", + "details": result.stderr + }), 500 - # Read the transcription - output_txt = filepath + ".txt" - with open(output_txt, 'r') as f: + # Read output + with open(tmp_path + ".txt", 'r') as f: transcription = f.read() - # Clean up files - os.remove(filepath) - os.remove(output_txt) - 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=4002, debug=True) + app.run(host='0.0.0.0', port=5000)