Duplicate from kuprel/min-dalle
Browse filesCo-authored-by: Brett Kuprel <[email protected]>
- .gitattributes +27 -0
- .gitignore +19 -0
- README.md +16 -0
- app.py +203 -0
- examples/astronaut-horse.jpg +0 -0
- examples/astronaut-mars.jpg +0 -0
- examples/cleopatra-iphone.jpg +0 -0
- examples/dali-walle.jpg +0 -0
- examples/elmo-riot.jpg +0 -0
- examples/funeral-whole-foods.jpg +0 -0
- examples/godzilla-trial.jpg +0 -0
- examples/gollum.jpg +0 -0
- examples/jesus-talent.jpg +0 -0
- examples/lofi-nuclear-war.jpg +0 -0
- examples/marx-slimed.jpg +0 -0
- examples/moai-statue.jpg +0 -0
- examples/nuclear-broccoli.jpg +0 -0
- examples/rusty-iron-man.jpg +0 -0
- examples/scientists-rhyme.jpg +0 -0
- examples/singularity.jpg +0 -0
- requirements.txt +5 -0
.gitattributes
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
**/__pycache__/
|
| 2 |
+
**/.cache/
|
| 3 |
+
**/*.pkl
|
| 4 |
+
**/.DS*
|
| 5 |
+
**/*.pt
|
| 6 |
+
*.mlpackage
|
| 7 |
+
**/*.ckpt
|
| 8 |
+
.vscode
|
| 9 |
+
**/.ipynb_checkpoints
|
| 10 |
+
**/generated
|
| 11 |
+
**/pretrained
|
| 12 |
+
**/*.msgpack
|
| 13 |
+
*.egg-info/
|
| 14 |
+
*.egg
|
| 15 |
+
dist
|
| 16 |
+
build
|
| 17 |
+
README
|
| 18 |
+
.cog
|
| 19 |
+
cog
|
README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: min(DALL·E)
|
| 3 |
+
metaTitle: min(DALL·E)
|
| 4 |
+
emoji: ⚡️ 🥑 ⚡️
|
| 5 |
+
colorFrom: yellow
|
| 6 |
+
colorTo: green
|
| 7 |
+
sdk: gradio
|
| 8 |
+
sdk_version: 3.3.1
|
| 9 |
+
app_file: app.py
|
| 10 |
+
pinned: true
|
| 11 |
+
license: mit
|
| 12 |
+
python_version: 3.10.5
|
| 13 |
+
models:
|
| 14 |
+
- kuprel/min-dalle
|
| 15 |
+
duplicated_from: kuprel/min-dalle
|
| 16 |
+
---
|
app.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio
|
| 2 |
+
import subprocess
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch, torch.backends.cudnn, torch.backends.cuda
|
| 5 |
+
from min_dalle import MinDalle
|
| 6 |
+
from emoji import demojize
|
| 7 |
+
import string
|
| 8 |
+
|
| 9 |
+
def filename_from_text(text: str) -> str:
|
| 10 |
+
text = demojize(text, delimiters=['', ''])
|
| 11 |
+
text = text.lower().encode('ascii', errors='ignore').decode()
|
| 12 |
+
allowed_chars = string.ascii_lowercase + ' '
|
| 13 |
+
text = ''.join(i for i in text.lower() if i in allowed_chars)
|
| 14 |
+
text = text[:64]
|
| 15 |
+
text = '-'.join(text.strip().split())
|
| 16 |
+
if len(text) == 0: text = 'blank'
|
| 17 |
+
return text
|
| 18 |
+
|
| 19 |
+
def log_gpu_memory():
|
| 20 |
+
print(subprocess.check_output('nvidia-smi').decode('utf-8'))
|
| 21 |
+
|
| 22 |
+
log_gpu_memory()
|
| 23 |
+
|
| 24 |
+
model = MinDalle(
|
| 25 |
+
is_mega=True,
|
| 26 |
+
is_reusable=True,
|
| 27 |
+
device='cuda',
|
| 28 |
+
dtype=torch.float32
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
log_gpu_memory()
|
| 32 |
+
|
| 33 |
+
def run_model(
|
| 34 |
+
text: str,
|
| 35 |
+
grid_size: int,
|
| 36 |
+
is_seamless: bool,
|
| 37 |
+
save_as_png: bool,
|
| 38 |
+
temperature: float,
|
| 39 |
+
supercondition: str,
|
| 40 |
+
top_k: str
|
| 41 |
+
) -> str:
|
| 42 |
+
torch.set_grad_enabled(False)
|
| 43 |
+
torch.backends.cudnn.enabled = True
|
| 44 |
+
torch.backends.cudnn.deterministic = False
|
| 45 |
+
torch.backends.cudnn.benchmark = True
|
| 46 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 47 |
+
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = True
|
| 48 |
+
|
| 49 |
+
print('text:', text)
|
| 50 |
+
print('grid_size:', grid_size)
|
| 51 |
+
print('is_seamless:', is_seamless)
|
| 52 |
+
print('temperature:', temperature)
|
| 53 |
+
print('supercondition:', supercondition)
|
| 54 |
+
print('top_k:', top_k)
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
temperature = float(temperature)
|
| 58 |
+
assert(temperature > 1e-6)
|
| 59 |
+
except:
|
| 60 |
+
raise Exception('Temperature must be a positive nonzero number')
|
| 61 |
+
try:
|
| 62 |
+
grid_size = int(grid_size)
|
| 63 |
+
assert(grid_size <= 5)
|
| 64 |
+
assert(grid_size >= 1)
|
| 65 |
+
except:
|
| 66 |
+
raise Exception('Grid size must be between 1 and 5')
|
| 67 |
+
try:
|
| 68 |
+
top_k = int(top_k)
|
| 69 |
+
assert(top_k <= 16384)
|
| 70 |
+
assert(top_k >= 1)
|
| 71 |
+
except:
|
| 72 |
+
raise Exception('Top k must be between 1 and 16384')
|
| 73 |
+
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
image = model.generate_image(
|
| 76 |
+
text = text,
|
| 77 |
+
seed = -1,
|
| 78 |
+
grid_size = grid_size,
|
| 79 |
+
is_seamless = bool(is_seamless),
|
| 80 |
+
temperature = temperature,
|
| 81 |
+
supercondition_factor = float(supercondition),
|
| 82 |
+
top_k = top_k,
|
| 83 |
+
is_verbose = True
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
log_gpu_memory()
|
| 87 |
+
|
| 88 |
+
ext = 'png' if bool(save_as_png) else 'jpg'
|
| 89 |
+
filename = filename_from_text(text)
|
| 90 |
+
image_path = '{}.{}'.format(filename, ext)
|
| 91 |
+
image.save(image_path)
|
| 92 |
+
|
| 93 |
+
return image_path
|
| 94 |
+
|
| 95 |
+
demo = gradio.Blocks(analytics_enabled=True)
|
| 96 |
+
|
| 97 |
+
with demo:
|
| 98 |
+
with gradio.Row():
|
| 99 |
+
with gradio.Column():
|
| 100 |
+
input_text = gradio.Textbox(
|
| 101 |
+
label='Input Text',
|
| 102 |
+
value='Moai statue giving a TED Talk',
|
| 103 |
+
lines=3
|
| 104 |
+
)
|
| 105 |
+
run_button = gradio.Button(value='Generate Image').style(full_width=True)
|
| 106 |
+
output_image = gradio.Image(
|
| 107 |
+
value='examples/moai-statue.jpg',
|
| 108 |
+
label='Output Image',
|
| 109 |
+
type='file',
|
| 110 |
+
interactive=False
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
with gradio.Column():
|
| 114 |
+
gradio.Markdown('## Settings')
|
| 115 |
+
with gradio.Row():
|
| 116 |
+
grid_size = gradio.Slider(
|
| 117 |
+
label='Grid Size',
|
| 118 |
+
value=5,
|
| 119 |
+
minimum=1,
|
| 120 |
+
maximum=5,
|
| 121 |
+
step=1
|
| 122 |
+
)
|
| 123 |
+
save_as_png = gradio.Checkbox(
|
| 124 |
+
label='Output PNG',
|
| 125 |
+
value=False
|
| 126 |
+
)
|
| 127 |
+
is_seamless = gradio.Checkbox(
|
| 128 |
+
label='Seamless',
|
| 129 |
+
value=False
|
| 130 |
+
)
|
| 131 |
+
gradio.Markdown('#### Advanced')
|
| 132 |
+
with gradio.Row():
|
| 133 |
+
temperature = gradio.Number(
|
| 134 |
+
label='Temperature',
|
| 135 |
+
value=1
|
| 136 |
+
)
|
| 137 |
+
top_k = gradio.Dropdown(
|
| 138 |
+
label='Top-k',
|
| 139 |
+
choices=[str(2 ** i) for i in range(15)],
|
| 140 |
+
value='128'
|
| 141 |
+
)
|
| 142 |
+
supercondition = gradio.Dropdown(
|
| 143 |
+
label='Super Condition',
|
| 144 |
+
choices=[str(2 ** i) for i in range(2, 7)],
|
| 145 |
+
value='16'
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
gradio.Markdown(
|
| 149 |
+
"""
|
| 150 |
+
####
|
| 151 |
+
- **Input Text**: For long prompts, only the first 64 text tokens will be used to generate the image.
|
| 152 |
+
- **Grid Size**: Size of the image grid. 3x3 takes about 15 seconds.
|
| 153 |
+
- **Seamless**: Tile images in image token space instead of pixel space.
|
| 154 |
+
- **Temperature**: High temperature increases the probability of sampling low scoring image tokens.
|
| 155 |
+
- **Top-k**: Each image token is sampled from the top-k scoring tokens.
|
| 156 |
+
- **Super Condition**: Higher values can result in better agreement with the text.
|
| 157 |
+
"""
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
gradio.Examples(
|
| 161 |
+
examples=[
|
| 162 |
+
['Rusty Iron Man suit found abandoned in the woods being reclaimed by nature', 3, 'examples/rusty-iron-man.jpg'],
|
| 163 |
+
['Moai statue giving a TED Talk', 5, 'examples/moai-statue.jpg'],
|
| 164 |
+
['Court sketch of Godzilla on trial', 5, 'examples/godzilla-trial.jpg'],
|
| 165 |
+
['lofi nuclear war to relax and study to', 5, 'examples/lofi-nuclear-war.jpg'],
|
| 166 |
+
['Karl Marx slimed at Kids Choice Awards', 4, 'examples/marx-slimed.jpg'],
|
| 167 |
+
['Scientists trying to rhyme orange with banana', 4, 'examples/scientists-rhyme.jpg'],
|
| 168 |
+
['Jesus turning water into wine on Americas Got Talent', 5, 'examples/jesus-talent.jpg'],
|
| 169 |
+
['Elmo in a street riot throwing a Molotov cocktail, hyperrealistic', 5, 'examples/elmo-riot.jpg'],
|
| 170 |
+
['Trail cam footage of gollum eating watermelon', 4, 'examples/gollum.jpg'],
|
| 171 |
+
['Funeral at Whole Foods', 4, 'examples/funeral-whole-foods.jpg'],
|
| 172 |
+
['Singularity, hyperrealism', 5, 'examples/singularity.jpg'],
|
| 173 |
+
['Astronaut riding a horse hyperrealistic', 5, 'examples/astronaut-horse.jpg'],
|
| 174 |
+
['An astronaut walking on Mars next to a Starship rocket, realistic', 5, 'examples/astronaut-mars.jpg'],
|
| 175 |
+
['Nuclear explosion broccoli', 4, 'examples/nuclear-broccoli.jpg'],
|
| 176 |
+
['Dali painting of WALL·E', 5, 'examples/dali-walle.jpg'],
|
| 177 |
+
['Cleopatra checking her iPhone', 4, 'examples/cleopatra-iphone.jpg'],
|
| 178 |
+
],
|
| 179 |
+
inputs=[
|
| 180 |
+
input_text,
|
| 181 |
+
grid_size,
|
| 182 |
+
output_image
|
| 183 |
+
],
|
| 184 |
+
examples_per_page=20
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
run_button.click(
|
| 188 |
+
fn=run_model,
|
| 189 |
+
inputs=[
|
| 190 |
+
input_text,
|
| 191 |
+
grid_size,
|
| 192 |
+
is_seamless,
|
| 193 |
+
save_as_png,
|
| 194 |
+
temperature,
|
| 195 |
+
supercondition,
|
| 196 |
+
top_k
|
| 197 |
+
],
|
| 198 |
+
outputs=[
|
| 199 |
+
output_image
|
| 200 |
+
]
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
demo.launch()
|
examples/astronaut-horse.jpg
ADDED
|
examples/astronaut-mars.jpg
ADDED
|
examples/cleopatra-iphone.jpg
ADDED
|
examples/dali-walle.jpg
ADDED
|
examples/elmo-riot.jpg
ADDED
|
examples/funeral-whole-foods.jpg
ADDED
|
examples/godzilla-trial.jpg
ADDED
|
examples/gollum.jpg
ADDED
|
examples/jesus-talent.jpg
ADDED
|
examples/lofi-nuclear-war.jpg
ADDED
|
examples/marx-slimed.jpg
ADDED
|
examples/moai-statue.jpg
ADDED
|
examples/nuclear-broccoli.jpg
ADDED
|
examples/rusty-iron-man.jpg
ADDED
|
examples/scientists-rhyme.jpg
ADDED
|
examples/singularity.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
min-dalle==0.4.6
|
| 2 |
+
emoji==1.7.0
|
| 3 |
+
|
| 4 |
+
--find-links https://download.pytorch.org/whl/torch_stable.html
|
| 5 |
+
torch==1.12.1+cu116
|