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-12 15:20:37 +00:00
|
|
|
self.model = MinDalle(
|
|
|
|
is_mega=True,
|
|
|
|
is_reusable=True,
|
|
|
|
dtype=torch.float32
|
|
|
|
)
|
2022-06-29 19:53:25 +00:00
|
|
|
|
|
|
|
def predict(
|
|
|
|
self,
|
2022-07-12 13:17:18 +00:00
|
|
|
text: str = Input(default='Dali painting of WALL·E'),
|
2022-07-12 16:04:56 +00:00
|
|
|
output_png: bool = Input(default=False),
|
2022-07-12 13:17:18 +00:00
|
|
|
intermediate_outputs: bool = Input(default=True),
|
|
|
|
grid_size: int = Input(ge=1, le=9, default=5),
|
2022-07-12 16:04:56 +00:00
|
|
|
log2_temperature: float = Input(ge=-3, le=3, default=2),
|
|
|
|
log2_top_k: int = Input(ge=0, le=14, default=4),
|
2022-07-12 15:20:37 +00:00
|
|
|
log2_supercondition_factor: float = Input(ge=2, le=6, default=4)
|
2022-07-05 00:02:33 +00:00
|
|
|
) -> Iterator[Path]:
|
2022-07-12 15:20:37 +00:00
|
|
|
log2_mid_count = 3 if intermediate_outputs else 0
|
|
|
|
image_stream = self.model.generate_image_stream(
|
|
|
|
text = text,
|
|
|
|
seed = -1,
|
|
|
|
grid_size = grid_size,
|
|
|
|
log2_mid_count = log2_mid_count,
|
|
|
|
temperature = 2 ** log2_temperature,
|
|
|
|
supercondition_factor = 2 ** log2_supercondition_factor,
|
|
|
|
top_k = 2 ** log2_top_k,
|
|
|
|
is_verbose = True
|
|
|
|
)
|
2022-07-04 22:37:07 +00:00
|
|
|
|
2022-07-12 15:20:37 +00:00
|
|
|
i = 0
|
|
|
|
path = Path(tempfile.mkdtemp())
|
|
|
|
for image in image_stream:
|
|
|
|
i += 1
|
2022-07-12 16:04:56 +00:00
|
|
|
ext = 'png' if i == 2 ** log2_mid_count and output_png else 'jpg'
|
2022-07-12 15:20:37 +00:00
|
|
|
image_path = path / 'min-dalle-iter-{}.{}'.format(i, ext)
|
|
|
|
image.save(str(image_path))
|
|
|
|
yield image_path
|