Abinivesh commited on
Commit
db2b2f5
·
verified ·
1 Parent(s): 729b2e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -2,40 +2,39 @@ import gradio as gr
2
  import numpy as np
3
  import random
4
 
5
- import spaces
6
- from diffusers import DiffusionPipeline
7
  import torch
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/stable-diffusion-3.5-large"
11
 
12
  if torch.cuda.is_available():
13
- torch_dtype = torch.bfloat16
14
  else:
15
  torch_dtype = torch.float32
16
 
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
  pipe = pipe.to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
- @spaces.GPU(duration=65)
24
  def infer(
25
  prompt,
26
  negative_prompt="",
27
  seed=42,
28
  randomize_seed=False,
29
- width=1024,
30
- height=1024,
31
- guidance_scale=4.5,
32
- num_inference_steps=40,
33
  progress=gr.Progress(track_tqdm=True),
34
  ):
35
  if randomize_seed:
36
  seed = random.randint(0, MAX_SEED)
37
 
38
- generator = torch.Generator().manual_seed(seed)
39
 
40
  image = pipe(
41
  prompt=prompt,
@@ -51,7 +50,8 @@ def infer(
51
 
52
 
53
  examples = [
54
- "A capybara wearing a suit holding a sign that reads Hello World",
 
55
  ]
56
 
57
  css = """
@@ -63,8 +63,10 @@ css = """
63
 
64
  with gr.Blocks(css=css) as demo:
65
  with gr.Column(elem_id="col-container"):
66
- gr.Markdown(" # [Stable Diffusion 3.5 Large (8B)](https://huggingface.co/stabilityai/stable-diffusion-3.5-large)")
67
- 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-large) to run locally with ComfyUI or diffusers.")
 
 
68
  with gr.Row():
69
  prompt = gr.Text(
70
  label="Prompt",
@@ -83,7 +85,6 @@ with gr.Blocks(css=css) as demo:
83
  label="Negative prompt",
84
  max_lines=1,
85
  placeholder="Enter a negative prompt",
86
- visible=False,
87
  )
88
 
89
  seed = gr.Slider(
@@ -102,7 +103,7 @@ with gr.Blocks(css=css) as demo:
102
  minimum=512,
103
  maximum=MAX_IMAGE_SIZE,
104
  step=32,
105
- value=1024,
106
  )
107
 
108
  height = gr.Slider(
@@ -110,27 +111,34 @@ with gr.Blocks(css=css) as demo:
110
  minimum=512,
111
  maximum=MAX_IMAGE_SIZE,
112
  step=32,
113
- value=1024,
114
  )
115
 
116
  with gr.Row():
117
  guidance_scale = gr.Slider(
118
  label="Guidance scale",
119
  minimum=0.0,
120
- maximum=7.5,
121
- step=0.1,
122
- value=4.5,
123
  )
124
 
125
  num_inference_steps = gr.Slider(
126
  label="Number of inference steps",
127
  minimum=1,
128
- maximum=50,
129
  step=1,
130
- value=40,
131
  )
132
 
133
- gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True, cache_mode="lazy")
 
 
 
 
 
 
 
134
  gr.on(
135
  triggers=[run_button.click, prompt.submit],
136
  fn=infer,
@@ -148,4 +156,4 @@ with gr.Blocks(css=css) as demo:
148
  )
149
 
150
  if __name__ == "__main__":
151
- demo.launch()
 
2
  import numpy as np
3
  import random
4
 
5
+ from diffusers import StableDiffusionPipeline
 
6
  import torch
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ model_repo_id = "runwayml/stable-diffusion-v1-5"
10
 
11
  if torch.cuda.is_available():
12
+ torch_dtype = torch.float16
13
  else:
14
  torch_dtype = torch.float32
15
 
16
+ pipe = StableDiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
  pipe = pipe.to(device)
18
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
22
+
23
  def infer(
24
  prompt,
25
  negative_prompt="",
26
  seed=42,
27
  randomize_seed=False,
28
+ width=512,
29
+ height=512,
30
+ guidance_scale=7.5,
31
+ num_inference_steps=50,
32
  progress=gr.Progress(track_tqdm=True),
33
  ):
34
  if randomize_seed:
35
  seed = random.randint(0, MAX_SEED)
36
 
37
+ generator = torch.Generator(device).manual_seed(seed)
38
 
39
  image = pipe(
40
  prompt=prompt,
 
50
 
51
 
52
  examples = [
53
+ "A futuristic cityscape with flying cars",
54
+ "A magical forest with glowing mushrooms",
55
  ]
56
 
57
  css = """
 
63
 
64
  with gr.Blocks(css=css) as demo:
65
  with gr.Column(elem_id="col-container"):
66
+ gr.Markdown(" # Stable Diffusion v1.5 Demo")
67
+ gr.Markdown(
68
+ "[Learn more](https://huggingface.co/runwayml/stable-diffusion-v1-5) about the Stable Diffusion v1.5 model. "
69
+ )
70
  with gr.Row():
71
  prompt = gr.Text(
72
  label="Prompt",
 
85
  label="Negative prompt",
86
  max_lines=1,
87
  placeholder="Enter a negative prompt",
 
88
  )
89
 
90
  seed = gr.Slider(
 
103
  minimum=512,
104
  maximum=MAX_IMAGE_SIZE,
105
  step=32,
106
+ value=512,
107
  )
108
 
109
  height = gr.Slider(
 
111
  minimum=512,
112
  maximum=MAX_IMAGE_SIZE,
113
  step=32,
114
+ value=512,
115
  )
116
 
117
  with gr.Row():
118
  guidance_scale = gr.Slider(
119
  label="Guidance scale",
120
  minimum=0.0,
121
+ maximum=20.0,
122
+ step=0.5,
123
+ value=7.5,
124
  )
125
 
126
  num_inference_steps = gr.Slider(
127
  label="Number of inference steps",
128
  minimum=1,
129
+ maximum=100,
130
  step=1,
131
+ value=50,
132
  )
133
 
134
+ gr.Examples(
135
+ examples=examples,
136
+ inputs=[prompt],
137
+ outputs=[result, seed],
138
+ fn=infer,
139
+ cache_examples=True,
140
+ cache_mode="lazy",
141
+ )
142
  gr.on(
143
  triggers=[run_button.click, prompt.submit],
144
  fn=infer,
 
156
  )
157
 
158
  if __name__ == "__main__":
159
+ demo.launch()