This commit is contained in:
Andrey Gumirov
2022-05-01 00:57:58 +07:00
parent a968042f6e
commit e1bd7234ed
19 changed files with 329 additions and 39 deletions

14
nft_svc/Dockerfile Normal file
View File

@ -0,0 +1,14 @@
FROM python:3.9
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY . /app
EXPOSE 5000
ENTRYPOINT [ "flask" ]
CMD ["run", "--host=0.0.0.0", "--port=5000"]

61
nft_svc/app.py Normal file
View File

@ -0,0 +1,61 @@
import base64
import io
from PIL import Image
from flask import Flask, request, jsonify
import matplotlib.image as mpimg
import numpy as np
import numba
app = Flask(__name__)
@numba.njit
def optimized_mandelbrot(n_rows, n_columns, iterations, cx, cy):
x_cor = np.linspace(-2, 2, n_rows)
y_cor = np.linspace(-2, 2, n_columns)
output = np.zeros((n_rows,n_columns))
c = cx + 1j * cy
for i in range(n_rows):
for j in range(n_columns):
z = x_cor[i] + y_cor[j] * 1j
count = 0
for k in range(iterations):
z = (z*z) + c
count += 1
if np.abs(z) > 4:
break
output[i, j] = count
return output.T
def open_image_as_array(path):
return mpimg.imread(path)
def get_encoded_img(arr):
img = Image.fromarray(arr).convert("L")
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
encoded_img = base64.encodebytes(img_byte_arr.getvalue()).decode('ascii')
return encoded_img
@app.route('/getImage', methods=['GET'])
def get_image():
name = request.args.get('name')
fractal = optimized_mandelbrot(1000, 1000, np.random.randint(2, 251), np.random.uniform(-1, 1), np.random.uniform(-1, 1))
img = get_encoded_img(fractal)
return jsonify({
"code": 0,
"image": img,
"first_time": 1
})
def start_app():
app.run()
if __name__ == '__main__':
start_app()

View File

@ -0,0 +1,10 @@
# Gunicorn config variables
loglevel = "info"
errorlog = "-" # stderr
accesslog = "-" # stdout
worker_tmp_dir = "/dev/shm"
graceful_timeout = 120
timeout = 120
keepalive = 5
threads = 4
workers = 4

6
nft_svc/requirements.txt Normal file
View File

@ -0,0 +1,6 @@
matplotlib~=3.5.1
numpy~=1.21.6
numba~=0.55.1
Pillow~=9.1.0
Flask~=2.1.1
gunicorn