eder0782 commited on
Commit
a8f57f8
·
verified ·
1 Parent(s): 344bafa
Files changed (1) hide show
  1. app.py +54 -83
app.py CHANGED
@@ -5,71 +5,56 @@
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,
@@ -77,19 +62,13 @@ with gr.Blocks(css=css) as demo:
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,
@@ -97,59 +76,51 @@ with gr.Blocks(css=css) as demo:
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()
 
5
  import gradio as gr
6
  import numpy as np
7
  import random
 
8
  import spaces
 
9
  import torch
10
+ from diffusers import DiffusionPipeline
11
 
12
+ dtype = torch.bfloat16
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
14
 
15
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
 
 
 
 
 
 
16
 
17
  MAX_SEED = np.iinfo(np.int32).max
18
+ MAX_IMAGE_SIZE = 2048
19
 
20
+ @spaces.GPU()
21
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
 
22
  if randomize_seed:
23
  seed = random.randint(0, MAX_SEED)
 
24
  generator = torch.Generator().manual_seed(seed)
 
25
  image = pipe(
26
+ prompt = prompt,
27
+ width = width,
28
+ height = height,
29
+ num_inference_steps = num_inference_steps,
30
+ generator = generator,
31
+ guidance_scale=0.0
32
+ ).images[0]
 
 
33
  return image, seed
34
+
 
35
  examples = [
36
+ "a tiny astronaut hatching from an egg on the moon",
37
+ "a cat holding a sign that says hello world",
38
+ "an anime illustration of a wiener schnitzel",
39
  ]
40
 
41
+ css="""
42
  #col-container {
43
  margin: 0 auto;
44
+ max-width: 520px;
45
  }
46
  """
47
 
48
  with gr.Blocks(css=css) as demo:
49
+
50
  with gr.Column(elem_id="col-container"):
51
+ gr.Markdown(f"""# FLUX.1 [schnell]
52
+ 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
53
+ [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
54
+ """)
55
+
56
  with gr.Row():
57
+
58
  prompt = gr.Text(
59
  label="Prompt",
60
  show_label=False,
 
62
  placeholder="Enter your prompt",
63
  container=False,
64
  )
65
+
66
+ run_button = gr.Button("Run", scale=0)
67
+
68
  result = gr.Image(label="Result", show_label=False)
69
+
70
  with gr.Accordion("Advanced Settings", open=False):
71
+
 
 
 
 
 
 
72
  seed = gr.Slider(
73
  label="Seed",
74
  minimum=0,
 
76
  step=1,
77
  value=0,
78
  )
79
+
80
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
81
+
82
  with gr.Row():
83
+
84
  width = gr.Slider(
85
  label="Width",
86
+ minimum=256,
87
  maximum=MAX_IMAGE_SIZE,
88
  step=32,
89
+ value=1024,
90
  )
91
+
92
  height = gr.Slider(
93
  label="Height",
94
+ minimum=256,
95
  maximum=MAX_IMAGE_SIZE,
96
  step=32,
97
  value=1024,
98
  )
99
+
100
  with gr.Row():
101
+
102
+
 
 
 
 
 
 
103
  num_inference_steps = gr.Slider(
104
  label="Number of inference steps",
105
  minimum=1,
106
  maximum=50,
107
  step=1,
108
+ value=4,
109
  )
110
+
111
+ gr.Examples(
112
+ examples = examples,
113
+ fn = infer,
114
+ inputs = [prompt],
115
+ outputs = [result, seed],
116
+ cache_examples="lazy"
117
+ )
118
 
 
119
  gr.on(
120
  triggers=[run_button.click, prompt.submit],
121
+ fn = infer,
122
+ inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
123
+ outputs = [result, seed]
 
 
 
 
 
 
 
 
 
124
  )
125
 
126
+ demo.launch()