SeedOfEvil commited on
Commit
f11f490
·
verified ·
1 Parent(s): 8103535

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -1,24 +1,32 @@
1
  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
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
12
 
13
- if torch.cuda.is_available():
14
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
15
-
16
  pipe = DiffusionPipeline.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
  @spaces.GPU(duration=65)
23
  def infer(
24
  prompt,
@@ -31,11 +39,16 @@ def infer(
31
  num_inference_steps=40,
32
  progress=gr.Progress(track_tqdm=True),
33
  ):
 
34
  if randomize_seed:
35
  seed = random.randint(0, MAX_SEED)
 
36
 
37
- generator = torch.Generator().manual_seed(seed)
 
 
38
 
 
39
  image = pipe(
40
  prompt=prompt,
41
  negative_prompt=negative_prompt,
@@ -44,13 +57,14 @@ def infer(
44
  width=width,
45
  height=height,
46
  generator=generator,
 
47
  ).images[0]
48
 
49
  return image, seed
50
 
51
-
52
  examples = [
53
- "A capybara wearing a suit holding a sign that reads Hello World",
54
  ]
55
 
56
  css = """
@@ -63,7 +77,7 @@ css = """
63
  with gr.Blocks(css=css) as demo:
64
  with gr.Column(elem_id="col-container"):
65
  gr.Markdown(" # [Stable Diffusion 3.5 Large (8B)](https://huggingface.co/stabilityai/stable-diffusion-3.5-large)")
66
- 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.")
67
  with gr.Row():
68
  prompt = gr.Text(
69
  label="Prompt",
@@ -72,11 +86,8 @@ with gr.Blocks(css=css) as demo:
72
  placeholder="Enter your prompt",
73
  container=False,
74
  )
75
-
76
  run_button = gr.Button("Run", scale=0, variant="primary")
77
-
78
  result = gr.Image(label="Result", show_label=False)
79
-
80
  with gr.Accordion("Advanced Settings", open=False):
81
  negative_prompt = gr.Text(
82
  label="Negative prompt",
@@ -84,7 +95,6 @@ with gr.Blocks(css=css) as demo:
84
  placeholder="Enter a negative prompt",
85
  visible=False,
86
  )
87
-
88
  seed = gr.Slider(
89
  label="Seed",
90
  minimum=0,
@@ -92,9 +102,7 @@ with gr.Blocks(css=css) as demo:
92
  step=1,
93
  value=0,
94
  )
95
-
96
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
97
-
98
  with gr.Row():
99
  width = gr.Slider(
100
  label="Width",
@@ -103,7 +111,6 @@ with gr.Blocks(css=css) as demo:
103
  step=32,
104
  value=1024,
105
  )
106
-
107
  height = gr.Slider(
108
  label="Height",
109
  minimum=512,
@@ -111,7 +118,6 @@ with gr.Blocks(css=css) as demo:
111
  step=32,
112
  value=1024,
113
  )
114
-
115
  with gr.Row():
116
  guidance_scale = gr.Slider(
117
  label="Guidance scale",
@@ -120,7 +126,6 @@ with gr.Blocks(css=css) as demo:
120
  step=0.1,
121
  value=4.5,
122
  )
123
-
124
  num_inference_steps = gr.Slider(
125
  label="Number of inference steps",
126
  minimum=1,
@@ -128,7 +133,6 @@ with gr.Blocks(css=css) as demo:
128
  step=1,
129
  value=40,
130
  )
131
-
132
  gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True, cache_mode="lazy")
133
  gr.on(
134
  triggers=[run_button.click, prompt.submit],
@@ -148,3 +152,4 @@ with gr.Blocks(css=css) as demo:
148
 
149
  if __name__ == "__main__":
150
  demo.launch()
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
  import spaces
5
  from diffusers import DiffusionPipeline
6
  import torch
7
 
8
+ # Set device and model parameters
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  model_repo_id = "stabilityai/stable-diffusion-3.5-large"
11
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
12
 
13
+ # Load the Stable Diffusion pipeline and move it to the appropriate device.
 
 
14
  pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
15
  pipe = pipe.to(device)
16
 
17
+ # Maximum values as defined in your original code
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 1024
20
 
21
+ # Define a helper function to truncate text to a maximum of 77 tokens.
22
+ def truncate_text(text, max_tokens=77):
23
+ if text.strip() == "":
24
+ return text
25
+ # Use the pipeline's tokenizer (CLIP tokenizer)
26
+ tokens = pipe.tokenizer(text, truncation=True, max_length=max_tokens, add_special_tokens=True)
27
+ truncated_text = pipe.tokenizer.decode(tokens["input_ids"], skip_special_tokens=True)
28
+ return truncated_text
29
+
30
  @spaces.GPU(duration=65)
31
  def infer(
32
  prompt,
 
39
  num_inference_steps=40,
40
  progress=gr.Progress(track_tqdm=True),
41
  ):
42
+ # Optionally randomize seed
43
  if randomize_seed:
44
  seed = random.randint(0, MAX_SEED)
45
+ generator = torch.Generator(device=device).manual_seed(seed)
46
 
47
+ # Truncate both prompt and negative prompt to 77 tokens.
48
+ prompt = truncate_text(prompt, max_tokens=77)
49
+ negative_prompt = truncate_text(negative_prompt, max_tokens=77) if negative_prompt.strip() else ""
50
 
51
+ # Explicitly set pad_token_id to eos_token_id for open-end generation.
52
  image = pipe(
53
  prompt=prompt,
54
  negative_prompt=negative_prompt,
 
57
  width=width,
58
  height=height,
59
  generator=generator,
60
+ pad_token_id=pipe.tokenizer.eos_token_id,
61
  ).images[0]
62
 
63
  return image, seed
64
 
65
+ # Example prompt for testing
66
  examples = [
67
+ "A capybara wearing a suit holding a sign that reads Hello World",
68
  ]
69
 
70
  css = """
 
77
  with gr.Blocks(css=css) as demo:
78
  with gr.Column(elem_id="col-container"):
79
  gr.Markdown(" # [Stable Diffusion 3.5 Large (8B)](https://huggingface.co/stabilityai/stable-diffusion-3.5-large)")
80
+ 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.")
81
  with gr.Row():
82
  prompt = gr.Text(
83
  label="Prompt",
 
86
  placeholder="Enter your prompt",
87
  container=False,
88
  )
 
89
  run_button = gr.Button("Run", scale=0, variant="primary")
 
90
  result = gr.Image(label="Result", show_label=False)
 
91
  with gr.Accordion("Advanced Settings", open=False):
92
  negative_prompt = gr.Text(
93
  label="Negative prompt",
 
95
  placeholder="Enter a negative prompt",
96
  visible=False,
97
  )
 
98
  seed = gr.Slider(
99
  label="Seed",
100
  minimum=0,
 
102
  step=1,
103
  value=0,
104
  )
 
105
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
106
  with gr.Row():
107
  width = gr.Slider(
108
  label="Width",
 
111
  step=32,
112
  value=1024,
113
  )
 
114
  height = gr.Slider(
115
  label="Height",
116
  minimum=512,
 
118
  step=32,
119
  value=1024,
120
  )
 
121
  with gr.Row():
122
  guidance_scale = gr.Slider(
123
  label="Guidance scale",
 
126
  step=0.1,
127
  value=4.5,
128
  )
 
129
  num_inference_steps = gr.Slider(
130
  label="Number of inference steps",
131
  minimum=1,
 
133
  step=1,
134
  value=40,
135
  )
 
136
  gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True, cache_mode="lazy")
137
  gr.on(
138
  triggers=[run_button.click, prompt.submit],
 
152
 
153
  if __name__ == "__main__":
154
  demo.launch()
155
+