master
parent
301b58b644
commit
be2296e442
66
Dockerfile
66
Dockerfile
|
@ -1,51 +1,55 @@
|
||||||
FROM ubuntu:22.04
|
FROM ubuntu:22.04 AS build
|
||||||
|
|
||||||
# Install system dependencies
|
# Install build dependencies
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
python3 \
|
|
||||||
python3-pip \
|
|
||||||
git \
|
git \
|
||||||
cmake \
|
cmake \
|
||||||
make \
|
make \
|
||||||
g++ \
|
g++ \
|
||||||
|
python3 \
|
||||||
|
python3-pip \
|
||||||
ffmpeg \
|
ffmpeg \
|
||||||
curl \
|
libavcodec-dev \
|
||||||
|
libavformat-dev \
|
||||||
|
libavutil-dev \
|
||||||
|
libswresample-dev \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Set working directory
|
# Clone whisper.cpp
|
||||||
RUN mkdir -p /app && chmod 777 /app
|
RUN git clone https://github.com/ggerganov/whisper.cpp.git /whisper.cpp
|
||||||
WORKDIR /app
|
WORKDIR /whisper.cpp
|
||||||
|
|
||||||
# Clone whisper.cpp (shallow clone)
|
# Build whisper.cpp
|
||||||
RUN git clone --depth 1 https://github.com/ggerganov/whisper.cpp.git
|
RUN make
|
||||||
RUN chmod -R 777 whisper.cpp
|
|
||||||
|
|
||||||
# Build whisper.cpp properly
|
# Download a model (base.en in this example)
|
||||||
RUN cd whisper.cpp && \
|
RUN ./models/download-ggml-model.sh base.en
|
||||||
pwd && \
|
|
||||||
make
|
|
||||||
|
|
||||||
# Verify the binary was built
|
FROM ubuntu:22.04 AS runtime
|
||||||
RUN ls -lh /app/whisper.cpp
|
|
||||||
|
|
||||||
# Download the small.en model
|
# Install runtime dependencies
|
||||||
RUN mkdir -p /app/whisper.cpp/models && \
|
RUN apt-get update && apt-get install -y \
|
||||||
cd /app/whisper.cpp/models && \
|
python3 \
|
||||||
curl -L "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.en.bin" --output ggml-small.en.bin
|
python3-pip \
|
||||||
|
ffmpeg \
|
||||||
|
libavcodec-dev \
|
||||||
|
libavformat-dev \
|
||||||
|
libavutil-dev \
|
||||||
|
libswresample-dev \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Copy built whisper.cpp and model
|
||||||
|
COPY --from=build /whisper.cpp /whisper.cpp
|
||||||
|
WORKDIR /whisper.cpp
|
||||||
|
|
||||||
# Install Python dependencies
|
# Install Python dependencies
|
||||||
COPY requirements.txt .
|
RUN pip3 install flask flask-cors
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
|
||||||
|
|
||||||
# Copy API code
|
# Copy API server script
|
||||||
COPY app.py .
|
COPY app.py .
|
||||||
|
|
||||||
# Create uploads directory
|
# Expose port
|
||||||
RUN mkdir -p uploads
|
EXPOSE 5000
|
||||||
|
|
||||||
# Set environment variables
|
# Run the server
|
||||||
ENV WHISPER_CPP_PATH="/app/whisper.cpp"
|
|
||||||
ENV MODEL_PATH="/app/whisper.cpp/models/ggml-small.en.bin"
|
|
||||||
|
|
||||||
EXPOSE 4002
|
|
||||||
CMD ["python3", "app.py"]
|
CMD ["python3", "app.py"]
|
||||||
|
|
60
app.py
60
app.py
|
@ -1,59 +1,61 @@
|
||||||
import os
|
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
|
from flask_cors import CORS
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
CORS(app)
|
||||||
|
|
||||||
# Use environment variables (set in Dockerfile)
|
WHISPER_CPP_PATH = "/whisper.cpp/main"
|
||||||
WHISPER_CPP_PATH = os.getenv("WHISPER_CPP_PATH", "/app/whisper.cpp/main") # Absolute path
|
MODEL_PATH = "/whisper.cpp/models/ggml-base.en.bin"
|
||||||
MODEL_PATH = os.getenv("MODEL_PATH", "/app/whisper.cpp/models/ggml-small.en.bin") # Absolute path
|
|
||||||
|
|
||||||
@app.route('/stt', methods=['POST'])
|
@app.route('/transcribe', methods=['POST'])
|
||||||
def transcribe_audio():
|
def transcribe_audio():
|
||||||
if 'audio' not in request.files:
|
if 'audio' not in request.files:
|
||||||
return jsonify({"error": "No audio file provided"}), 400
|
return jsonify({"error": "No audio file provided"}), 400
|
||||||
|
|
||||||
audio_file = request.files['audio']
|
audio_file = request.files['audio']
|
||||||
if audio_file.filename == '':
|
|
||||||
return jsonify({"error": "Empty filename"}), 400
|
|
||||||
|
|
||||||
# Save to a temporary file
|
# Save the uploaded file to a temporary location
|
||||||
tmp_path = "/tmp/audio_upload.wav"
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_audio:
|
||||||
audio_file.save(tmp_path)
|
audio_file.save(tmp_audio.name)
|
||||||
|
tmp_path = tmp_audio.name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Run whisper.cpp (absolute paths)
|
# Run whisper.cpp to transcribe the audio
|
||||||
result = subprocess.run(
|
cmd = [
|
||||||
[
|
|
||||||
WHISPER_CPP_PATH,
|
WHISPER_CPP_PATH,
|
||||||
"-m", MODEL_PATH,
|
"-m", MODEL_PATH,
|
||||||
"-f", tmp_path,
|
"-f", tmp_path,
|
||||||
"--output-txt"
|
"--output-txt",
|
||||||
],
|
"--output-file", os.path.join(tempfile.gettempdir(), "output")
|
||||||
capture_output=True,
|
]
|
||||||
text=True
|
|
||||||
)
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||||
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"error": "Transcription failed",
|
"error": "Transcription failed",
|
||||||
"details": result.stderr
|
"stderr": result.stderr
|
||||||
}), 500
|
}), 500
|
||||||
|
|
||||||
# Read output
|
# Read the output file
|
||||||
with open(tmp_path + ".txt", 'r') as f:
|
output_file = os.path.join(tempfile.gettempdir(), "output.txt")
|
||||||
|
with open(output_file, 'r') as f:
|
||||||
transcription = f.read()
|
transcription = f.read()
|
||||||
|
|
||||||
return jsonify({"text": transcription.strip()})
|
return jsonify({
|
||||||
|
"transcription": transcription.strip()
|
||||||
|
})
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
return jsonify({"error": str(e)}), 500
|
|
||||||
finally:
|
finally:
|
||||||
# Clean up
|
# Clean up temporary files
|
||||||
if os.path.exists(tmp_path):
|
if os.path.exists(tmp_path):
|
||||||
os.remove(tmp_path)
|
os.unlink(tmp_path)
|
||||||
if os.path.exists(tmp_path + ".txt"):
|
output_file = os.path.join(tempfile.gettempdir(), "output.txt")
|
||||||
os.remove(tmp_path + ".txt")
|
if os.path.exists(output_file):
|
||||||
|
os.unlink(output_file)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=4002)
|
app.run(host='0.0.0.0', port=5000)
|
||||||
|
|
Loading…
Reference in New Issue