From d3e6b4974d2f4fd6b478a507f8a77e3b324b3145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20P=C4=99=C5=9Bko?= Date: Thu, 6 Sep 2018 21:27:40 +0200 Subject: [PATCH] Add minor config changes - Model caching - Production settings --- .gitignore | 1 + backend.env | 2 -- docker-compose-prod.yml | 52 ++++++++++++++++++++++++++++++++ docker-compose.yml | 5 +-- settings/settings.py | 19 ++++++------ style_transfer/style_transfer.py | 17 +++++++---- 6 files changed, 77 insertions(+), 19 deletions(-) delete mode 100644 backend.env create mode 100644 docker-compose-prod.yml diff --git a/.gitignore b/.gitignore index daa37ca..bd68633 100644 --- a/.gitignore +++ b/.gitignore @@ -124,3 +124,4 @@ pids *.pid *.seed *.pid.lock +secretkey.txt diff --git a/backend.env b/backend.env deleted file mode 100644 index 4296764..0000000 --- a/backend.env +++ /dev/null @@ -1,2 +0,0 @@ -# python -PYTHONUNBUFFERED=1 diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml new file mode 100644 index 0000000..16a36f3 --- /dev/null +++ b/docker-compose-prod.yml @@ -0,0 +1,52 @@ +version: '2.3' + +services: + db: + image: postgres:10 + env_file: + - db.env + networks: + - db_network + volumes: + - db_volume:/var/lib/postgresql/data + + web: + build: . + runtime: nvidia + volumes: + - .:/comixify + - static_volume:/comixify/static + - media_volume:/comixify/media + networks: + - nginx_network + - db_network + depends_on: + - db + environment: + - DEBUG=false + - PYTHONUNBUFFERED=1 + tty: true + + nginx: + image: nginx + ports: + - 80:80 + volumes: + - ./nginx.conf:/etc/nginx/conf.d/default.conf + - static_volume:/comixify/static + - media_volume:/comixify/media + depends_on: + - web + networks: + - nginx_network + +networks: + nginx_network: + driver: bridge + db_network: + driver: bridge + +volumes: + db_volume: + static_volume: + media_volume: \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 15f1a32..9d70339 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,8 +22,9 @@ services: - db_network depends_on: - db - env_file: - - backend.env + environment: + - DEBUG=true + - PYTHONUNBUFFERED=1 tty: true nginx: diff --git a/settings/settings.py b/settings/settings.py index af4f2ca..0adac0a 100644 --- a/settings/settings.py +++ b/settings/settings.py @@ -15,19 +15,19 @@ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'secret_123' +# Use a separate file for the secret key +with open(os.path.join(BASE_DIR, 'secretkey.txt')) as f: + SECRET_KEY = f.read().strip() # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.environ.get('DEBUG') == 'true' ALLOWED_HOSTS = ['35.241.250.34', 'comixify.ii.pw.edu.pl', 'localhost', '127.0.0.1'] - # Application definition INSTALLED_APPS = [ @@ -74,7 +74,6 @@ TEMPLATES = [ WSGI_APPLICATION = 'settings.wsgi.application' - # Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases @@ -90,7 +89,6 @@ DATABASES = { } } - # Password validation # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators @@ -108,7 +106,12 @@ AUTH_PASSWORD_VALIDATORS = [ 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] - +CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', + 'LOCATION': 'unique-snowflake', + } +} # Internationalization # https://docs.djangoproject.com/en/2.0/topics/i18n/ @@ -123,7 +126,6 @@ USE_L10N = True USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.0/howto/static-files/ @@ -133,7 +135,6 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') - PERMITTED_VIDEO_EXTENSIONS = ['mp4', 'avi'] MAX_FILE_SIZE = 50000000 NUMBERS_OF_FRAMES_TO_SHOW = 10 diff --git a/style_transfer/style_transfer.py b/style_transfer/style_transfer.py index 76f183d..a61a7f5 100644 --- a/style_transfer/style_transfer.py +++ b/style_transfer/style_transfer.py @@ -5,6 +5,7 @@ import numpy as np import torch import torchvision.transforms as transforms from django.conf import settings +from django.core.cache import cache from torch.autograd import Variable from CartoonGAN.network.Transformer import Transformer @@ -21,12 +22,16 @@ class StyleTransfer(): style = kwargs.get("style", "Hayao") resize = kwargs.get("resize", 450) - # TODO: We should load model to memory right after deployment, not on each request. - # load pretrained model - model = Transformer() - model.load_state_dict(torch.load(os.path.join("CartoonGAN/pretrained_model", style + "_net_G_float.pth"))) - model.eval() - model.cuda() if gpu else model.float() + model_cache_key = 'model_cache' + model = cache.get(model_cache_key) # get model from cache + + if model is None: + # load pretrained model + model = Transformer() + model.load_state_dict(torch.load(os.path.join("CartoonGAN/pretrained_model", style + "_net_G_float.pth"))) + model.eval() + model.cuda() if gpu else model.float() + cache.set(model_cache_key, model, None) # None is the timeout parameter. It means cache forever stylized_imgs = [] for img in frames: