2022-06-29 19:53:25 +00:00
|
|
|
import tempfile
|
|
|
|
from cog import BasePredictor, Path, Input
|
|
|
|
|
2022-07-01 23:44:24 +00:00
|
|
|
from min_dalle import MinDalle
|
2022-07-04 22:37:07 +00:00
|
|
|
from PIL import Image
|
2022-06-29 19:53:25 +00:00
|
|
|
|
|
|
|
class Predictor(BasePredictor):
|
|
|
|
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-04 14:28:47 +00:00
|
|
|
le=5,
|
2022-07-04 11:28:44 +00:00
|
|
|
default=4
|
2022-07-04 12:05:55 +00:00
|
|
|
),
|
|
|
|
seed: int = Input(
|
|
|
|
description='Set the seed to a positive number for reproducible results',
|
|
|
|
default=-1
|
|
|
|
),
|
2022-07-04 22:37:07 +00:00
|
|
|
log2_intermediate_image_count: int = Input(
|
|
|
|
description='Set the log2 number of intermediate images to show',
|
|
|
|
ge=0,
|
|
|
|
le=4,
|
|
|
|
default=3
|
|
|
|
),
|
2022-06-29 19:53:25 +00:00
|
|
|
) -> Path:
|
|
|
|
|
2022-07-04 22:41:00 +00:00
|
|
|
def handle_intermediate_image(i: int, image: Image.Image):
|
2022-07-04 22:37:07 +00:00
|
|
|
if i + 1 == 16: return
|
|
|
|
out_path = Path(tempfile.mkdtemp()) / 'output.jpg'
|
|
|
|
image.save(str(out_path))
|
|
|
|
|
|
|
|
image = self.model.generate_image(
|
|
|
|
text,
|
|
|
|
seed,
|
|
|
|
grid_size=grid_size,
|
|
|
|
log2_mid_count=log2_intermediate_image_count,
|
|
|
|
handle_intermediate_image=handle_intermediate_image
|
|
|
|
)
|
|
|
|
|
|
|
|
return handle_intermediate_image(-1, image)
|