Skip to content

Commit

Permalink
add jinja and a working nginx conf for port 80
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidrains committed Jun 3, 2023
1 parent 0dbd64b commit 9595d66
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include TPDNE_utils *.tmpl
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ from TPDNE_utils import sample_image_and_save_repeatedly

def generate_image():
import numpy as np
return np.random.randn(3, 1024, 1024)
return np.random.randn(1024, 1024, 3)

# saves a new sampled image every 250ms as out/sampled.jpeg

Expand All @@ -41,7 +41,9 @@ sample_image_and_save_repeatedly(generate_image, 'out/sampled')

## Todo

- [ ] take care of an nginx template that can be generated from a command-line
- [x] take care of an nginx template
- [ ] handle ssl in nginx
- [ ] auto-handle various types of tensor outputs. auto-detect channel dimension and move it to last

## Citations

Expand Down
14 changes: 14 additions & 0 deletions TPDNE_utils/nginx.conf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server {
listen 80 default_server;
listen [::]:80 default_server;

root {{ root }};

index {{ index }};

server_name {{ server_name }};

location / {
try_files $uri $uri/ =404;
}
}
49 changes: 47 additions & 2 deletions TPDNE_utils/tpdne.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@
from beartype import beartype
from beartype.typing import Callable, Optional

# templating

from jinja2 import Environment, FileSystemLoader

current_dir = Path(__file__).parents[0]
environment = Environment(loader = FileSystemLoader(str(current_dir)))
nginx_template = environment.get_template('nginx.conf.tmpl')

# helper functions

def exists(val):
return val is not None

# main function

@beartype
def sample_image_and_save_repeatedly(
fn: Callable[..., np.ndarray], # function that returns a ndarray of shape (3, <width>, <height>)
Expand All @@ -24,25 +36,58 @@ def sample_image_and_save_repeatedly(
quality = 99,
resize_image_to: Optional[int] = None,
generate_favicon: bool = True,
favicon_size: int = 32
favicon_size: int = 32,
generate_nginx_conf: bool = True,
symbolic_link_nginx_conf: bool = True,
nginx_sites_available_path: str = '/etc/nginx/sites-available',
nginx_conf_filename = 'default',
domain_name = '_'
):
assert 0 < quality <= 100
assert favicon_size in {16, 32}
assert image_format in {'jpeg', 'png', 'webp'}

tmp_dir = Path(tmp_dir)
output_path = Path(output_path)

assert output_path.suffix == '', 'output path suffix will be automatically determined by `image_format` keyword arg'

output_path = output_path.with_suffix(f'.{image_format}')

call_every_seconds = call_every_ms / 1000

assert tmp_dir.is_dir()
output_path.parents[0].mkdir(parents = True, exist_ok = True)
root = output_path.parents[0]
root.mkdir(parents = True, exist_ok = True)

tmp_image_index = 0

# linking nginx

if generate_nginx_conf:
nginx_sites_path = Path(nginx_sites_available_path)
nginx_sites_conf_path = nginx_sites_path / nginx_conf_filename

assert nginx_sites_path.is_dir()

nginx_conf_text = nginx_template.render(
root = str(root.resolve()),
index = output_path.name,
server_name = domain_name
)

tmp_conf_path = Path(tmp_dir / 'nginx.server.conf')
tmp_conf_path.write_text(nginx_conf_text)

print(f'nginx server conf generated at {str(tmp_conf_path)}')

if symbolic_link_nginx_conf:
os.system(f'ln -nfs {str(tmp_conf_path)} {nginx_sites_conf_path}')

print(f'nginx conf linked to {nginx_sites_conf_path}\nrun `systemctl reload nginx` for it to be in effect')

# invoke `fn` in a while loop

while True:
start = time()
image_tensor = fn()
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
setup(
name = 'TPDNE-utils',
packages = find_packages(exclude=[]),
version = '0.0.5',
version = '0.0.6',
license='MIT',
description = 'TPDNE',
include_package_data = True,
author = 'Phil Wang',
author_email = 'lucidrains@gmail.com',
long_description_content_type = 'text/markdown',
url = 'https://github.com/lucidrains/TPDNE',
keywords = [],
install_requires = [
'beartype',
'jinja2',
'numpy',
'pillow'
],
Expand Down

0 comments on commit 9595d66

Please sign in to comment.