forked from pixray/pixray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcogrun.py
executable file
·166 lines (152 loc) · 7.41 KB
/
cogrun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from cog import BasePredictor, Input, Path
from typing import Iterator
import torch
import yaml
import pathlib
import os
import yaml
from util import get_single_rgb
# https://stackoverflow.com/a/6587648/1010653
import tempfile, shutil
def create_temporary_copy(src_path):
_, tf_suffix = os.path.splitext(src_path)
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, f"tempfile{tf_suffix}")
shutil.copy2(src_path, temp_path)
return temp_path
class BasePixrayPredictor(BasePredictor):
def setup(self):
print("---> BasePixrayPredictor Setup")
os.environ['TORCH_HOME'] = 'models/'
def predict(self,
settings: str = Input(description="Default settings to use"),
**kwargs) -> Iterator[Path]:
# workaround for import issue when deploying
import pixray
"""Run a single prediction on the model"""
print("---> BasePixrayPredictor Predict")
os.environ['TORCH_HOME'] = 'models/'
settings_file = f"cogs/{settings}.yaml"
with open(settings_file, 'r') as stream:
try:
base_settings = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print("YAML ERROR", exc)
sys.exit(1)
pixray.reset_settings()
pixray.add_settings(**base_settings)
pixray.add_settings(**kwargs)
pixray.add_settings(skip_args=True)
settings = pixray.apply_settings()
pixray.do_init(settings)
run_complete = False
while run_complete == False:
run_complete = pixray.do_run(settings, return_display=True)
output_file = os.path.join(settings.outdir, settings.output)
temp_copy = create_temporary_copy(output_file)
yield Path(os.path.realpath(temp_copy))
class PixrayVqgan(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text prompt", default="rainbow mountain"),
quality: str = Input(description="better is slower", default="normal", choices=["draft", "normal", "better", "best"]),
aspect: str = Input(description="wide vs square", default="widescreen", choices=["widescreen", "square"]),
# num_cuts: int = Input(description="number of cuts", default=24, ge:4, le:96),
# batches: int = Input(description="number of batches", default=1, ge:1, le:32),
**kwargs
) -> Iterator[Path]:
yield from super().predict(settings="pixray_vqgan", **kwargs)
class PixrayPixel(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text promps", default="Beirut Skyline. #pixelart"),
aspect: str = Input(description="wide vs square", default="widescreen", choices=["widescreen", "square"]),
drawer: str = Input(description="render engine", default="pixel", choices=["pixel", "vqgan", "line_sketch", "clipdraw"]),
**kwargs
) -> Iterator[Path]:
yield from super().predict(settings="pixray_pixel", **kwargs)
class Text2Image(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text prompt", default="Cairo skyline at sunset."),
drawer: str = Input(description="render engine", default="vqgan", choices=["pixel", "vqgan", "vdiff", "fft", "fast_pixel", "line_sketch", "clipdraw"]),
settings: str = Input(description="extra settings in `name: value` format. reference: https://dazhizhong.gitbook.io/pixray-docs/docs/primary-settings", default='\n')
) -> Iterator[Path]:
ydict = yaml.safe_load(settings)
if ydict == None:
# no settings
ydict = {}
yield from super().predict(settings="text2image", prompts=prompts, drawer=drawer, **ydict)
class Text2Pixel(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text prompt", default="Manhattan skyline at sunset. #pixelart"),
aspect: str = Input(description="wide or narrow", default="widescreen", choices=["widescreen", "square", "portrait"]),
pixel_scale: float = Input(description="bigger pixels", default=1.0, ge=0.5, le=2.0),
**kwargs
) -> Iterator[Path]:
yield from super().predict(settings="text2pixel", **kwargs)
class PixrayRaw(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text prompt", default="Manhattan skyline at sunset. #pixelart"),
settings: str = Input(description="yaml settings", default="\n")
) -> Iterator[Path]:
ydict = yaml.safe_load(settings)
if ydict == None:
# no settings
ydict = {}
yield from super().predict(settings="pixrayraw", prompts=prompts, **ydict)
class PixrayApi(BasePixrayPredictor):
def predict(self,
settings: str = Input(description="yaml settings", default="\n")
) -> Iterator[Path]:
ydict = yaml.safe_load(settings)
if ydict == None:
# no settings
ydict = {}
yield from super().predict(settings="pixrayapi", **ydict)
class Tiler(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text prompt", default=""),
pixelart: bool = Input(description="pixelart style?", default=False),
mirror: bool = Input(description="shifted pattern?", default=False),
settings: str = Input(description="yaml settings", default="\n")
) -> Iterator[Path]:
ydict = yaml.safe_load(settings)
if ydict == None:
# no settings
ydict = {}
if pixelart:
if mirror:
settings = "tiler_pixel_shift"
else:
settings = "tiler_pixel"
yield from super().predict(prompts=f"{prompts} #pixelart", settings=settings, **ydict)
else:
if mirror:
settings = "tiler_fft_shift"
else:
settings = "tiler_fft"
yield from super().predict(prompts=prompts, settings=settings, **ydict)
class PixrayVdiff(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text prompt", default="Manhattan skyline at sunset. #artstation 🌇"),
settings: str = Input(description="extra settings in `name: value` format. reference: https://dazhizhong.gitbook.io/pixray-docs/docs/primary-settings", default='\n')
) -> Iterator[Path]:
ydict = yaml.safe_load(settings)
if ydict == None:
# no settings
ydict = {}
yield from super().predict(settings="pixray_vdiff", prompts=prompts, **ydict)
class EightBidG(BasePixrayPredictor):
def predict(self,
prompts: str = Input(description="text prompt", default=""),
palette: str = Input(description="colors to use", default="full color", choices=["full color", "web safe", "grayscale"]),
border: str = Input(description="border color", default="none", choices=["white", "black", "grey", "none"]),
) -> Iterator[Path]:
ydict = {}
if border == "none":
ydict.update({"custom_loss":"smoothness:0.25"})
else:
ydict.update({"custom_loss":"edge,smoothness:0.25", "edge_thickness":2, "edge_color":get_single_rgb(border)})
if palette == "grayscale":
ydict.update({"filters":"lookup", "palette":'black->white\\256'})
elif palette == "web safe":
ydict.update({"filters":"lookup", "palette":'https://www.pagetutor.com/common/net216pics/net216.gif'})
yield from super().predict(prompts=prompts, settings="8bidg", **ydict)