From 4eca03a945c15f6831b7f16c96046ced56da9d18 Mon Sep 17 00:00:00 2001 From: Kar l5 Date: Sun, 15 Jun 2025 00:39:51 +0530 Subject: [PATCH] v2 --- Dockerfile | 41 ++++++++++++++++++++++++----- app/main.py | 68 ++++++++++++++++++++++++++++++++++++++---------- requirements.txt | 7 +++-- 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2d799d7..4f95b12 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ - build-essential \ + python3 \ + python3-pip \ + git \ + cmake \ + make \ + g++ \ ffmpeg \ curl \ - && rm -rf /var/lib/apt/lists/* + && rm -rf /var/lib/apt/lists/* +# Set working directory 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 . RUN pip install --no-cache-dir -r requirements.txt -# Copy app and model -COPY app ./app +# Copy API code +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 -CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "4002"] + +# Run the API +CMD ["python3", "app.py"] diff --git a/app/main.py b/app/main.py index 5914a79..3959378 100644 --- a/app/main.py +++ b/app/main.py @@ -1,19 +1,59 @@ -from fastapi import FastAPI, File, UploadFile -import shutil +from flask import Flask, request, jsonify 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 -whisper = Whisper.from_pretrained("./app/model/ggml-tiny.en.bin") +# Configure upload folder (adjust as needed) +UPLOAD_FOLDER = "./uploads" +os.makedirs(UPLOAD_FOLDER, exist_ok=True) +app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER -@app.post("/transcribe") -async def transcribe_audio(audio: UploadFile = File(...)): - temp_file = f"temp_{audio.filename}" - with open(temp_file, "wb") as buffer: - shutil.copyfileobj(audio.file, buffer) +# 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 - result = whisper.transcribe(temp_file) - os.remove(temp_file) - return {"text": result} +@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 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) diff --git a/requirements.txt b/requirements.txt index 04fab71..b7f8b2e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -fastapi -uvicorn -python-multipart -whispercpp +flask==2.3.2 +python-dotenv==1.0.0 +werkzeug==2.3.6