Skip to content

Commit

Permalink
files modification
Browse files Browse the repository at this point in the history
  • Loading branch information
kmswlee committed Apr 1, 2020
1 parent 4949e60 commit e383b88
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 22 deletions.
12 changes: 9 additions & 3 deletions Dockerfile-api
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@ RUN apt-get -y update

RUN mkdir -p /root/.torch/models

RUN mkdir -p /data/models
RUN mkdir /data/

ADD . /data/

WORKDIR /data

RUN pip install -r requirements.txt

RUN pip install Flask
RUN pip install Flask

RUN pip install Pillow

RUN pip install scikit-image

RUN pip install requests

EXPOSE 5000
RUN wget -O /root/.torch/models/vgg16_bn-6c64b313.pth https://download.pytorch.org/models/vgg16_bn-6c64b313.pth

RUN wget -O /root/.torch/models/resnet34-333f7ec4.pth https://download.pytorch.org/models/resnet34-333f7ec4.pth

RUN wget -O /root/.torch/models/resnet101-5d3b4d8f.pth https://download.pytorch.org/models/resnet101-5d3b4d8f.pth

EXPOSE 80

ENTRYPOINT ["python3"]

Expand Down
134 changes: 115 additions & 19 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# import the necessary packages
import os
import sys
import requests
import ssl
from flask import Flask
from flask import Flask, redirect, url_for, request
from flask import request
from flask import jsonify
from flask import send_file
Expand All @@ -28,29 +27,26 @@

torch.backends.cudnn.benchmark=True


os.environ['CUDA_VISIBLE_DEVICES']='0'
#os.environ['CUDA_VISIBLE_DEVICES']='0'

app = Flask(__name__)



# define a predict function as an endpoint
@app.route("/process", methods=["POST"])
@app.route("/process-img", methods=["POST"])
def process_image():

input_path = generate_random_filename(upload_directory,"jpeg")
output_path = os.path.join(results_img_directory, os.path.basename(input_path))

try:
url = request.json["source_url"]
render_factor = int(request.json["render_factor"])
render_factor = 35#int(request.json["render_factor"])

download(url, input_path)

try:
image_colorizer.plot_transformed_image(path=input_path, figsize=(20,20),
render_factor=render_factor, display_render_factor=True, compare=False)
render_factor=render_factor, display_render_factor=True, compare=False)
except:
convertToJPG(input_path)
image_colorizer.plot_transformed_image(path=input_path, figsize=(20,20),
Expand All @@ -71,27 +67,127 @@ def process_image():
output_path
])

# @app.route("/process-video", methods=["POST"])
# def process_video():

# input_path = generate_random_filename(upload_directory,"mp4")
# output_path = os.path.join(results_video_directory, os.path.basename(input_path))

# try:
# url = request.json["source_url"]
# render_factor = int(request.json["render_factor"])

# video_path = video_colorizer.colorize_from_url(source_url=url, file_name=input_path, render_factor=render_factor)
# callback = send_file(output_path, mimetype='application/octet-stream')

# return callback, 200

# except:
# traceback.print_exc()
# return {'message': 'input error'}, 400

# finally:
# clean_all([
# input_path,
# output_path
# ])

@app.route('/health')
def health():
return "ok"

@app.route('/')
def main():
return """
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<div class=container>
<div class="jumbotron mt-3">
<h1>ainized-DeOldify</h1>
<A>Git hub repository : </A> <A href="https://bit.ly/39xp9db"> DeOldify </A> <br>
<A>API deployed on </A> <A href="http://bit.ly/390JkQr"> Ainize </A>
<hr class="my-4">
<h3>Image URL: <input id="source_url" placeholder="http://"> </h3><br>
<style>
#submit{
border-top-left-radius: 5px;
border-top-right-radius: 5px;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
</style>
<h3>RUN: <button type="submit" class="btn btn-primary btn-lg" id="submit">Submit</button></h3>
<div id="result">
<image id="resultImage">
</div>
<script>
const run = (retry_cnt=0, retry_sec=1) => {
if (retry_cnt < 3) {
retry_cnt += 1
retry_sec *= 2
} else {
throw Error('Retry Error');
}
url = "https://deoldify-api.kmswlee.endpoint.ainize.ai/process-img"
data = JSON.stringify({
source_url: document.getElementById('source_url').value,
render_factor: 35,
})
fetch(url, {method:'POST', headers: {'Content-Type': 'application/json'}, body: data})
.then(response => {
if (response.status === 200) {
return response;
} else if (response.status === 429) {
console.log(`retry ${retry_cnt}th after ${retry_sec}secs ...`);
setTimeout(
() => {
run(retry_cnt, retry_sec)
}, retry_sec * 1000
)
} else {
throw Error('Server Error - Debugging Please!');
}
})
.then(response => response.blob())
.then(blob => URL.createObjectURL(blob))
.then(imageURL => {
document.getElementById('result').style.display = 'block';
document.getElementById('resultImage').src = imageURL;
})
};
document.getElementById('submit').onclick = () => run()
</script>
</div>
</div>
"""

if __name__ == '__main__':
global upload_directory
global results_img_directory
global results_video_directory
global image_colorizer
global video_colorizer

upload_directory = '/data/upload/'
create_directory(upload_directory)
#create_directory(upload_directory)

results_img_directory = '/data/result_images/'
create_directory(results_img_directory)
#create_directory(results_img_directory)

results_video_directory = '/data/video/result/'
#create_directory(results_video_directory)

model_directory = '/data/models/'
create_directory(model_directory)

artistic_model_url = 'https://www.dropbox.com/s/zkehq1uwahhbc2o/ColorizeArtistic_gen.pth?dl=0'
get_model_bin(artistic_model_url, os.path.join(model_directory, 'ColorizeArtistic_gen.pth'))
#create_directory(model_directory)

#artistic_model_url = 'https://www.dropbox.com/s/zkehq1uwahhbc2o/ColorizeArtistic_gen.pth?dl=0'
#get_model_bin(artistic_model_url, os.path.join(model_directory, 'ColorizeArtistic_gen.pth'))

image_colorizer = get_image_colorizer(artistic=True)

port = 5000
host = '0.0.0.0'
#video_colorizer = get_video_colorizer()

app.run(host=host, port=port, threaded=False)
print('ready for')
app.run(host='0.0.0.0', port=80, threaded=False)

0 comments on commit e383b88

Please sign in to comment.