Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
#col-container {
|
2 |
margin: 0 auto;
|
3 |
max-width: 520px;
|
@@ -5,44 +53,127 @@
|
|
5 |
|
6 |
/* Кнопки */
|
7 |
.gr-button {
|
8 |
-
background-color: #5271FF !important;
|
9 |
-
border-color: #5271FF !important;
|
10 |
-
color: white !important;
|
11 |
}
|
12 |
|
13 |
.gr-button:hover,
|
14 |
.gr-button:focus,
|
15 |
.gr-button:active {
|
16 |
-
background-color: #3e5ac0 !important;
|
17 |
border-color: #3e5ac0 !important;
|
18 |
}
|
19 |
|
20 |
/* Ползунки */
|
21 |
.gr-slider .noUi-connect {
|
22 |
-
background: #5271FF !important;
|
23 |
}
|
24 |
|
25 |
.gr-slider .noUi-handle {
|
26 |
-
border-color: #5271FF !important;
|
27 |
}
|
28 |
|
29 |
.gr-slider .noUi-handle:focus,
|
30 |
.gr-slider .noUi-handle:active {
|
31 |
-
border-color: #3e5ac0 !important;
|
32 |
}
|
33 |
|
34 |
/* Чекбоксы */
|
35 |
.gr-checkbox label::before {
|
36 |
-
border-color: #5271FF !important;
|
37 |
}
|
38 |
|
39 |
.gr-checkbox input[type=checkbox]:checked + label::before {
|
40 |
-
background-color: #5271FF !important;
|
41 |
}
|
42 |
|
43 |
/* Текстовые поля и другие элементы */
|
44 |
input[type="text"]:focus,
|
45 |
textarea:focus {
|
46 |
-
border-color: #5271FF !important;
|
47 |
-
outline: none !important;
|
48 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
from diffusers import DiffusionPipeline
|
5 |
+
import torch
|
6 |
+
|
7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
+
|
9 |
+
MAX_SEED = np.iinfo(np.int32).max
|
10 |
+
MAX_IMAGE_SIZE = 1024
|
11 |
+
|
12 |
+
pipe = DiffusionPipeline.from_pretrained(
|
13 |
+
"stabilityai/sdxl-turbo",
|
14 |
+
torch_dtype=torch.float16,
|
15 |
+
variant="fp16",
|
16 |
+
use_safetensors=True
|
17 |
+
)
|
18 |
+
pipe.enable_xformers_memory_efficient_attention()
|
19 |
+
pipe = pipe.to(device)
|
20 |
+
|
21 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
22 |
+
try:
|
23 |
+
if randomize_seed:
|
24 |
+
seed = random.randint(0, MAX_SEED)
|
25 |
+
|
26 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
27 |
+
|
28 |
+
image = pipe(
|
29 |
+
prompt=prompt,
|
30 |
+
negative_prompt=negative_prompt,
|
31 |
+
guidance_scale=guidance_scale,
|
32 |
+
num_inference_steps=num_inference_steps,
|
33 |
+
width=width,
|
34 |
+
height=height,
|
35 |
+
generator=generator
|
36 |
+
).images[0]
|
37 |
+
|
38 |
+
return image
|
39 |
+
except Exception as e:
|
40 |
+
return f"An error occurred: {e}"
|
41 |
+
|
42 |
+
examples = [
|
43 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
44 |
+
"An astronaut riding a green horse",
|
45 |
+
"A delicious ceviche cheesecake slice",
|
46 |
+
]
|
47 |
+
|
48 |
+
css = """
|
49 |
#col-container {
|
50 |
margin: 0 auto;
|
51 |
max-width: 520px;
|
|
|
53 |
|
54 |
/* Кнопки */
|
55 |
.gr-button {
|
56 |
+
background-color: #5271FF !important;
|
57 |
+
border-color: #5271FF !important;
|
58 |
+
color: white !important;
|
59 |
}
|
60 |
|
61 |
.gr-button:hover,
|
62 |
.gr-button:focus,
|
63 |
.gr-button:active {
|
64 |
+
background-color: #3e5ac0 !important;
|
65 |
border-color: #3e5ac0 !important;
|
66 |
}
|
67 |
|
68 |
/* Ползунки */
|
69 |
.gr-slider .noUi-connect {
|
70 |
+
background: #5271FF !important;
|
71 |
}
|
72 |
|
73 |
.gr-slider .noUi-handle {
|
74 |
+
border-color: #5271FF !important;
|
75 |
}
|
76 |
|
77 |
.gr-slider .noUi-handle:focus,
|
78 |
.gr-slider .noUi-handle:active {
|
79 |
+
border-color: #3e5ac0 !important;
|
80 |
}
|
81 |
|
82 |
/* Чекбоксы */
|
83 |
.gr-checkbox label::before {
|
84 |
+
border-color: #5271FF !important;
|
85 |
}
|
86 |
|
87 |
.gr-checkbox input[type=checkbox]:checked + label::before {
|
88 |
+
background-color: #5271FF !important;
|
89 |
}
|
90 |
|
91 |
/* Текстовые поля и другие элементы */
|
92 |
input[type="text"]:focus,
|
93 |
textarea:focus {
|
94 |
+
border-color: #5271FF !important;
|
95 |
+
outline: none !important;
|
96 |
}
|
97 |
+
"""
|
98 |
+
|
99 |
+
power_device = "GPU" if torch.cuda.is_available() else "CPU"
|
100 |
+
|
101 |
+
with gr.Blocks(css=css) as demo:
|
102 |
+
with gr.Column(elem_id="col-container"):
|
103 |
+
gr.Markdown(f"""
|
104 |
+
# ZeroGPU Text-to-Image Gradio Template
|
105 |
+
Currently running on {power_device}.
|
106 |
+
""")
|
107 |
+
|
108 |
+
with gr.Row():
|
109 |
+
prompt = gr.Text(
|
110 |
+
label="Prompt",
|
111 |
+
show_label=False,
|
112 |
+
max_lines=1,
|
113 |
+
placeholder="Enter your prompt",
|
114 |
+
container=False,
|
115 |
+
)
|
116 |
+
run_button = gr.Button("Run", scale=0)
|
117 |
+
|
118 |
+
result = gr.Image(label="Result", show_label=False)
|
119 |
+
|
120 |
+
with gr.Accordion("Advanced Settings", open=False):
|
121 |
+
negative_prompt = gr.Text(
|
122 |
+
label="Negative prompt",
|
123 |
+
max_lines=1,
|
124 |
+
placeholder="Enter a negative prompt",
|
125 |
+
visible=True,
|
126 |
+
)
|
127 |
+
seed = gr.Slider(
|
128 |
+
label="Seed",
|
129 |
+
minimum=0,
|
130 |
+
maximum=MAX_SEED,
|
131 |
+
step=1,
|
132 |
+
value=0,
|
133 |
+
)
|
134 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
135 |
+
|
136 |
+
with gr.Row():
|
137 |
+
width = gr.Slider(
|
138 |
+
label="Width",
|
139 |
+
minimum=256,
|
140 |
+
maximum=MAX_IMAGE_SIZE,
|
141 |
+
step=32,
|
142 |
+
value=512,
|
143 |
+
)
|
144 |
+
height = gr.Slider(
|
145 |
+
label="Height",
|
146 |
+
minimum=256,
|
147 |
+
maximum=MAX_IMAGE_SIZE,
|
148 |
+
step=32,
|
149 |
+
value=512,
|
150 |
+
)
|
151 |
+
|
152 |
+
with gr.Row():
|
153 |
+
guidance_scale = gr.Slider(
|
154 |
+
label="Guidance scale",
|
155 |
+
minimum=0.0,
|
156 |
+
maximum=10.0,
|
157 |
+
step=0.1,
|
158 |
+
value=7.5,
|
159 |
+
)
|
160 |
+
num_inference_steps = gr.Slider(
|
161 |
+
label="Number of inference steps",
|
162 |
+
minimum=1,
|
163 |
+
maximum=50,
|
164 |
+
step=1,
|
165 |
+
value=25,
|
166 |
+
)
|
167 |
+
|
168 |
+
gr.Examples(
|
169 |
+
examples=examples,
|
170 |
+
inputs=[prompt]
|
171 |
+
)
|
172 |
+
|
173 |
+
run_button.click(
|
174 |
+
fn=infer,
|
175 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
176 |
+
outputs=[result]
|
177 |
+
)
|
178 |
+
|
179 |
+
demo.queue().launch()
|