2022-07-05 01:30:27 +00:00
|
|
|
from contextlib import suppress
|
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-05 00:02:33 +00:00
|
|
|
from typing import Iterator
|
2022-07-05 00:36:23 +00:00
|
|
|
from math import log2
|
2022-06-29 19:53:25 +00:00
|
|
|
from cog import BasePredictor, Path, Input
|
|
|
|
|
|
|
|
|
2022-07-05 09:47:35 +00:00
|
|
|
class ReplicatePredictor(BasePredictor):
|
2022-06-29 19:53:25 +00:00
|
|
|
def setup(self):
|
2022-07-01 23:44:24 +00:00
|
|
|
self.model = MinDalle(is_mega=True)
|
2022-06-29 19:53:25 +00:00
|
|
|
|
|
|
|
def predict(
|
|
|
|
self,
|
|
|
|
text: str = Input(
|
2022-07-02 14:46:56 +00:00
|
|
|
description='Text',
|
2022-07-04 11:28:44 +00:00
|
|
|
default='Dali painting of WALL·E'
|
2022-06-29 19:53:25 +00:00
|
|
|
),
|
2022-07-02 14:05:16 +00:00
|
|
|
grid_size: int = Input(
|
2022-07-04 11:28:44 +00:00
|
|
|
description='Size of the image grid',
|
2022-07-02 14:32:17 +00:00
|
|
|
ge=1,
|
2022-07-05 00:38:35 +00:00
|
|
|
le=4,
|
2022-07-04 11:28:44 +00:00
|
|
|
default=4
|
2022-07-04 12:05:55 +00:00
|
|
|
),
|
|
|
|
seed: int = Input(
|
2022-07-05 10:14:11 +00:00
|
|
|
description='A positive number will generate reproducible results',
|
2022-07-04 12:05:55 +00:00
|
|
|
default=-1
|
|
|
|
),
|
2022-07-05 09:55:10 +00:00
|
|
|
log2_intermediate_image_count: int = Input(
|
2022-07-05 10:14:11 +00:00
|
|
|
description='Number of images to show while running, each adds a slight delay',
|
2022-07-05 09:55:10 +00:00
|
|
|
ge=0,
|
|
|
|
le=4,
|
|
|
|
default=2
|
2022-07-04 22:37:07 +00:00
|
|
|
),
|
2022-07-05 09:55:10 +00:00
|
|
|
log2_supercondition_factor: int = Input(
|
2022-07-05 09:57:55 +00:00
|
|
|
description='Higher values result in better agreement with the text but a narrower variety of generated images',
|
2022-07-05 09:55:10 +00:00
|
|
|
ge=1,
|
|
|
|
le=6,
|
|
|
|
default=4
|
2022-07-05 01:30:27 +00:00
|
|
|
),
|
2022-07-05 00:02:33 +00:00
|
|
|
) -> Iterator[Path]:
|
|
|
|
image_stream = self.model.generate_image_stream(
|
2022-07-05 00:10:37 +00:00
|
|
|
text,
|
|
|
|
seed,
|
2022-07-04 22:37:07 +00:00
|
|
|
grid_size=grid_size,
|
2022-07-05 09:55:10 +00:00
|
|
|
log2_mid_count=log2_intermediate_image_count,
|
|
|
|
log2_supercondition_factor=log2_supercondition_factor,
|
2022-07-05 00:02:33 +00:00
|
|
|
is_verbose=True
|
2022-07-04 22:37:07 +00:00
|
|
|
)
|
|
|
|
|
2022-07-05 02:05:32 +00:00
|
|
|
iter = 0
|
|
|
|
path = Path(tempfile.mkdtemp())
|
2022-07-05 00:02:33 +00:00
|
|
|
for image in image_stream:
|
2022-07-05 02:05:32 +00:00
|
|
|
iter += 1
|
|
|
|
image_path = path / 'min-dalle-iter-{}.jpg'.format(iter)
|
|
|
|
image.save(str(image_path))
|
|
|
|
yield image_path
|