Yjiggfghhjnjj commited on
Commit
0b836de
1 Parent(s): 411a38c

Update app.py

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