master
Kar 2025-06-15 00:39:51 +05:30
parent dc5c455f0e
commit 4eca03a945
3 changed files with 91 additions and 25 deletions

View File

@ -1,19 +1,46 @@
FROM python:3.10-slim # Base image
FROM ubuntu:22.04
# Install dependencies # Install system dependencies
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
build-essential \ python3 \
python3-pip \
git \
cmake \
make \
g++ \
ffmpeg \ ffmpeg \
curl \ curl \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app WORKDIR /app
# Clone whisper.cpp and compile
RUN git clone https://github.com/ggerganov/whisper.cpp.git && \
cd whisper.cpp && \
make
# Download the small.en model
RUN cd whisper.cpp/models && \
curl -L "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.en.bin" --output ggml-small.en.bin
# Install Python dependencies
COPY requirements.txt . COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# Copy app and model # Copy API code
COPY app ./app COPY app.py .
# Create uploads directory
RUN mkdir -p uploads
# Environment variables (adjust paths if needed)
ENV WHISPER_CPP_PATH="/app/whisper.cpp/main"
ENV MODEL_PATH="/app/whisper.cpp/models/ggml-small.en.bin"
# Expose Flask port
EXPOSE 4002 EXPOSE 4002
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "4002"]
# Run the API
CMD ["python3", "app.py"]

View File

@ -1,19 +1,59 @@
from fastapi import FastAPI, File, UploadFile from flask import Flask, request, jsonify
import shutil
import os import os
from whispercpp import Whisper import subprocess
from werkzeug.utils import secure_filename
app = FastAPI() app = Flask(__name__)
# Load the local model without Hugging Face token # Configure upload folder (adjust as needed)
whisper = Whisper.from_pretrained("./app/model/ggml-tiny.en.bin") UPLOAD_FOLDER = "./uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.post("/transcribe") # Path to whisper.cpp executable & model (update paths as needed)
async def transcribe_audio(audio: UploadFile = File(...)): WHISPER_CPP_PATH = "./whisper.cpp/main" # Path to compiled whisper.cpp binary
temp_file = f"temp_{audio.filename}" MODEL_PATH = "./whisper.cpp/models/ggml-small.en.bin" # Path to model
with open(temp_file, "wb") as buffer:
shutil.copyfileobj(audio.file, buffer)
result = whisper.transcribe(temp_file) @app.route('/transcribe', methods=['POST'])
os.remove(temp_file) def transcribe_audio():
return {"text": result} 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 the uploaded file
filename = secure_filename(audio_file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
audio_file.save(filepath)
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)
if result.returncode != 0:
return jsonify({"error": "Transcription failed", "details": result.stderr}), 500
# Read the transcription
output_txt = filepath + ".txt"
with open(output_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
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4002, debug=True)

View File

@ -1,4 +1,3 @@
fastapi flask==2.3.2
uvicorn python-dotenv==1.0.0
python-multipart werkzeug==2.3.6
whispercpp