-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
131 lines (112 loc) · 3.92 KB
/
app.py
File metadata and controls
131 lines (112 loc) · 3.92 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
import time
import boto3
from botocore.exceptions import ClientError
import logging
from flask import Flask, request, send_file
import re
import os
from glob import glob
from io import BytesIO
from zipfile import ZipFile
from flask_cors import CORS, cross_origin
app = Flask(__name__)
s3 = boto3.client('s3')
cors = CORS(app)
@app.route('/', methods=['GET', 'POST'])
def handle_request():
print('hello world')
return {'hello': 'world'}
def if_bucket_exsist(file_id):
try:
response = s3.list_buckets()
for bucket in response['Buckets']:
if (bucket["Name"]==file_id):
return False
except ClientError as e:
logging.error(e)
return False
return True
def image_generated(text):
try:
os.chdir('stable-diffusion/')
os.system("python scripts/txt2img.py --prompt {} --plms --ckpt sd-v1-4.ckpt --skip_grid --n_samples 1".format(text))
return True
except:
return False
@app.route('/store', methods=['GET'])
def store_request(bucket_name=None, region=None):
try:
args = request.args.to_dict()
file_id = args['user_id']
text = args['caption']
bucket_name = re.sub(r'@.*', '', file_id)
region = 'us-west-1'
if if_bucket_exsist(bucket_name):
if region is None:
s3_client = boto3.client('s3')
s3_client.create_bucket(Bucket=bucket_name)
else:
s3_client = boto3.client('s3', region_name=region)
location = {'LocationConstraint': region}
s3_client.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration=location)
if image_generated(text):
#upload to the bucket
os.chdir('outputs/txt2img-samples/samples/')
for f in os.listdir():
file_name = f
object_name = file_name
s3.upload_file(file_name, bucket_name, object_name)
#package images in zip file
stream = BytesIO()
with ZipFile(stream, 'w') as zf:
for file in glob(os.path.join('*.png')):
zf.write(file, os.path.basename(file))
stream.seek(0)
#clean the folder after running the model
for f in os.listdir():
os.remove(os.path.join(f))
os.chdir('../../../../')
#return the zip file
return send_file(
stream,
as_attachment=True,
download_name='archive.zip'
)
except ClientError as e:
logging.error(e)
return {'working':'failed'}
return {'working':'ok'}
@app.route('/getRecent', methods=['GET'])
def getRecent():
try:
args = request.args.to_dict()
buck_n = args['user_id']
bucket_name = re.sub(r'@.*', '', buck_n)
s3 = boto3.resource('s3')
my_bucket = s3.Bucket(bucket_name)
os.chdir('send_images/')
# download file into current directory
for s3_object in my_bucket.objects.all():
# Need to split s3_object.key into path and file name, else it will give error file not found.
path, filename = os.path.split(s3_object.key)
my_bucket.download_file(s3_object.key, filename)
stream = BytesIO()
with ZipFile(stream, 'w') as zf:
for file in glob(os.path.join('*.png')):
zf.write(file, os.path.basename(file))
stream.seek(0)
#clean the folder after running the model
for f in os.listdir():
os.remove(os.path.join(f))
os.chdir('../')
return send_file(
stream,
as_attachment=True,
download_name='archive.zip'
)
except ClientError as e:
logging.error(e)
return {'working':'failed'}
if __name__ == '__main__':
app.run(host = '0.0.0.0')