Development of an API for noise removal in audio
An API was developed in Python using the FastAPI framework, and Uvicorn was used to run the application. Poetry was used for dependency management.
FastAPI
FastAPI is a modern, fast web framework for building APIs with Python 3.7+ based on standard Python type hints. It is designed to be easy to use and developer-friendly while providing high-performance thanks to its asynchronous (async/await) capabilities.
FastAPI is especially suitable for creating microservices and APIs that require high performance and low latency. It includes features such as automatic data validation, built-in compatibility with OpenAPI and JSON Schema, automatic API documentation generation, and WebSocket APIs support.
Uvicornio
Uvicorn is an ultra-fast ASGI (Asynchronous Server Gateway Interface) server implementation designed to seamlessly work with modern Python web frameworks like FastAPI, Starlette, and Quart.
Installing fastapi along with uvicorn.
pip install fastapi uvicorn
Running an API application with Uvicorn
uvicorn main:app --reload
Poetry
With Poetry, you can manage project dependencies in a separate file (pyproject.toml) that can include detailed information such as version ranges and hashes, and then install all dependencies in a virtual environment with a single command. Poetry also has features such as dependency resolution, compatibility checking, and conflict resolution to help ensure that your project’s dependencies are compatible and up to date.
Crear un nuevo proyecto usando poesía como administrador de dependencias y FastAPI como marco
The steps to create the project are detailed
- Create the project
poetry new name_file
The command will create the project ¨name_file¨ that contains the correct structure that houses two important files: poetry.lock and pyproject.tolm
2. To activate the virtual environment and create a new shell run
poetry shell
3. Go to the .toml file and change the python version to:
python = ">=3.9,<3.11"
4. Run the command
poetry update
5. Add required dependencies.
poetry add fastapi uvicorn transformers[sentencepiece] torchaudio tensorflow speechbrain
6. Import these libraries
import os
import torchaudio
from fastapi import FastAPI,File,UploadFile
from fastapi.responses import FileResponse
from speechbrain.pretrained import SepformerSeparation as separator
import tempfile
import shutil
7. Create an instance
app = FastAPI()
8. Create a route and define the operator of the route operation decorator
@app.post("/sound/")
9. Use the UploadFile function to upload the audio file and call the models that split the audio
async def create_upload_file(file: UploadFile = File(...)):
model1 = separator.from_hparams(source="speechbrain/sepformer-wham", savedir='pretrained_models/sepformer-wham')
model2 = separator.from_hparams(source="speechbrain/sepformer-libri2mix", savedir='pretrained_models/sepformer-libri2mix')
10. Use temporary files with tempfile to save the loaded audio, save the sounds without background noise and finally create a zip folder which will house the result of the two pre-trained models.
audio_file_path=tempfile.NamedTemporaryFile(suffix=".wav",mode='w+b')
with audio_file_path as buffer:
shutil.copyfileobj(file.file, buffer)
est_sources = model2.separate_file(audio_file_path.name)
est_sources2 = model1.separate_file(audio_file_path.name)
nome=tempfile.TemporaryDirectory()
file_path01 = os.path.join(nome.name, "uno.wav")
file_path02 = os.path.join(nome.name, "dos.wav")
torchaudio.save(file_path01, est_sources[:, :, 0].detach().cpu(), 8000)
torchaudio.save(file_path02, est_sources2[:, :, 0].detach().cpu(), 8000)
filename = tempfile.NamedTemporaryFile()
format = "zip"
directory = nome.name
shutil.make_archive(filename.name, format, directory)
name_path=filename.name+'.zip'
11. With FileResponse the user will be able to download the audios as the result of the pre-trained models
return FileResponse(name_path,media_type="application/octet-stream",filename="without_noice.zip")
12. Run the server with:
uvicorn cambio001:app --reload
13. Create the documentation for the API with the address provided by the server followed by /docs
http://127.0.0.1:8000/docs
14. Run post and upload the audio file you want to remove background noise.
15. Download zip with the result of the two models
You can get the code for this example in this Github repository.
Do you need some help?
We are ready to listen to you. If you need some help creating the next big thing, you can contact our team on our website or at info@jrtec.io