abidlabs HF staff commited on
Commit
0f77467
1 Parent(s): 0f51c59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -200
app.py CHANGED
@@ -1,203 +1,6 @@
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()
 
1
+ import gradio as gr
 
 
 
 
 
 
2
 
3
+ with gr.Blocks() as demo:
4
+ gr.Gallery(["examples/dali-walle.jpg"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  demo.launch()