From cda34bae2c5c2eede430f76422831ef42d6c689b Mon Sep 17 00:00:00 2001 From: rezakarbasi Date: Wed, 22 May 2024 23:50:47 +0330 Subject: [PATCH 1/2] feat: dockerfiles are made and tested on the device --- dockerize/Dockerfile | 26 +++++++++++++++++++ dockerize/app.py | 51 ++++++++++++++++++++++++++++++++++++++ dockerize/requirements.txt | 4 +++ 3 files changed, 81 insertions(+) create mode 100644 dockerize/Dockerfile create mode 100644 dockerize/app.py create mode 100644 dockerize/requirements.txt diff --git a/dockerize/Dockerfile b/dockerize/Dockerfile new file mode 100644 index 0000000..45e0db5 --- /dev/null +++ b/dockerize/Dockerfile @@ -0,0 +1,26 @@ +# Use an official Python runtime as a parent image +FROM python:3.8-slim + +# Set environment variable +ENV APP_PORT="5000" + +# Set the working directory in the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Update the package list and install ffmpeg +RUN apt-get update && \ +apt-get install -y ffmpeg && \ +rm -rf /var/lib/apt/lists/* + +# Install any needed packages specified in requirements.txt +RUN pip install --no-cache-dir -r /app/dockerize/requirements.txt + +# Make port 1555 available to the world outside this container +EXPOSE 5000 + +# Run app.py when the container launches +CMD ["python", "-m", "dockerize.app"] +# CMD ["python"] \ No newline at end of file diff --git a/dockerize/app.py b/dockerize/app.py new file mode 100644 index 0000000..31285bb --- /dev/null +++ b/dockerize/app.py @@ -0,0 +1,51 @@ +from flask import Flask, request, send_file, jsonify +from pydub import AudioSegment +import os + +# install wrapper's package +from rnnoise_wrapper import RNNoise + +app = Flask(__name__) +denoiser = RNNoise("./rnnoise_wrapper/libs/librnnoise_default.so.0.4.1") + +def read_audio(file): + # Parse the file format from the filename extension + format = file.filename.rsplit('.', 1)[1].lower() + + # Use pydub to read various audio formats + audio = AudioSegment.from_file(file, format=format) + + # Normalize the audio to the rnnoise's input format + audio = audio.set_frame_rate(48000) + audio = audio.set_sample_width(2) + audio = audio.set_channels(1) + + return audio + +@app.route('/denoise', methods=['POST']) +def denoise(): + global denoiser + + if 'file' not in request.files: + return jsonify({"status": "File not found"}), 400 + + file = request.files['file'] + + if file.filename == '': + return jsonify({"status": "File name invalid"}), 404 + + + if file: + try: + audio = read_audio(file) + denoised_audio = denoiser.filter(audio) + denoiser.write_wav("denoised.wav", denoised_audio) + return send_file("denoised.wav", as_attachment=True) + except: + return jsonify({"status": "An exception happend!"}), 500 + + return jsonify({"status": "File invalid"}), 403 + +if __name__ == '__main__': + port = int(os.getenv("APP_PORT", 5000)) + app.run(debug=True, host='0.0.0.0', port=port) diff --git a/dockerize/requirements.txt b/dockerize/requirements.txt new file mode 100644 index 0000000..2804cfe --- /dev/null +++ b/dockerize/requirements.txt @@ -0,0 +1,4 @@ +numpy>=1.16 +pydub>=0.23.1 +Flask +. \ No newline at end of file From 604731e34596c1c91b5dab5e0b8258dfb941e94c Mon Sep 17 00:00:00 2001 From: rezakarbasi Date: Thu, 23 May 2024 00:00:29 +0330 Subject: [PATCH 2/2] doc: docker section for readme added issue #13 addressed --- README.md | 16 ++++++++++++++++ dockerize/app.py | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d5858e..5bd25be 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,22 @@ rnnoise_wrapper -i input.wav -o output.wav - `input.wav` - имя исходной .wav аудиозаписи - `output.wav` - имя .wav аудиофайла, в который будет сохранена аудиозапись после шумоподавления +## Docker +The docker image can get an audio file, do the process on it and export the file. + +To build the dockerfile stand in the root of repo then: + +`docker build -f dockerize/Dockerfile -t rnnoisewrapper .` + +To run the container: + +`docker run -d -p 5000:5000 rnnoisewrapper:latest` + +and to test the api using curl you can insert this command: + +`curl -X POST -F "file=@/path/to/loca/sound/file.wav" 127.0.0.1:5000/process --output processed_audio.wav` + + ## Обучение Инструкция по обучению RNNoise на своих данных находится в [`TRAINING.md`](https://github.com/Desklop/RNNoise_Wrapper/tree/master/TRAINING.md). diff --git a/dockerize/app.py b/dockerize/app.py index 31285bb..356447d 100644 --- a/dockerize/app.py +++ b/dockerize/app.py @@ -22,8 +22,8 @@ def read_audio(file): return audio -@app.route('/denoise', methods=['POST']) -def denoise(): +@app.route('/process', methods=['POST']) +def process(): global denoiser if 'file' not in request.files: