SeedOfEvil commited on
Commit
8ccbda4
·
verified ·
1 Parent(s): 2f73345

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -3,8 +3,8 @@ import numpy as np
3
  import random
4
  import spaces
5
  import torch
6
- from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
7
- from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
8
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
9
 
10
  dtype = torch.bfloat16
@@ -18,10 +18,20 @@ torch.cuda.empty_cache()
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 2048
20
 
 
21
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
22
 
23
  @spaces.GPU(duration=75)
24
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
  generator = torch.Generator().manual_seed(seed)
@@ -36,7 +46,7 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
36
  output_type="pil",
37
  good_vae=good_vae,
38
  ):
39
- yield img, seed
40
 
41
  examples = [
42
  "a tiny astronaut hatching from an egg on the moon",
@@ -44,7 +54,7 @@ examples = [
44
  "an anime illustration of a wiener schnitzel",
45
  ]
46
 
47
- css="""
48
  #col-container {
49
  margin: 0 auto;
50
  max-width: 520px;
@@ -60,7 +70,6 @@ with gr.Blocks(css=css) as demo:
60
  """)
61
 
62
  with gr.Row():
63
-
64
  prompt = gr.Text(
65
  label="Prompt",
66
  show_label=False,
@@ -68,13 +77,11 @@ with gr.Blocks(css=css) as demo:
68
  placeholder="Enter your prompt",
69
  container=False,
70
  )
71
-
72
  run_button = gr.Button("Run", scale=0)
73
 
74
  result = gr.Image(label="Result", show_label=False)
75
 
76
  with gr.Accordion("Advanced Settings", open=False):
77
-
78
  seed = gr.Slider(
79
  label="Seed",
80
  minimum=0,
@@ -82,11 +89,9 @@ with gr.Blocks(css=css) as demo:
82
  step=1,
83
  value=0,
84
  )
85
-
86
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
87
 
88
  with gr.Row():
89
-
90
  width = gr.Slider(
91
  label="Width",
92
  minimum=256,
@@ -94,7 +99,6 @@ with gr.Blocks(css=css) as demo:
94
  step=32,
95
  value=1024,
96
  )
97
-
98
  height = gr.Slider(
99
  label="Height",
100
  minimum=256,
@@ -104,7 +108,6 @@ with gr.Blocks(css=css) as demo:
104
  )
105
 
106
  with gr.Row():
107
-
108
  guidance_scale = gr.Slider(
109
  label="Guidance Scale",
110
  minimum=1,
@@ -112,7 +115,6 @@ with gr.Blocks(css=css) as demo:
112
  step=0.1,
113
  value=3.5,
114
  )
115
-
116
  num_inference_steps = gr.Slider(
117
  label="Number of inference steps",
118
  minimum=1,
@@ -122,18 +124,18 @@ with gr.Blocks(css=css) as demo:
122
  )
123
 
124
  gr.Examples(
125
- examples = examples,
126
- fn = infer,
127
- inputs = [prompt],
128
- outputs = [result, seed],
129
  cache_examples="lazy"
130
  )
131
 
132
  gr.on(
133
  triggers=[run_button.click, prompt.submit],
134
- fn = infer,
135
- inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
136
- outputs = [result, seed]
137
  )
138
 
139
- demo.launch()
 
3
  import random
4
  import spaces
5
  import torch
6
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
7
+ from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
8
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
9
 
10
  dtype = torch.bfloat16
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 2048
20
 
21
+ # Bind the live preview helper function to the pipe
22
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
23
 
24
  @spaces.GPU(duration=75)
25
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
26
+ # --- Fix: Truncate prompt to allowed token length ---
27
+ # Use the pipeline's tokenizer to tokenize the prompt.
28
+ tokenizer = pipe.tokenizer
29
+ max_length = tokenizer.model_max_length # Typically 77 for CLIP
30
+ tokens = tokenizer.tokenize(prompt)
31
+ if len(tokens) > max_length:
32
+ prompt = tokenizer.decode(tokenizer.encode(prompt, truncation=True, max_length=max_length), skip_special_tokens=True)
33
+ # -------------------------------------------------------
34
+
35
  if randomize_seed:
36
  seed = random.randint(0, MAX_SEED)
37
  generator = torch.Generator().manual_seed(seed)
 
46
  output_type="pil",
47
  good_vae=good_vae,
48
  ):
49
+ yield img, seed
50
 
51
  examples = [
52
  "a tiny astronaut hatching from an egg on the moon",
 
54
  "an anime illustration of a wiener schnitzel",
55
  ]
56
 
57
+ css = """
58
  #col-container {
59
  margin: 0 auto;
60
  max-width: 520px;
 
70
  """)
71
 
72
  with gr.Row():
 
73
  prompt = gr.Text(
74
  label="Prompt",
75
  show_label=False,
 
77
  placeholder="Enter your prompt",
78
  container=False,
79
  )
 
80
  run_button = gr.Button("Run", scale=0)
81
 
82
  result = gr.Image(label="Result", show_label=False)
83
 
84
  with gr.Accordion("Advanced Settings", open=False):
 
85
  seed = gr.Slider(
86
  label="Seed",
87
  minimum=0,
 
89
  step=1,
90
  value=0,
91
  )
 
92
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
93
 
94
  with gr.Row():
 
95
  width = gr.Slider(
96
  label="Width",
97
  minimum=256,
 
99
  step=32,
100
  value=1024,
101
  )
 
102
  height = gr.Slider(
103
  label="Height",
104
  minimum=256,
 
108
  )
109
 
110
  with gr.Row():
 
111
  guidance_scale = gr.Slider(
112
  label="Guidance Scale",
113
  minimum=1,
 
115
  step=0.1,
116
  value=3.5,
117
  )
 
118
  num_inference_steps = gr.Slider(
119
  label="Number of inference steps",
120
  minimum=1,
 
124
  )
125
 
126
  gr.Examples(
127
+ examples=examples,
128
+ fn=infer,
129
+ inputs=[prompt],
130
+ outputs=[result, seed],
131
  cache_examples="lazy"
132
  )
133
 
134
  gr.on(
135
  triggers=[run_button.click, prompt.submit],
136
+ fn=infer,
137
+ inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
138
+ outputs=[result, seed]
139
  )
140
 
141
+ demo.launch()