-
Notifications
You must be signed in to change notification settings - Fork 2
/
creator.py
158 lines (133 loc) · 4.12 KB
/
creator.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
from threading import Lock
import eventlet
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import fire
# focused imports
from big_sleep import Imagine
import torch
# default options, overridable from the command line
GPU_IDX = 0
default_http_address = '127.0.0.1'
default_http_port = 5000
default_big_sleep_hyper_params = {
'text': "a bp",
'lr': .07,
'image_size': 512,
'gradient_accumulate_every': 4,
'epochs': 20,
'iterations': 1050,
'save_every': 50,
'overwrite': False,
'save_progress': False,
'bilinear': False,
'open_folder': True,
'seed': 0,
'torch_deterministic': False,
}
# eventlet patching to improve coroutine compatibility
eventlet.monkey_patch()
def initial_test():
text = 'puppy with purple eyes'
imagine = Imagine(
text=text,
lr=.07,
save_every=10,
open_folder=True,
epochs=1,
iterations=10,
bilinear=False,
image_size=256,
save_progress=True,
seed=False,
torch_deterministic=False,
)
imagine()
imagine.reset()
del imagine.model
del imagine.optimizer
del imagine
class State:
def __init__(self):
self.busy = False
self.connections_count = 0
def set_busy(self, busy):
self.busy = busy
emit('@status:server', self.message_server_status())
def update_connections(self, adj):
self.connections_count += adj
emit('@status:server', self.message_server_status())
def message_server_status(self):
gpu_properties = torch.cuda.get_device_properties(GPU_IDX)
# COPY ui type definition (ServerStatus)
return {
'busy': self.busy,
'clientsCount': self.connections_count,
'gpuNumber': GPU_IDX,
'gpuName': gpu_properties.name,
'gpuTotal': gpu_properties.total_memory,
}
# globals
state = State()
# Main()
def run_app(http_host=default_http_address, http_port=default_http_port, skip_test=False):
# configure Flask for serving
print(f'# Starting HTTP endpoint on {http_host}:{http_port}')
app = Flask(__name__, template_folder='static-site')
app.logger.setLevel(20)
socket_io = SocketIO(app, async_mode='eventlet', logger=True, engineio_logger=True, cors_allowed_origins='*')
print(f' - socket.io in async mode: {socket_io.async_mode}, options: {socket_io.server_options}')
print()
# test out app functionality at start
if not skip_test:
initial_test()
@app.route('/')
def index():
return render_template('index.html')
@socket_io.on('connect')
def client_connected():
state.update_connections(+1)
print(f'Client {state.connections_count} connected')
@socket_io.on('disconnect')
def client_disconnected():
state.update_connections(-1)
print(f'Client disconnected ({state.connections_count} left)')
@socket_io.on('@admin:clear_busy')
def admin_clear_busy():
print('admin_clear_busy: ADMIN override')
state.set_busy(False)
mutex = Lock()
@socket_io.on('@dream:new')
def dream_new(json):
mutex.acquire()
print('a')
state.set_busy(True)
print('b')
input_text = json['text']
print('c')
imagine = Imagine(
text=input_text,
lr=.07,
open_folder=False,
epochs=1,
iterations=10,
bilinear=False,
image_size=256,
save_every=10,
save_progress=True,
seed=False,
torch_deterministic=False,
)
print('d')
imagine()
print('e')
state.set_busy(False)
print('f')
mutex.release()
print('g')
# start sever
socket_io.run(app, host=http_host, port=http_port)
# Main
assert torch.cuda.is_available(), 'CUDA must be available in order to use Deep Daze'
if __name__ == '__main__':
fire.Fire(run_app)