cbensimon HF Staff commited on
Commit
387ec72
·
verified ·
1 Parent(s): fec2c01

Create app.py

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