ehristoforu commited on
Commit
1e9dbf0
1 Parent(s): 0ebff89

Create app.py

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