-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
145 lines (106 loc) · 4.44 KB
/
app.py
File metadata and controls
145 lines (106 loc) · 4.44 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import sys
import requests
import ssl
from flask import Flask
from flask import request
from flask import jsonify
from flask import send_file
from flask import redirect, url_for
from flask import render_template
from werkzeug import secure_filename
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
import json
import zipfile
from io import BytesIO
import time
from uuid import uuid4
from os import path
# import torch
# import fastai
# from fasterai.visualize import *
from pathlib import Path
from config import ALLOWED_EXTENSIONS, FROM_EMAIL, TO_EMAIL, SENDGRIP_API, HOST_IP, PORT
# torch.backends.cudnn.benchmark=True
# image_colorizer = get_image_colorizer(artistic=True)
# os.environ['CUDA_VISIBLE_DEVICES']='0'
app = Flask(__name__, template_folder="./static")
# app.config['SENDGRID_DEFAULT_FROM'] = 'customer@colorizer.com'
sg = SendGridAPIClient(SENDGRIP_API)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# define a predict function as an endpoint
@app.route("/process_image", methods=["POST", "GET"])
def process_image():
if request.method == 'POST':
memory_file = BytesIO()
# if request.files.get('photos') and request.files.get('photos').filename == '':
# return redirect(url_for('index'))
# if request.files.getlist('photos')[0].filename == '':
# return redirect(url_for('index'))
with zipfile.ZipFile(memory_file, 'w') as zf:
# print(request.files.getlist('photos'))
for f in request.files.getlist('photos'):
#f.save(os.path.join(app.config['UPLOAD_PATH'], f.filename))
print("hi")
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
data = zipfile.ZipInfo(filename)
data.date_time = time.localtime(time.time())[:6]
data.compress_type = zipfile.ZIP_DEFLATED
zf.writestr(data, f.read())
print(f)
memory_file.seek(0)
return send_file(memory_file, attachment_filename='result.zip', as_attachment=True)
# return 'Upload completed.'
return redirect(url_for('index'))
# return "Kuku"
@app.route("/contact_us", methods=["POST", "GET"])
def contact_us():
if request.method == 'POST':
if request.form['email'] and request.form['note']:
message = Mail(
from_email=FROM_EMAIL,
to_emails=TO_EMAIL,
subject='Customer of colorizer',
html_content='<strong>'+ request.form['name'] +'</strong> <br>' + request.form['note'])
response = sg.send(message)
# dataDict = json.loads(data)
# print(data['name'])
# message = Mail(
# from_email='customer@colorizer.com',
# to_emails='lazukav@gmail.com',
# subject='Sending with Twilio SendGrid is Fun',
# html_content='<strong>and easy to do anywhere, even with Python</strong>')
# response = sg.send(message)
return redirect(url_for('index'))
@app.route('/hello_world')
def hello_world():
# message = Mail(
# from_email='customer@colorizer.com',
# to_emails='lazukav@gmail.com',
# subject='Sending with Twilio SendGrid is Fun',
# html_content='<strong>and easy to do anywhere, even with Python</strong>')
# response = sg.send(message)
return 'Hello, World!'
@app.route('/')
def index():
return render_template('index.html')
# @app.route("/process_image_api", methods=["POST"])
# def process_image_api():
# source_url = request.json["source_url"]
# render_factor = int(request.json["render_factor"])
# upload_directory = 'upload'
# if not os.path.exists(upload_directory):
# os.mkdir(upload_directory)
# random_filename = str(uuid4()) + '.png'
# image_colorizer.plot_transformed_image_from_url(url=source_url, path=os.path.join(upload_directory, random_filename), figsize=(20,20),
# render_factor=render_factor, display_render_factor=True, compare=False)
# callback = send_file(os.path.join("result_images", random_filename), mimetype='image/jpeg')
# os.remove(os.path.join("result_images", random_filename))
# os.remove(os.path.join("upload", random_filename))
# return callback
if __name__ == '__main__':
app.run(host=HOST_IP, port=PORT, threaded=True, debug=True)