Kar 2025-06-15 21:47:28 +05:30
parent 2bd8b22cfa
commit 9562d16078
2 changed files with 38 additions and 38 deletions

View File

@ -1,7 +1,6 @@
# Base image
FROM ubuntu:22.04 FROM ubuntu:22.04
# Install system dependencies # Install dependencies
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
python3 \ python3 \
python3-pip \ python3-pip \
@ -16,7 +15,7 @@ RUN apt-get update && apt-get install -y \
# Set working directory # Set working directory
WORKDIR /app WORKDIR /app
# Clone whisper.cpp and compile # Clone and build whisper.cpp
RUN git clone https://github.com/ggerganov/whisper.cpp.git && \ RUN git clone https://github.com/ggerganov/whisper.cpp.git && \
cd whisper.cpp && \ cd whisper.cpp && \
make make
@ -35,12 +34,13 @@ COPY app.py .
# Create uploads directory # Create uploads directory
RUN mkdir -p uploads RUN mkdir -p uploads
# Environment variables (adjust paths if needed) # Correct paths (critical fix!)
ENV WHISPER_CPP_PATH="/app/whisper.cpp/main" ENV WHISPER_CPP_PATH="/app/whisper.cpp/main"
ENV MODEL_PATH="/app/whisper.cpp/models/ggml-small.en.bin" ENV MODEL_PATH="/app/whisper.cpp/models/ggml-small.en.bin"
# Expose Flask port # Verify the binary exists (debugging step)
EXPOSE 4002 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"] CMD ["python3", "app.py"]

62
app.py
View File

@ -1,18 +1,12 @@
from flask import Flask, request, jsonify
import os import os
from flask import Flask, request, jsonify
import subprocess import subprocess
from werkzeug.utils import secure_filename
app = Flask(__name__) app = Flask(__name__)
# Configure upload folder (adjust as needed) # Use environment variables (set in Dockerfile)
UPLOAD_FOLDER = "./uploads" WHISPER_CPP_PATH = os.getenv("WHISPER_CPP_PATH", "/app/whisper.cpp/main") # Absolute path
os.makedirs(UPLOAD_FOLDER, exist_ok=True) MODEL_PATH = os.getenv("MODEL_PATH", "/app/whisper.cpp/models/ggml-small.en.bin") # Absolute path
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
@app.route('/transcribe', methods=['POST']) @app.route('/transcribe', methods=['POST'])
def transcribe_audio(): def transcribe_audio():
@ -23,37 +17,43 @@ def transcribe_audio():
if audio_file.filename == '': if audio_file.filename == '':
return jsonify({"error": "Empty filename"}), 400 return jsonify({"error": "Empty filename"}), 400
# Save the uploaded file # Save to a temporary file
filename = secure_filename(audio_file.filename) tmp_path = "/tmp/audio_upload.wav"
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) audio_file.save(tmp_path)
audio_file.save(filepath)
try: try:
# Run whisper.cpp to transcribe # Run whisper.cpp (absolute paths)
command = [ result = subprocess.run(
WHISPER_CPP_PATH, [
"-m", MODEL_PATH, WHISPER_CPP_PATH,
"-f", filepath, "-m", MODEL_PATH,
"--output-txt" # Output as text "-f", tmp_path,
] "--output-txt"
result = subprocess.run(command, capture_output=True, text=True) ],
capture_output=True,
text=True
)
if result.returncode != 0: 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 # Read output
output_txt = filepath + ".txt" with open(tmp_path + ".txt", 'r') as f:
with open(output_txt, 'r') as f:
transcription = f.read() transcription = f.read()
# Clean up files
os.remove(filepath)
os.remove(output_txt)
return jsonify({"text": transcription.strip()}) return jsonify({"text": transcription.strip()})
except Exception as e: except Exception as e:
return jsonify({"error": str(e)}), 500 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__': if __name__ == '__main__':
app.run(host='0.0.0.0', port=4002, debug=True) app.run(host='0.0.0.0', port=5000)