K00B404 commited on
Commit
6c0abe9
·
verified ·
1 Parent(s): 6997aec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +202 -0
app.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Sure, here's an updated version of the script that should be more suitable for running on a CPU with 16GB of RAM:
2
+
3
+ python
4
+
5
+ import os
6
+ import random
7
+ import uuid
8
+
9
+ import gradio as gr
10
+ import numpy as np
11
+ from PIL import Image
12
+ import spaces
13
+ import torch
14
+ from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
15
+
16
+ DESCRIPTION = """
17
+ # DALL•E 3 XL v2
18
+ """
19
+
20
+ def save_image(img):
21
+ unique_name = str(uuid.uuid4()) + ".png"
22
+ img.save(unique_name)
23
+ return unique_name
24
+
25
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
26
+ if randomize_seed:
27
+ seed = random.randint(0, MAX_SEED)
28
+ return seed
29
+
30
+ MAX_SEED = np.iinfo(np.int32).max
31
+
32
+ if not torch.cuda.is_available():
33
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may work on CPU.</p>"
34
+
35
+ MAX_SEED = np.iinfo(np.int32).max
36
+
37
+ USE_TORCH_COMPILE = 0
38
+ ENABLE_CPU_OFFLOAD = 0
39
+
40
+
41
+ if torch.cuda.is_available():
42
+ pipe = StableDiffusionXLPipeline.from_pretrained(
43
+ "fluently/Fluently-XL-v2",
44
+ torch_dtype=torch.float16,
45
+ use_safetensors=True,
46
+ )
47
+ else:
48
+ pipe = StableDiffusionPipeline.from_pretrained(
49
+ "runwayml/stable-diffusion-v1-5",
50
+ torch_dtype=torch.float32,
51
+ use_safetensors=True,
52
+ )
53
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
54
+
55
+ @spaces.CPU(enable_queue=True)
56
+ def generate(
57
+ prompt: str,
58
+ negative_prompt: str = "",
59
+ use_negative_prompt: bool = False,
60
+ seed: int = 0,
61
+ width: int = 512,
62
+ height: int = 512,
63
+ guidance_scale: float = 3,
64
+ randomize_seed: bool = False,
65
+ progress=gr.Progress(track_tqdm=True),
66
+ ):
67
+ seed = int(randomize_seed_fn(seed, randomize_seed))
68
+
69
+ if not use_negative_prompt:
70
+ negative_prompt = "" # type: ignore
71
+
72
+ images = pipe(
73
+ prompt=prompt,
74
+ negative_prompt=negative_prompt,
75
+ width=width,
76
+ height=height,
77
+ guidance_scale=guidance_scale,
78
+ num_inference_steps=25,
79
+ num_images_per_prompt=1,
80
+ output_type="pil",
81
+ ).images
82
+ image_paths = [save_image(img) for img in images]
83
+ print(image_paths)
84
+ return image_paths, seed
85
+
86
+ examples = [
87
+ "neon holography crystal cat",
88
+ "a cat eating a piece of cheese",
89
+ "an astronaut riding a horse in space",
90
+ "a cartoon of a boy playing with a tiger",
91
+ "a cute robot artist painting on an easel, concept art",
92
+ "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
93
+ ]
94
+
95
+ css = '''
96
+ .gradio-container{max-width: 560px !important}
97
+ h1{text-align:center}
98
+ footer {
99
+ visibility: hidden
100
+ }
101
+ '''
102
+ with gr.Blocks(css=css, theme="pseudolab/huggingface-korea-theme") as demo:
103
+ gr.Markdown(DESCRIPTION)
104
+ gr.DuplicateButton(
105
+ value="Duplicate Space for private use",
106
+ elem_id="duplicate-button",
107
+ visible=False,
108
+ )
109
+
110
+ with gr.Group():
111
+ with gr.Row():
112
+ prompt = gr.Text(
113
+ label="Prompt",
114
+ show_label=False,
115
+ max_lines=1,
116
+ placeholder="Enter your prompt",
117
+ container=False,
118
+ )
119
+ run_button = gr.Button("Run", scale=0)
120
+ result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
121
+ with gr.Accordion("Advanced options", open=False):
122
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
123
+ negative_prompt = gr.Text(
124
+ label="Negative prompt",
125
+ lines=4,
126
+ max_lines=6,
127
+ value="""(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)""",
128
+ placeholder="Enter a negative prompt",
129
+ visible=True,
130
+ )
131
+ seed = gr.Slider(
132
+ label="Seed",
133
+ minimum=0,
134
+ maximum=MAX_SEED,
135
+ step=1,
136
+ value=0,
137
+ visible=True
138
+ )
139
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
140
+ with gr.Row(visible=True):
141
+ width = gr.Slider(
142
+ label="Width",
143
+ minimum=512,
144
+ maximum=1024,
145
+ step=8,
146
+ value=512,
147
+ )
148
+ height = gr.Slider(
149
+ label="Height",
150
+ minimum=512,
151
+ maximum=1024,
152
+ step=8,
153
+ value=512,
154
+ )
155
+ with gr.Row():
156
+ guidance_scale = gr.Slider(
157
+ label="Guidance Scale",
158
+ minimum=0.1,
159
+ maximum=20.0,
160
+ step=0.1,
161
+ value=6,
162
+ )
163
+
164
+ gr.Examples(
165
+ examples=examples,
166
+ inputs=prompt,
167
+ outputs=[result, seed],
168
+ fn=generate,
169
+ cache_examples=False,
170
+ )
171
+
172
+ use_negative_prompt.change(
173
+ fn=lambda x: gr.update(visible=x),
174
+ inputs=use_negative_prompt,
175
+ outputs=negative_prompt,
176
+ api_name=False,
177
+ )
178
+
179
+
180
+ gr.on(
181
+ triggers=[
182
+ prompt.submit,
183
+ negative_prompt.submit,
184
+ run_button.click,
185
+ ],
186
+ fn=generate,
187
+ inputs=[
188
+ prompt,
189
+ negative_prompt,
190
+ use_negative_prompt,
191
+ seed,
192
+ width,
193
+ height,
194
+ guidance_scale,
195
+ randomize_seed,
196
+ ],
197
+ outputs=[result, seed],
198
+ api_name="run",
199
+ )
200
+
201
+ if __name__ == "__main__":
202
+ demo.queue(max_size=20).launch(show_api=False, debug=False)