ehristoforu commited on
Commit
80d1235
β€’
1 Parent(s): c05a567

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +13 -0
  2. app.py +207 -0
  3. requirements.txt +6 -0
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: JuggernautXL
3
+ emoji: 🌍
4
+ colorFrom: pink
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 4.8.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
13
+
14
+ DESCRIPTION = """# JuggernautXL ```V6```"""
15
+ if not torch.cuda.is_available():
16
+ DESCRIPTION += "\n<p>Running on CPU πŸ₯Ά This demo may not work on CPU.</p>"
17
+
18
+ MAX_SEED = np.iinfo(np.int32).max
19
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
20
+ #MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
21
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
22
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
23
+
24
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
25
+
26
+
27
+ if torch.cuda.is_available():
28
+ pipe = StableDiffusionXLPipeline.from_single_file(
29
+ "https://huggingface.co/lllyasviel/fav_models/blob/main/fav/juggernautXL_version6Rundiffusion.safetensors",
30
+ use_safetensors=True,
31
+ )
32
+ if ENABLE_CPU_OFFLOAD:
33
+ pipe.enable_model_cpu_offload()
34
+ else:
35
+ pipe.to(device)
36
+ print("Loaded on Device!")
37
+ pipe.load_lora_weights("stabilityai/stable-diffusion-xl-base-1.0", weight_name="sd_xl_offset_example-lora_1.0.safetensors")
38
+ pipe.fuse_lora(lora_scale=0.1)
39
+ if USE_TORCH_COMPILE:
40
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
41
+ print("Model Compiled!")
42
+
43
+
44
+ def save_image(img):
45
+ unique_name = str(uuid.uuid4()) + ".png"
46
+ img.save(unique_name)
47
+ return unique_name
48
+
49
+
50
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
51
+ if randomize_seed:
52
+ seed = random.randint(0, MAX_SEED)
53
+ return seed
54
+
55
+
56
+ @spaces.GPU(enable_queue=True)
57
+ def generate(
58
+ prompt: str,
59
+ negative_prompt: str = "",
60
+ use_negative_prompt: bool = False,
61
+ seed: int = 0,
62
+ width: int = 1024,
63
+ height: int = 1024,
64
+ guidance_scale: float = 3,
65
+ randomize_seed: bool = False,
66
+ progress=gr.Progress(track_tqdm=True),
67
+ ):
68
+
69
+ pipe.to(device)
70
+ seed = int(randomize_seed_fn(seed, randomize_seed))
71
+
72
+ if not use_negative_prompt:
73
+ negative_prompt = None # type: ignore
74
+ with torch.no_grad():
75
+ images = pipe(
76
+ prompt=prompt,
77
+ negative_prompt=negative_prompt,
78
+ width=width,
79
+ height=height,
80
+ guidance_scale=guidance_scale,
81
+ num_inference_steps=25,
82
+ num_images_per_prompt=1,
83
+ output_type="pil",
84
+ ).images
85
+
86
+ image_paths = [save_image(img) for img in images]
87
+ print(image_paths)
88
+ return image_paths, seed
89
+
90
+
91
+
92
+
93
+ examples = [
94
+ "neon holography crystal cat",
95
+ "a cat eating a piece of cheese",
96
+ "an astronaut riding a horse in space",
97
+ "a cartoon of a boy playing with a tiger",
98
+ "a cute robot artist painting on an easel, concept art",
99
+ "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
100
+ ]
101
+
102
+ css = '''
103
+ .gradio-container{max-width: 560px !important}
104
+ h1{text-align:center}
105
+ footer {
106
+ visibility: hidden
107
+ }
108
+ '''
109
+ with gr.Blocks(css=css) as demo:
110
+ gr.Markdown(DESCRIPTION)
111
+ gr.DuplicateButton(
112
+ value="Duplicate Space for private use",
113
+ elem_id="duplicate-button",
114
+ visible=False,
115
+ )
116
+ with gr.Group():
117
+ with gr.Row():
118
+ prompt = gr.Text(
119
+ label="Prompt",
120
+ show_label=False,
121
+ max_lines=1,
122
+ placeholder="Enter your prompt",
123
+ container=False,
124
+ )
125
+ run_button = gr.Button("Run", scale=0)
126
+ result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
127
+ with gr.Accordion("Advanced options", open=False):
128
+ with gr.Row():
129
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
130
+ negative_prompt = gr.Text(
131
+ label="Negative prompt",
132
+ max_lines=1,
133
+ placeholder="Enter a negative prompt",
134
+ visible=False,
135
+ )
136
+ seed = gr.Slider(
137
+ label="Seed",
138
+ minimum=0,
139
+ maximum=MAX_SEED,
140
+ step=1,
141
+ value=0,
142
+ visible=True
143
+ )
144
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
145
+ with gr.Row(visible=True):
146
+ width = gr.Slider(
147
+ label="Width",
148
+ minimum=512,
149
+ maximum=1048,
150
+ step=8,
151
+ value=768,
152
+ )
153
+ height = gr.Slider(
154
+ label="Height",
155
+ minimum=512,
156
+ maximum=1048,
157
+ step=8,
158
+ value=768,
159
+ )
160
+ with gr.Row():
161
+ guidance_scale = gr.Slider(
162
+ label="Guidance Scale",
163
+ minimum=0.1,
164
+ maximum=20,
165
+ step=0.1,
166
+ value=3.5,
167
+ )
168
+
169
+ gr.Examples(
170
+ examples=examples,
171
+ inputs=prompt,
172
+ outputs=[result, seed],
173
+ fn=generate,
174
+ cache_examples=False,
175
+ )
176
+
177
+ use_negative_prompt.change(
178
+ fn=lambda x: gr.update(visible=x),
179
+ inputs=use_negative_prompt,
180
+ outputs=negative_prompt,
181
+ api_name=False,
182
+ )
183
+
184
+
185
+ gr.on(
186
+ triggers=[
187
+ prompt.submit,
188
+ negative_prompt.submit,
189
+ run_button.click,
190
+ ],
191
+ fn=generate,
192
+ inputs=[
193
+ prompt,
194
+ negative_prompt,
195
+ use_negative_prompt,
196
+ seed,
197
+ width,
198
+ height,
199
+ guidance_scale,
200
+ randomize_seed,
201
+ ],
202
+ outputs=[result, seed],
203
+ api_name="run",
204
+ )
205
+
206
+ if __name__ == "__main__":
207
+ demo.queue(max_size=20).launch(show_api=False, debug=False)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ diffusers==0.25.0
3
+ transformers
4
+ safetensors
5
+ accelerate
6
+ omegaconf