Add minor config changes

- Model caching
- Production settings
This commit is contained in:
Maciej Pęśko 2018-09-06 21:27:40 +02:00
parent 4a16d2f278
commit d3e6b4974d
6 changed files with 77 additions and 19 deletions

1
.gitignore vendored
View file

@ -124,3 +124,4 @@ pids
*.pid *.pid
*.seed *.seed
*.pid.lock *.pid.lock
secretkey.txt

View file

@ -1,2 +0,0 @@
# python
PYTHONUNBUFFERED=1

52
docker-compose-prod.yml Normal file
View file

@ -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:

View file

@ -22,8 +22,9 @@ services:
- db_network - db_network
depends_on: depends_on:
- db - db
env_file: environment:
- backend.env - DEBUG=true
- PYTHONUNBUFFERED=1
tty: true tty: true
nginx: nginx:

View file

@ -15,19 +15,19 @@ import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # 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! # 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'] ALLOWED_HOSTS = ['35.241.250.34', 'comixify.ii.pw.edu.pl', 'localhost', '127.0.0.1']
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
@ -74,7 +74,6 @@ TEMPLATES = [
WSGI_APPLICATION = 'settings.wsgi.application' WSGI_APPLICATION = 'settings.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
@ -90,7 +89,6 @@ DATABASES = {
} }
} }
# Password validation # Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators # 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', 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
}, },
] ]
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/ # https://docs.djangoproject.com/en/2.0/topics/i18n/
@ -123,7 +126,6 @@ USE_L10N = True
USE_TZ = True USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/ # 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_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
PERMITTED_VIDEO_EXTENSIONS = ['mp4', 'avi'] PERMITTED_VIDEO_EXTENSIONS = ['mp4', 'avi']
MAX_FILE_SIZE = 50000000 MAX_FILE_SIZE = 50000000
NUMBERS_OF_FRAMES_TO_SHOW = 10 NUMBERS_OF_FRAMES_TO_SHOW = 10

View file

@ -5,6 +5,7 @@ import numpy as np
import torch import torch
import torchvision.transforms as transforms import torchvision.transforms as transforms
from django.conf import settings from django.conf import settings
from django.core.cache import cache
from torch.autograd import Variable from torch.autograd import Variable
from CartoonGAN.network.Transformer import Transformer from CartoonGAN.network.Transformer import Transformer
@ -21,12 +22,16 @@ class StyleTransfer():
style = kwargs.get("style", "Hayao") style = kwargs.get("style", "Hayao")
resize = kwargs.get("resize", 450) resize = kwargs.get("resize", 450)
# TODO: We should load model to memory right after deployment, not on each request. model_cache_key = 'model_cache'
# load pretrained model model = cache.get(model_cache_key) # get model from cache
model = Transformer()
model.load_state_dict(torch.load(os.path.join("CartoonGAN/pretrained_model", style + "_net_G_float.pth"))) if model is None:
model.eval() # load pretrained model
model.cuda() if gpu else model.float() 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 = [] stylized_imgs = []
for img in frames: for img in frames: