This commit is contained in:
		
							
								
								
									
										31
									
								
								Dockerfile
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								Dockerfile
									
									
									
									
									
								
							@@ -1,28 +1,9 @@
 | 
			
		||||
# Use the official lightweight Python image.
 | 
			
		||||
# https://hub.docker.com/_/python
 | 
			
		||||
FROM python:3.8-slim
 | 
			
		||||
FROM python:3
 | 
			
		||||
 | 
			
		||||
# Copy local code to the container image.
 | 
			
		||||
ENV APP_HOME /app
 | 
			
		||||
ENV PIP_NO_CACHE_DIR=true
 | 
			
		||||
ADD classification_service requirements.txt /classification_service/
 | 
			
		||||
WORKDIR /classification_service/
 | 
			
		||||
 | 
			
		||||
# Set timezone
 | 
			
		||||
ENV TZ Europe/Budapest
 | 
			
		||||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
 | 
			
		||||
RUN pip3 install -r requirements.txt && pip3 install gunicorn
 | 
			
		||||
 | 
			
		||||
WORKDIR $APP_HOME
 | 
			
		||||
 | 
			
		||||
ARG RELEASE_ID
 | 
			
		||||
ENV RELEASE_ID ${RELEASE_ID:-""}
 | 
			
		||||
 | 
			
		||||
#Copy requirements to install
 | 
			
		||||
COPY requirements.txt ./
 | 
			
		||||
 | 
			
		||||
# Install production dependencies.
 | 
			
		||||
RUN pip install -r requirements.txt
 | 
			
		||||
 | 
			
		||||
COPY ./src ./src
 | 
			
		||||
 | 
			
		||||
EXPOSE 8080
 | 
			
		||||
 | 
			
		||||
ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:8080", "--workers", "1", "--threads", "8", "app:app"]
 | 
			
		||||
EXPOSE 8000
 | 
			
		||||
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										42
									
								
								classification_service/app.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								classification_service/app.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
import os
 | 
			
		||||
from flask import Flask
 | 
			
		||||
import sentry_sdk
 | 
			
		||||
from sentry_sdk.integrations.flask import FlaskIntegration
 | 
			
		||||
 | 
			
		||||
# import stuff
 | 
			
		||||
 | 
			
		||||
from utils import register_all_error_handlers
 | 
			
		||||
 | 
			
		||||
# import views
 | 
			
		||||
from views import ClassifyView
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Setup sentry
 | 
			
		||||
SENTRY_DSN = os.environ.get("SENTRY_DSN")
 | 
			
		||||
if SENTRY_DSN:
 | 
			
		||||
    sentry_sdk.init(
 | 
			
		||||
        dsn=SENTRY_DSN,
 | 
			
		||||
        integrations=[FlaskIntegration()],
 | 
			
		||||
        send_default_pii=True,
 | 
			
		||||
        release=os.environ.get('RELEASE_ID', 'test'),
 | 
			
		||||
        environment=os.environ.get('RELEASEMODE', 'dev')
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
# create flask app
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
 | 
			
		||||
# important stuff
 | 
			
		||||
app.secret_key = os.environ.get('SECRET_KEY', os.urandom(12))
 | 
			
		||||
 | 
			
		||||
# register error handlers
 | 
			
		||||
register_all_error_handlers(app)
 | 
			
		||||
 | 
			
		||||
# register views
 | 
			
		||||
for view in [ClassifyView]:
 | 
			
		||||
    view.register(app, trailing_slash=False)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# start debugging if needed
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    app.run(debug=True)
 | 
			
		||||
							
								
								
									
										2
									
								
								classification_service/schemas/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								classification_service/schemas/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1
									
								
								classification_service/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								classification_service/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
import os
 | 
			
		||||
							
								
								
									
										3
									
								
								classification_service/utils/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								classification_service/utils/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
from .require_decorators import json_required
 | 
			
		||||
from .error_handlers import register_all_error_handlers
 | 
			
		||||
							
								
								
									
										18
									
								
								classification_service/utils/error_handlers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								classification_service/utils/error_handlers.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_standard_error_handler(code: int):
 | 
			
		||||
    def error_handler(err):
 | 
			
		||||
        return {"msg": str(err)}, code
 | 
			
		||||
 | 
			
		||||
    return error_handler
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# function to register all handlers
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def register_all_error_handlers(app):
 | 
			
		||||
    error_codes_to_override = [404, 403, 401, 405, 400, 409, 422]
 | 
			
		||||
 | 
			
		||||
    for code in error_codes_to_override:
 | 
			
		||||
        app.register_error_handler(code, get_standard_error_handler(code))
 | 
			
		||||
							
								
								
									
										16
									
								
								classification_service/utils/require_decorators.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								classification_service/utils/require_decorators.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
from flask import request, current_app, abort
 | 
			
		||||
 | 
			
		||||
from functools import wraps
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def json_required(f):
 | 
			
		||||
    @wraps(f)
 | 
			
		||||
    def call(*args, **kwargs):
 | 
			
		||||
 | 
			
		||||
        if request.is_json:
 | 
			
		||||
            return f(*args, **kwargs)
 | 
			
		||||
        else:
 | 
			
		||||
            abort(400, "JSON required")
 | 
			
		||||
 | 
			
		||||
    return call
 | 
			
		||||
							
								
								
									
										2
									
								
								classification_service/views/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								classification_service/views/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
from .classify_view import ClassifyView
 | 
			
		||||
							
								
								
									
										11
									
								
								classification_service/views/classify_view.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								classification_service/views/classify_view.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
from flask import request, jsonify
 | 
			
		||||
from flask_classful import FlaskView
 | 
			
		||||
from utils import json_required
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ClassifyView(FlaskView):
 | 
			
		||||
 | 
			
		||||
    @json_required
 | 
			
		||||
    def post(self):
 | 
			
		||||
        return jsonify(request.json)
 | 
			
		||||
@@ -1,2 +1,14 @@
 | 
			
		||||
sentry_sdk
 | 
			
		||||
gunicorn
 | 
			
		||||
requests
 | 
			
		||||
blinker
 | 
			
		||||
Flask
 | 
			
		||||
marshmallow
 | 
			
		||||
Flask-Classful
 | 
			
		||||
sentry-sdk
 | 
			
		||||
 | 
			
		||||
pyAudioanalysis
 | 
			
		||||
numpy
 | 
			
		||||
eyed3
 | 
			
		||||
pydub
 | 
			
		||||
scipy
 | 
			
		||||
matplotlib
 | 
			
		||||
sklearn
 | 
			
		||||
		Reference in New Issue
	
	Block a user