Commit ef8d024f authored by Jan Rudolf's avatar Jan Rudolf

ADD flask application endpoint, base classifier

parent 7378ccd8
......@@ -4,3 +4,6 @@ data
genres.tar.gz
*/.ipynb_checkpoints
input
upload
preprocessed_*
audio_classification.egg-info
clean:
rm -rf venv
clean-cache:
rm -rf preprocessed_* data
setup-dev: clean
clean-build:
rm -rf venv audio_classification.egg-info
clean-all: clean-build clean-cache
setup-dev: clean-build
python3 -m venv venv
./venv/bin/pip3 install -U -r requirements.txt
./venv/bin/pip3 install -e .
venv-activate:
. ./venv/bin/activate
......@@ -21,3 +27,7 @@ export-libs:
ntb:
. ./venv/bin/activate && \
jupyter notebook
server:
. ./venv/bin/activate && \
python3 audio_classification/run/app.py
from abc import ABC, abstractmethod
from audio_classification.preprocess import preprocess_sample
class BaseClassifier(ABC):
def __init__(
self,
**kwargs # arguments for preprocessing
):
self.preprocess_kwargs = kwargs
@abstractmethod
def classify(self, df):
pass
def classify_file(self, path):
return self.classify(preprocess_sample(path, **self.preprocess_kwargs))
class MockClassifier(BaseClassifier):
def classify(self, df):
return 'jazz'
......@@ -14,7 +14,7 @@ DUMB_PREFIX = '/Users/sness/mirex2008/genres/'
def preprocess_sample(path, **kwargs):
audio_file, sr = librosa.load(path)
y, _ = librosa.effects.trim(audio_file)
return librosa.feature.mfcc(y=y, **kwargs)
return librosa.feature.mfcc(y=y, sr=sr, **kwargs)
def preprocess(n_mfcc):
......
import os
from random import random
from flask import Flask, request, flash, jsonify
from werkzeug.utils import redirect, secure_filename
from audio_classification.classifier.base_classifier import MockClassifier
UPLOAD_FOLDER = 'upload'
app = Flask(__name__)
classifiers = {
'mock': MockClassifier()
}
if not os.path.exists(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
@app.route('/classify', methods=['POST'])
def classify():
if request.json is None:
flash('No json presented')
return redirect(request.url)
classifier_id = request.json['classifier_id']
if classifier_id not in classifiers:
flash('Unknown classifier id')
return redirect(request.url)
classifier = classifiers[classifier_id]
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
filename = secure_filename(file.filename)
folder = os.path.join(UPLOAD_FOLDER, f'c{random()}')
filepath = os.path.join(folder, filename)
try:
os.mkdir(folder)
file.save(filepath)
prediction = classifier.classify_file(filepath)
jsonify(prediction)
except Exception as e:
flash(e)
return redirect(request.url)
finally:
if os.path.exists(filepath):
os.remove(filepath)
if os.path.exists(folder):
os.rmdir(folder)
if __name__ == '__main__':
app.run(port=4666)
......@@ -9,15 +9,18 @@ bleach==3.2.1
certifi==2020.6.20
cffi==1.14.3
chardet==3.0.4
click==7.1.2
cycler==0.10.0
decorator==4.4.2
defusedxml==0.6.0
entrypoints==0.3
Flask==1.1.2
idna==2.10
importlib-metadata==2.0.0
ipykernel==5.3.4
ipython==7.19.0
ipython-genutils==0.2.0
itsdangerous==1.1.0
jedi==0.17.2
Jinja2==2.11.2
joblib==0.17.0
......@@ -71,4 +74,5 @@ traitlets==5.0.5
urllib3==1.25.11
wcwidth==0.2.5
webencodings==0.5.1
Werkzeug==1.0.1
zipp==3.4.0
from setuptools import setup, find_packages
setup(name='audio_classification', version='0', packages=find_packages())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment