You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.7 KiB
55 lines
1.7 KiB
2 years ago
|
from contextlib import suppress
|
||
2 years ago
|
from min_dalle import MinDalle
|
||
2 years ago
|
import tempfile
|
||
2 years ago
|
from typing import Iterator
|
||
2 years ago
|
from math import log2
|
||
2 years ago
|
from cog import BasePredictor, Path, Input
|
||
|
|
||
|
|
||
2 years ago
|
class ReplicatePredictor(BasePredictor):
|
||
2 years ago
|
def setup(self):
|
||
2 years ago
|
self.model = MinDalle(is_mega=True)
|
||
2 years ago
|
|
||
|
def predict(
|
||
|
self,
|
||
|
text: str = Input(
|
||
2 years ago
|
description='Text',
|
||
2 years ago
|
default='Dali painting of WALL·E'
|
||
2 years ago
|
),
|
||
2 years ago
|
grid_size: int = Input(
|
||
2 years ago
|
description='Size of the image grid',
|
||
2 years ago
|
ge=1,
|
||
2 years ago
|
le=4,
|
||
2 years ago
|
default=4
|
||
2 years ago
|
),
|
||
|
seed: int = Input(
|
||
|
description='Set the seed to a positive number for reproducible results',
|
||
|
default=-1
|
||
|
),
|
||
2 years ago
|
intermediate_image_count: int = Input(
|
||
2 years ago
|
description='Set the number of intermediate images to show while running',
|
||
2 years ago
|
choices=[1, 2, 4, 8, 16],
|
||
2 years ago
|
default=4
|
||
2 years ago
|
),
|
||
2 years ago
|
supercondition_factor: int = Input(
|
||
|
description='Lower results in a wider variety of images but less agreement with the text',
|
||
|
choices=[2, 4, 8, 16, 32, 64],
|
||
2 years ago
|
default=8
|
||
2 years ago
|
),
|
||
2 years ago
|
) -> Iterator[Path]:
|
||
|
image_stream = self.model.generate_image_stream(
|
||
2 years ago
|
text,
|
||
|
seed,
|
||
2 years ago
|
grid_size=grid_size,
|
||
2 years ago
|
log2_mid_count=log2(intermediate_image_count),
|
||
2 years ago
|
log2_supercondition_factor=log2(supercondition_factor),
|
||
2 years ago
|
is_verbose=True
|
||
2 years ago
|
)
|
||
|
|
||
2 years ago
|
iter = 0
|
||
|
path = Path(tempfile.mkdtemp())
|
||
2 years ago
|
for image in image_stream:
|
||
2 years ago
|
iter += 1
|
||
|
image_path = path / 'min-dalle-iter-{}.jpg'.format(iter)
|
||
|
image.save(str(image_path))
|
||
|
yield image_path
|