Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,19 +3,23 @@ import numpy as np
|
|
3 |
import random
|
4 |
import torch
|
5 |
from diffusers import DiffusionPipeline
|
|
|
6 |
|
|
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
-
model_repo_id = "stabilityai/sdxl-turbo" # Modelo
|
9 |
|
10 |
-
# Usando
|
11 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
12 |
|
|
|
13 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype).to(device)
|
14 |
|
15 |
-
#
|
16 |
MAX_SEED = np.iinfo(np.int32).max
|
17 |
-
MAX_IMAGE_SIZE =
|
18 |
|
|
|
19 |
def infer(
|
20 |
prompt,
|
21 |
negative_prompt,
|
@@ -27,25 +31,29 @@ def infer(
|
|
27 |
num_inference_steps,
|
28 |
progress=gr.Progress(track_tqdm=True),
|
29 |
):
|
30 |
-
#
|
31 |
if randomize_seed:
|
32 |
seed = random.randint(0, MAX_SEED)
|
33 |
|
34 |
generator = torch.Generator(device).manual_seed(seed)
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
46 |
|
47 |
return image, seed
|
48 |
|
|
|
|
|
49 |
examples = [
|
50 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
51 |
"An astronaut riding a green horse",
|
@@ -100,7 +108,7 @@ with gr.Blocks(css=css) as demo:
|
|
100 |
minimum=256,
|
101 |
maximum=MAX_IMAGE_SIZE,
|
102 |
step=32,
|
103 |
-
value=
|
104 |
)
|
105 |
|
106 |
height = gr.Slider(
|
@@ -108,7 +116,7 @@ with gr.Blocks(css=css) as demo:
|
|
108 |
minimum=256,
|
109 |
maximum=MAX_IMAGE_SIZE,
|
110 |
step=32,
|
111 |
-
value=
|
112 |
)
|
113 |
|
114 |
with gr.Row():
|
@@ -117,15 +125,15 @@ with gr.Blocks(css=css) as demo:
|
|
117 |
minimum=0.0,
|
118 |
maximum=10.0,
|
119 |
step=0.1,
|
120 |
-
value=7.5, # Valor
|
121 |
)
|
122 |
|
123 |
num_inference_steps = gr.Slider(
|
124 |
label="Inference steps",
|
125 |
minimum=1,
|
126 |
-
maximum=
|
127 |
step=1,
|
128 |
-
value=20, #
|
129 |
)
|
130 |
|
131 |
gr.Examples(examples=examples, inputs=[prompt])
|
@@ -148,3 +156,4 @@ with gr.Blocks(css=css) as demo:
|
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
demo.launch()
|
|
|
|
3 |
import random
|
4 |
import torch
|
5 |
from diffusers import DiffusionPipeline
|
6 |
+
from torch import autocast # Usando autocast para otimizar operações em float16
|
7 |
|
8 |
+
# Verifica se a GPU está disponível
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
model_repo_id = "stabilityai/sdxl-turbo" # Modelo otimizado para velocidade
|
11 |
|
12 |
+
# Usando float16 para otimizar a execução na GPU
|
13 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
14 |
|
15 |
+
# Carregando o modelo com otimizações
|
16 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype).to(device)
|
17 |
|
18 |
+
# Max seed
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
20 |
+
MAX_IMAGE_SIZE = 512 # Dimensões menores para acelerar
|
21 |
|
22 |
+
# Função de inferência otimizada
|
23 |
def infer(
|
24 |
prompt,
|
25 |
negative_prompt,
|
|
|
31 |
num_inference_steps,
|
32 |
progress=gr.Progress(track_tqdm=True),
|
33 |
):
|
34 |
+
# Randomiza a semente, se necessário
|
35 |
if randomize_seed:
|
36 |
seed = random.randint(0, MAX_SEED)
|
37 |
|
38 |
generator = torch.Generator(device).manual_seed(seed)
|
39 |
|
40 |
+
# Usando autocast para acelerar o cálculo com float16 em GPUs
|
41 |
+
with autocast("cuda"):
|
42 |
+
# Geração da imagem com um número reduzido de passos (para acelerar)
|
43 |
+
image = pipe(
|
44 |
+
prompt=prompt,
|
45 |
+
negative_prompt=negative_prompt,
|
46 |
+
guidance_scale=guidance_scale,
|
47 |
+
num_inference_steps=num_inference_steps,
|
48 |
+
width=width,
|
49 |
+
height=height,
|
50 |
+
generator=generator,
|
51 |
+
).images[0]
|
52 |
|
53 |
return image, seed
|
54 |
|
55 |
+
|
56 |
+
# Exemplos para o Gradio
|
57 |
examples = [
|
58 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
59 |
"An astronaut riding a green horse",
|
|
|
108 |
minimum=256,
|
109 |
maximum=MAX_IMAGE_SIZE,
|
110 |
step=32,
|
111 |
+
value=512, # Dimensões reduzidas para otimizar
|
112 |
)
|
113 |
|
114 |
height = gr.Slider(
|
|
|
116 |
minimum=256,
|
117 |
maximum=MAX_IMAGE_SIZE,
|
118 |
step=32,
|
119 |
+
value=512, # Dimensões reduzidas para otimizar
|
120 |
)
|
121 |
|
122 |
with gr.Row():
|
|
|
125 |
minimum=0.0,
|
126 |
maximum=10.0,
|
127 |
step=0.1,
|
128 |
+
value=7.5, # Valor adequado para controle
|
129 |
)
|
130 |
|
131 |
num_inference_steps = gr.Slider(
|
132 |
label="Inference steps",
|
133 |
minimum=1,
|
134 |
+
maximum=30, # Menos passos para otimizar a velocidade
|
135 |
step=1,
|
136 |
+
value=20, # Um valor equilibrado
|
137 |
)
|
138 |
|
139 |
gr.Examples(examples=examples, inputs=[prompt])
|
|
|
156 |
|
157 |
if __name__ == "__main__":
|
158 |
demo.launch()
|
159 |
+
|