Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,160 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
26 |
-
seed,
|
27 |
-
randomize_seed,
|
28 |
-
width,
|
29 |
-
height,
|
30 |
-
guidance_scale,
|
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, 2k",
|
59 |
-
"An astronaut riding a green horse",
|
60 |
-
"A delicious ceviche cheesecake slice",
|
61 |
-
]
|
62 |
-
|
63 |
-
css = """
|
64 |
-
#col-container {
|
65 |
-
margin: 0 auto;
|
66 |
-
max-width: 640px;
|
67 |
-
}
|
68 |
-
"""
|
69 |
-
|
70 |
-
with gr.Blocks(css=css) as demo:
|
71 |
-
with gr.Column(elem_id="col-container"):
|
72 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
73 |
-
|
74 |
-
with gr.Row():
|
75 |
-
prompt = gr.Text(
|
76 |
-
label="Prompt",
|
77 |
-
show_label=False,
|
78 |
-
max_lines=1,
|
79 |
-
placeholder="Enter your prompt",
|
80 |
-
container=False,
|
81 |
-
)
|
82 |
-
|
83 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
84 |
-
|
85 |
-
result = gr.Image(label="Result", show_label=False)
|
86 |
-
|
87 |
-
with gr.Accordion("Advanced Settings", open=False):
|
88 |
-
negative_prompt = gr.Text(
|
89 |
-
label="Negative prompt",
|
90 |
-
max_lines=1,
|
91 |
-
placeholder="Enter a negative prompt",
|
92 |
-
visible=False,
|
93 |
-
)
|
94 |
-
|
95 |
-
seed = gr.Slider(
|
96 |
-
label="Seed",
|
97 |
-
minimum=0,
|
98 |
-
maximum=MAX_SEED,
|
99 |
-
step=1,
|
100 |
-
value=0,
|
101 |
-
)
|
102 |
-
|
103 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
104 |
-
|
105 |
-
with gr.Row():
|
106 |
-
width = gr.Slider(
|
107 |
-
label="Width",
|
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(
|
115 |
-
label="Height",
|
116 |
-
minimum=576,
|
117 |
-
maximum=MAX_IMAGE_SIZE,
|
118 |
-
step=32,
|
119 |
-
value=1024, # Dimensões reduzidas para otimizar
|
120 |
-
)
|
121 |
-
|
122 |
-
with gr.Row():
|
123 |
-
guidance_scale = gr.Slider(
|
124 |
-
label="Guidance scale",
|
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])
|
140 |
-
|
141 |
-
gr.on(
|
142 |
-
triggers=[run_button.click, prompt.submit],
|
143 |
-
fn=infer,
|
144 |
-
inputs=[
|
145 |
-
prompt,
|
146 |
-
negative_prompt,
|
147 |
-
seed,
|
148 |
-
randomize_seed,
|
149 |
-
width,
|
150 |
-
height,
|
151 |
-
guidance_scale,
|
152 |
-
num_inference_steps,
|
153 |
-
],
|
154 |
-
outputs=[result, seed],
|
155 |
-
)
|
156 |
-
|
157 |
-
if __name__ == "__main__":
|
158 |
-
demo.launch()
|
159 |
|
160 |
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
+
|
5 |
+
# Função para gerar a imagem
|
6 |
+
def generate_image():
|
7 |
+
# Criar uma imagem em branco
|
8 |
+
img = Image.new("RGB", (512, 512), color=(255, 255, 255))
|
9 |
+
draw = ImageDraw.Draw(img)
|
10 |
+
|
11 |
+
# Definir gradiente de amarelo para rosa
|
12 |
+
gradient = np.zeros((512, 512, 3), dtype=np.uint8)
|
13 |
+
for i in range(512):
|
14 |
+
for j in range(512):
|
15 |
+
r = int((i / 512) * 255) # Gradiente de vermelho
|
16 |
+
g = int((i / 512) * 255) # Gradiente de verde
|
17 |
+
b = int(255 - (i / 512) * 255) # Gradiente de azul
|
18 |
+
gradient[i, j] = [r, g, b]
|
19 |
+
|
20 |
+
# Converter o gradiente para uma imagem PIL
|
21 |
+
gradient_img = Image.fromarray(gradient)
|
22 |
+
img.paste(gradient_img, (0, 0))
|
23 |
+
|
24 |
+
# Adicionar o título na imagem
|
25 |
+
font = ImageFont.load_default()
|
26 |
+
draw.text((20, 20), "FLUX.1 [dev] 🖥️", font=font, fill=(0, 0, 0))
|
27 |
+
|
28 |
+
return img
|
29 |
+
|
30 |
+
# Definir a interface Gradio
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
with gr.Row():
|
33 |
+
gr.Markdown("### FLUX.1 [dev] 🖥️")
|
34 |
+
gr.Markdown(
|
35 |
+
"This is a Gradio app using FLUX.1. The app generates an image with a yellow to pink gradient."
|
36 |
+
)
|
37 |
+
result = gr.Image(label="Generated Image")
|
38 |
+
|
39 |
+
gr.Button("Generate Image").click(generate_image, outputs=[result])
|
40 |
+
|
41 |
+
demo.launch()
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
|