2022-07-05 00:02:33 +00:00
|
|
|
from min_dalle import MinDalle
|
2022-06-29 19:53:25 +00:00
|
|
|
import tempfile
|
2022-07-09 10:48:51 +00:00
|
|
|
import torch, torch.backends.cudnn
|
2022-07-05 00:02:33 +00:00
|
|
|
from typing import Iterator
|
2022-06-29 19:53:25 +00:00
|
|
|
from cog import BasePredictor, Path, Input
|
|
|
|
|
2022-07-09 10:48:51 +00:00
|
|
|
torch.backends.cudnn.deterministic = False
|
|
|
|
|
2022-06-29 19:53:25 +00:00
|
|
|
|
2022-07-05 09:47:35 +00:00
|
|
|
class ReplicatePredictor(BasePredictor):
|
2022-06-29 19:53:25 +00:00
|
|
|
def setup(self):
|
2022-07-05 11:07:29 +00:00
|
|
|
self.model = MinDalle(is_mega=True, is_reusable=True)
|
2022-06-29 19:53:25 +00:00
|
|
|
|
|
|
|
def predict(
|
|
|
|
self,
|
|
|
|
text: str = Input(
|
2022-07-06 02:14:19 +00:00
|
|
|
description='For long prompts, only the first 64 tokens will be used to generate the image.',
|
2022-07-04 11:28:44 +00:00
|
|
|
default='Dali painting of WALL·E'
|
2022-06-29 19:53:25 +00:00
|
|
|
),
|
2022-07-05 13:43:41 +00:00
|
|
|
intermediate_outputs: bool = Input(
|
|
|
|
description='Whether to show intermediate outputs while running. This adds less than a second to the run time.',
|
|
|
|
default=True
|
|
|
|
),
|
2022-07-02 14:05:16 +00:00
|
|
|
grid_size: int = Input(
|
2022-07-10 12:07:54 +00:00
|
|
|
description='Size of the image grid. 5x5 takes around 16 seconds, 8x8 takes around 36 seconds',
|
2022-07-02 14:32:17 +00:00
|
|
|
ge=1,
|
2022-07-07 12:21:20 +00:00
|
|
|
le=8,
|
2022-07-04 11:28:44 +00:00
|
|
|
default=4
|
2022-07-04 12:05:55 +00:00
|
|
|
),
|
2022-07-11 17:19:06 +00:00
|
|
|
log2_temperature: float = Input(
|
|
|
|
description='A higher temperature results in more variety.',
|
|
|
|
ge=-3,
|
|
|
|
le=3,
|
|
|
|
default=0
|
2022-07-05 01:30:27 +00:00
|
|
|
),
|
2022-07-05 00:02:33 +00:00
|
|
|
) -> Iterator[Path]:
|
2022-07-07 16:35:00 +00:00
|
|
|
try:
|
|
|
|
image_stream = self.model.generate_image_stream(
|
2022-07-11 17:19:06 +00:00
|
|
|
text = text,
|
|
|
|
seed = -1,
|
|
|
|
grid_size = grid_size,
|
|
|
|
log2_mid_count = 3 if intermediate_outputs else 0,
|
|
|
|
temperature = 2 ** log2_temperature,
|
|
|
|
supercondition_factor = 2 ** 4,
|
|
|
|
top_k = 2 ** 8,
|
|
|
|
is_verbose = True
|
2022-07-07 16:35:00 +00:00
|
|
|
)
|
2022-07-04 22:37:07 +00:00
|
|
|
|
2022-07-07 16:35:00 +00:00
|
|
|
iter = 0
|
|
|
|
path = Path(tempfile.mkdtemp())
|
|
|
|
for image in image_stream:
|
|
|
|
iter += 1
|
|
|
|
image_path = path / 'min-dalle-iter-{}.jpg'.format(iter)
|
|
|
|
image.save(str(image_path))
|
|
|
|
yield image_path
|
|
|
|
except:
|
|
|
|
print("An error occured, deleting model")
|
|
|
|
del self.model
|
2022-07-07 21:03:47 +00:00
|
|
|
torch.cuda.empty_cache()
|
2022-07-07 16:35:00 +00:00
|
|
|
self.setup()
|
|
|
|
raise Exception("There was an error, please try again")
|