-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
83 lines (66 loc) · 2.47 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask import Flask, request
from flask_celery import make_celery
from decouple import config
from deeptransformimpl import trigger_deeptransform
from flask_json import FlaskJSON, JsonError, json_response
import requests
# Create and configure an instance of the Flask application.
app = Flask(__name__)
FlaskJSON(app)
# Fetch environment configurations
DEEPAI_KEY = config('DEEPAI_KEY')
app.config['CELERY_BROKER_URL'] = config('CELERY_BROKER_URL')
app.config['CELERY_RESULT_BACKEND'] = config('CELERY_RESULT_BACKEND')
# Initialize celery app with flask app
celery = make_celery(app)
@app.route('/fasttransform', methods=['POST'])
def fast_neural_style_transform():
"""
For fast neural style transformation of image we are using API
exposed by deepai.org
"""
data = request.get_json(force=True)
try:
key = data['request_key']
style_url = data['style_url']
content_url = data['content_url']
except (KeyError):
raise JsonError(description='Key Error: Key missing in the request')
# Invoke deepai.org neural-style API
deepai_resp = requests.post(
"https://api.deepai.org/api/neural-style",
data={
'style': style_url,
'content': content_url,
},
headers={'api-key': DEEPAI_KEY}
)
deepai_resp_json = deepai_resp.json()
try:
tranformed_image_url = deepai_resp_json['output_url']
except (KeyError):
err_msg = 'Trans Error: Neural style transformation failed'
raise JsonError(status_=500, request_key=key, description=err_msg)
return json_response(request_key=key,
output_url=tranformed_image_url)
@app.route('/deeptransform', methods=['POST'])
def deep_neural_style_transform():
"""
For deep neural style transformation of image we are using API
developed internally by lambda school students
"""
data = request.get_json(force=True)
try:
key = data['request_key']
style_url = data['style_url']
content_url = data['content_url']
except (KeyError):
raise JsonError(description='Key Error: Key missing in the request')
deeptransform_async.delay(key, style_url, content_url)
return json_response(request_key=key,
description='Deep transformation is in progress')
@celery.task(name='app.deeptransform_async')
def deeptransform_async(key, style_url, content_url):
trigger_deeptransform(key, style_url, content_url)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)