forked from CompVis/latent-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 2
/
minion.py
151 lines (121 loc) · 3.92 KB
/
minion.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
import os
import logging
from threading import Thread
from typing import *
import torchvision
from ai.services.pubsub import PubSub, RedisPubSub, REDIS_HOST, REDIS_PORT
from ai.services.queue import Queue, RedisQueue
from ai.job_manager import JobManager
from ai.services.storage.s3 import S3, AWS_BUCKET_NAME, AWS_REGION_NAME
from ai.services.db.firestore import Firestore, FIREBASE_CREDENTIALS, PROJECT_ID
from generation_utils import LatentDiffusionModel
PUBSUB_HOST = os.getenv("PUBSUB_HOST", REDIS_HOST)
PUBSUB_PORT = os.getenv("PUBSUB_PORT", REDIS_PORT)
QUEUE_HOST = os.getenv("QUEUE_HOST", REDIS_HOST)
QUEUE_PORT = os.getenv("PUBSUB_PORT", REDIS_PORT)
BUCKET_NAME = os.getenv("AWS_BUCKET_NAME", AWS_BUCKET_NAME)
REGION_NAME = os.getenv("AWS_REGION_NAME", AWS_REGION_NAME)
class Minion:
def __init__(
self,
channel_name: str,
pubsub: PubSub,
queue: Queue,
):
self.channel_name = channel_name
self.pubsub = pubsub
self.queue = queue
self.job_cb = None
def register_job(
self,
cb: Callable,
):
self.job_cb = cb
def start(self, ):
thread = Thread(
target=self.pubsub.subscribe,
kwargs=dict(
channel_name=self.channel_name,
cb=self.job_cb,
),
)
thread.start()
return thread
def run(self, ):
# self.pubsub.subscribe(
# channel_name=self.channel_name,
# cb=self.job_cb,
# )
print("minion running")
print(self.channel_name)
self.queue.listen(
channel_name=self.channel_name,
cb=self.job_cb,
)
return
if __name__ == "__main__":
latent_diffusion_model = LatentDiffusionModel()
def message_cb(job_data_list, ):
prompt_list = []
generation_id_list = []
user_id_list = []
result_list = []
for job_data_dict in job_data_list:
num_generations = job_data_dict.get("numGenerations")
if num_generations is not None:
prompt_list += job_data_dict.get("prompt")
generation_id_list += job_data_dict.get("generationId")
user_id_list += job_data_dict.get("userId")
else:
prompt_list.append(job_data_dict.get("prompt"))
generation_id_list.append(job_data_dict.get("generationId"))
user_id_list.append(job_data_dict.get("userId"))
result_tensor_imgs = latent_diffusion_model.generate_from_prompt(
prompt_list, )[0]
for result_idx, result_tensor in enumerate(result_tensor_imgs):
img_pil = torchvision.transforms.ToPILImage()(result_tensor, )
result_list.append(
{
"prompt": prompt_list[result_idx],
"generationId": generation_id_list[result_idx],
"userId": user_id_list[result_idx],
"img": img_pil,
}, )
return result_list
logger = logging.getLogger("minion")
channel_name = "generations"
pubsub = RedisPubSub(
host=PUBSUB_HOST,
port=REDIS_PORT,
logger=logger,
)
queue = RedisQueue(
host=QUEUE_HOST,
port=QUEUE_PORT,
logger=logger,
)
storage = S3(
bucket_name=BUCKET_NAME,
region_name=REGION_NAME,
)
db = Firestore(
firebase_credentials_path=FIREBASE_CREDENTIALS,
project_id=PROJECT_ID,
)
minion = Minion(
channel_name=channel_name,
pubsub=pubsub,
queue=queue,
)
job_manager = JobManager(
pubsub=pubsub,
queue=queue,
storage=storage,
db=db,
logger=logger,
channel_name=channel_name,
)
job_manager.set_message_cb(message_cb, )
minion.register_job(job_manager.process_message, )
print("Minion running!")
minion.run()