eder0782 commited on
Commit
0cf40ee
·
verified ·
1 Parent(s): 4f1e465

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -24
app.py CHANGED
@@ -1,32 +1,155 @@
1
- import os
2
- import torch
 
 
3
  import gradio as gr
4
- from diffusers import StableDiffusion3Pipeline
5
- from huggingface_hub import login
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # Login com token via Secrets
8
- login(os.environ["HF_TOKEN"])
9
 
10
- # Carregar pipeline (sem variant)
11
- pipe = StableDiffusion3Pipeline.from_pretrained(
12
- "stabilityai/stable-diffusion-3.5-medium",
13
- torch_dtype=torch.float16
14
- ).to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- def gerar_imagem(prompt):
17
  image = pipe(
18
- prompt,
19
- num_inference_steps=30,
20
- guidance_scale=4.5
 
 
 
 
21
  ).images[0]
22
- return image
23
 
24
- demo = gr.Interface(
25
- fn=gerar_imagem,
26
- inputs=gr.Textbox(label="Prompt"),
27
- outputs=gr.Image(label="Imagem"),
28
- title="Stable Diffusion 3.5 Medium",
29
- description="Gere imagens com SD3.5 diretamente no Hugging Face"
30
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- demo.launch(share=True)
 
 
1
+
2
+ # from huggingface_hub import login
3
+ # # Login com token via Secrets
4
+ # login(os.environ["HF_TOKEN"])
5
  import gradio as gr
6
+ import numpy as np
7
+ import random
8
+
9
+ import spaces
10
+ from diffusers import DiffusionPipeline
11
+ import torch
12
+
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ model_repo_id = "stabilityai/stable-diffusion-3.5-medium"
15
+
16
+ if torch.cuda.is_available():
17
+ torch_dtype = torch.bfloat16
18
+ else:
19
+ torch_dtype = torch.float32
20
 
21
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
22
+ pipe = pipe.to(device)
23
 
24
+ MAX_SEED = np.iinfo(np.int32).max
25
+ MAX_IMAGE_SIZE = 1440
26
+
27
+ @spaces.GPU(duration=65)
28
+ def infer(
29
+ prompt,
30
+ negative_prompt="",
31
+ seed=42,
32
+ randomize_seed=False,
33
+ width=1024,
34
+ height=1024,
35
+ guidance_scale=5,
36
+ num_inference_steps=40,
37
+ progress=gr.Progress(track_tqdm=True),
38
+ ):
39
+ if randomize_seed:
40
+ seed = random.randint(0, MAX_SEED)
41
+
42
+ generator = torch.Generator().manual_seed(seed)
43
 
 
44
  image = pipe(
45
+ prompt=prompt,
46
+ negative_prompt=negative_prompt,
47
+ guidance_scale=guidance_scale,
48
+ num_inference_steps=num_inference_steps,
49
+ width=width,
50
+ height=height,
51
+ generator=generator,
52
  ).images[0]
 
53
 
54
+ return image, seed
55
+
56
+
57
+ examples = [
58
+ "A capybara wearing a suit holding a sign that reads Hello World",
59
+ ]
60
+
61
+ css = """
62
+ #col-container {
63
+ margin: 0 auto;
64
+ max-width: 640px;
65
+ }
66
+ """
67
+
68
+ with gr.Blocks(css=css) as demo:
69
+ with gr.Column(elem_id="col-container"):
70
+ gr.Markdown(" # [Stable Diffusion 3.5 Medium (2.6B)](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium)")
71
+ gr.Markdown("[Learn more](https://stability.ai/news/introducing-stable-diffusion-3-5) about the Stable Diffusion 3.5 series. Try on [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), or [download model](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium) to run locally with ComfyUI or diffusers.")
72
+ with gr.Row():
73
+ prompt = gr.Text(
74
+ label="Prompt",
75
+ show_label=False,
76
+ max_lines=1,
77
+ placeholder="Enter your prompt",
78
+ container=False,
79
+ )
80
+
81
+ run_button = gr.Button("Run", scale=0, variant="primary")
82
+
83
+ result = gr.Image(label="Result", show_label=False)
84
+
85
+ with gr.Accordion("Advanced Settings", open=False):
86
+ negative_prompt = gr.Text(
87
+ label="Negative prompt",
88
+ max_lines=1,
89
+ placeholder="Enter a negative prompt",
90
+ visible=False,
91
+ )
92
+
93
+ seed = gr.Slider(
94
+ label="Seed",
95
+ minimum=0,
96
+ maximum=MAX_SEED,
97
+ step=1,
98
+ value=0,
99
+ )
100
+
101
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
102
+
103
+ with gr.Row():
104
+ width = gr.Slider(
105
+ label="Width",
106
+ minimum=512,
107
+ maximum=MAX_IMAGE_SIZE,
108
+ step=32,
109
+ value=1024,
110
+ )
111
+
112
+ height = gr.Slider(
113
+ label="Height",
114
+ minimum=512,
115
+ maximum=MAX_IMAGE_SIZE,
116
+ step=32,
117
+ value=1024,
118
+ )
119
+
120
+ with gr.Row():
121
+ guidance_scale = gr.Slider(
122
+ label="Guidance scale",
123
+ minimum=0.0,
124
+ maximum=7.5,
125
+ step=0.1,
126
+ value=4.5,
127
+ )
128
+
129
+ num_inference_steps = gr.Slider(
130
+ label="Number of inference steps",
131
+ minimum=1,
132
+ maximum=50,
133
+ step=1,
134
+ value=40,
135
+ )
136
+
137
+ gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True, cache_mode="lazy")
138
+ gr.on(
139
+ triggers=[run_button.click, prompt.submit],
140
+ fn=infer,
141
+ inputs=[
142
+ prompt,
143
+ negative_prompt,
144
+ seed,
145
+ randomize_seed,
146
+ width,
147
+ height,
148
+ guidance_scale,
149
+ num_inference_steps,
150
+ ],
151
+ outputs=[result, seed],
152
+ )
153
 
154
+ if __name__ == "__main__":
155
+ demo.launch()