ehristoforu commited on
Commit
69d24df
1 Parent(s): 2acc20b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +203 -0
app.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (
13
+ StableDiffusionXLPipeline,
14
+ DPMSolverMultistepScheduler,
15
+ AutoencoderKL
16
+ )
17
+ DESCRIPTION = """
18
+ # [Visionix Playground](https://huggingface.co/spaces/ehristoforu/Visionix-Playground)
19
+
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 = 0
27
+ ENABLE_CPU_OFFLOAD = 0
28
+
29
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
30
+
31
+ pipe = StableDiffusionXLPipeline.from_pretrained(
32
+ "ehristoforu/Visionix-alpha",
33
+ torch_dtype=torch.float16,
34
+ use_safetensors=True,
35
+ )
36
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
37
+ pipe.to('cuda')
38
+
39
+ def save_image(img):
40
+ unique_name = str(uuid.uuid4()) + ".png"
41
+ img.save(unique_name)
42
+ return unique_name
43
+
44
+
45
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
46
+ if randomize_seed:
47
+ seed = random.randint(0, MAX_SEED)
48
+ return seed
49
+
50
+
51
+ @spaces.GPU(durarion=50, enable_queue=True)
52
+ def generate(
53
+ prompt: str,
54
+ negative_prompt: str = "",
55
+ use_negative_prompt: bool = False,
56
+ seed: int = 0,
57
+ width: int = 1024,
58
+ height: int = 1024,
59
+ guidance_scale: float = 3,
60
+ randomize_seed: bool = False,
61
+ progress=gr.Progress(track_tqdm=True),
62
+ ):
63
+
64
+ pipe.to(device)
65
+ seed = int(randomize_seed_fn(seed, randomize_seed))
66
+
67
+ if not use_negative_prompt:
68
+ negative_prompt = "" # type: ignore
69
+ images = pipe(
70
+ prompt=prompt,
71
+ negative_prompt=f"{negative_prompt}",
72
+ width=width,
73
+ height=height,
74
+ guidance_scale=guidance_scale,
75
+ num_inference_steps=25,
76
+ num_images_per_prompt=1,
77
+ output_type="pil",
78
+ ).images
79
+
80
+ image_paths = [save_image(img) for img in images]
81
+ print(image_paths)
82
+ return image_paths, seed
83
+
84
+
85
+
86
+
87
+ examples = [
88
+ "neon holography crystal cat",
89
+ "a cat eating a piece of cheese",
90
+ "an astronaut riding a horse in space",
91
+ "a cartoon of a boy playing with a tiger",
92
+ "a cute robot artist painting on an easel, concept art",
93
+ "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
94
+ ]
95
+
96
+ css = '''
97
+ .gradio-container{max-width: 560px !important}
98
+ h1{text-align:center}
99
+ footer {
100
+ visibility: hidden
101
+ }
102
+ '''
103
+ with gr.Blocks(title="Visionix Playground", css=css) as demo:
104
+ gr.Markdown(DESCRIPTION)
105
+ gr.DuplicateButton(
106
+ value="Duplicate Space for private use",
107
+ elem_id="duplicate-button",
108
+ visible=False,
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
+ with gr.Row():
123
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
124
+ negative_prompt = gr.Text(
125
+ label="Negative prompt",
126
+ max_lines=7,
127
+ lines=5,
128
+ value="",
129
+ placeholder="Enter a negative prompt",
130
+ visible=True,
131
+ )
132
+ seed = gr.Slider(
133
+ label="Seed",
134
+ minimum=0,
135
+ maximum=MAX_SEED,
136
+ step=1,
137
+ value=0,
138
+ visible=True
139
+ )
140
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
141
+ with gr.Row(visible=True):
142
+ width = gr.Slider(
143
+ label="Width",
144
+ minimum=512,
145
+ maximum=2048,
146
+ step=8,
147
+ value=1024,
148
+ )
149
+ height = gr.Slider(
150
+ label="Height",
151
+ minimum=512,
152
+ maximum=2048,
153
+ step=8,
154
+ value=1024,
155
+ )
156
+ with gr.Row():
157
+ guidance_scale = gr.Slider(
158
+ label="Guidance Scale",
159
+ minimum=0.1,
160
+ maximum=20,
161
+ step=0.1,
162
+ value=5.5,
163
+ )
164
+
165
+ gr.Examples(
166
+ examples=examples,
167
+ inputs=prompt,
168
+ outputs=[result, seed],
169
+ fn=generate,
170
+ cache_examples=False,
171
+ )
172
+
173
+ use_negative_prompt.change(
174
+ fn=lambda x: gr.update(visible=x),
175
+ inputs=use_negative_prompt,
176
+ outputs=negative_prompt,
177
+ api_name=False,
178
+ )
179
+
180
+
181
+ gr.on(
182
+ triggers=[
183
+ prompt.submit,
184
+ negative_prompt.submit,
185
+ run_button.click,
186
+ ],
187
+ fn=generate,
188
+ inputs=[
189
+ prompt,
190
+ negative_prompt,
191
+ use_negative_prompt,
192
+ seed,
193
+ width,
194
+ height,
195
+ guidance_scale,
196
+ randomize_seed,
197
+ ],
198
+ outputs=[result, seed],
199
+ api_name="run",
200
+ )
201
+
202
+ if __name__ == "__main__":
203
+ demo.queue(max_size=20).launch(show_api=False, debug=False)