update seeding

#7
by ariG23498 HF staff - opened
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,10 +1,10 @@
1
  import gradio as gr
2
  import torch
3
  import spaces
4
-
5
  from huggingface_hub import hf_hub_download
6
  from diffusers import FluxControlPipeline, FluxTransformer2DModel
7
  import numpy as np
 
8
  ####################################
9
  # Load the model(s) on GPU #
10
  ####################################
@@ -17,12 +17,15 @@ pipeline.load_lora_weights(
17
  hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"), adapter_name="hyper-sd"
18
  )
19
  pipeline.set_adapters(["hyper-sd"], adapter_weights=[0.125])
 
20
  MAX_SEED = np.iinfo(np.int32).max
 
21
  def get_seed(randomize_seed: bool, seed: int) -> int:
22
  """
23
  Get the random seed.
24
  """
25
- return np.random.randint(0, MAX_SEED) if randomize_seed else seed
 
26
  #####################################
27
  # The function for our Gradio app #
28
  #####################################
@@ -30,21 +33,25 @@ def get_seed(randomize_seed: bool, seed: int) -> int:
30
  def generate(prompt, input_image, seed, progress=gr.Progress(track_tqdm=True)):
31
  """
32
  Runs the Flux Control pipeline for editing the given `input_image`
33
- with the specified `prompt`. The pipeline is on CPU by default.
34
  """
35
- output_image = pipeline(
36
- control_image=input_image,
37
- prompt=prompt,
38
- guidance_scale=30.,
39
- num_inference_steps=8,
40
- max_sequence_length=512,
41
- height=input_image.height,
42
- width=input_image.width,
43
- generator=torch.manual_seed(seed)
44
- ).images[0]
45
 
46
- return output_image
 
 
 
 
 
 
 
 
 
 
 
47
 
 
48
 
49
  def launch_app():
50
  css = '''
@@ -54,7 +61,6 @@ def launch_app():
54
  gr.Markdown(
55
  """
56
  # Flux Control Editing πŸ–ŒοΈ
57
-
58
  Edit any image with the [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)
59
  [Flux Control edit framework](https://github.com/sayakpaul/flux-image-editing) by [Sayak Paul](https://huggingface.co/sayakpaul).
60
  """
@@ -78,10 +84,10 @@ def launch_app():
78
 
79
  # Connect button to function
80
  generate_button.click(
81
- get_seed,
82
- inputs=[randomize_seed, seed],
83
- outputs=[seed],
84
- ).then(
85
  fn=generate,
86
  inputs=[prompt, input_image, seed],
87
  outputs=[output_image],
@@ -107,7 +113,6 @@ def launch_app():
107
  )
108
  return demo
109
 
110
-
111
  if __name__ == "__main__":
112
  demo = launch_app()
113
  demo.launch()
 
1
  import gradio as gr
2
  import torch
3
  import spaces
 
4
  from huggingface_hub import hf_hub_download
5
  from diffusers import FluxControlPipeline, FluxTransformer2DModel
6
  import numpy as np
7
+
8
  ####################################
9
  # Load the model(s) on GPU #
10
  ####################################
 
17
  hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"), adapter_name="hyper-sd"
18
  )
19
  pipeline.set_adapters(["hyper-sd"], adapter_weights=[0.125])
20
+
21
  MAX_SEED = np.iinfo(np.int32).max
22
+
23
  def get_seed(randomize_seed: bool, seed: int) -> int:
24
  """
25
  Get the random seed.
26
  """
27
+ return int(np.random.randint(0, MAX_SEED) if randomize_seed else seed)
28
+
29
  #####################################
30
  # The function for our Gradio app #
31
  #####################################
 
33
  def generate(prompt, input_image, seed, progress=gr.Progress(track_tqdm=True)):
34
  """
35
  Runs the Flux Control pipeline for editing the given `input_image`
36
+ with the specified `prompt`. The pipeline is on GPU by default.
37
  """
38
+ seed = int(seed) # Ensure seed is an integer
39
+ generator = torch.Generator(device="cuda").manual_seed(seed) # Maintain reproducibility
 
 
 
 
 
 
 
 
40
 
41
+ with progress.tqdm(total=1, desc="Generating Image") as pbar:
42
+ output_image = pipeline(
43
+ control_image=input_image,
44
+ prompt=prompt,
45
+ guidance_scale=30.,
46
+ num_inference_steps=8,
47
+ max_sequence_length=512,
48
+ height=input_image.height,
49
+ width=input_image.width,
50
+ generator=generator # Pass the seeded generator
51
+ ).images[0]
52
+ pbar.update(1) # Update progress bar
53
 
54
+ return output_image
55
 
56
  def launch_app():
57
  css = '''
 
61
  gr.Markdown(
62
  """
63
  # Flux Control Editing πŸ–ŒοΈ
 
64
  Edit any image with the [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)
65
  [Flux Control edit framework](https://github.com/sayakpaul/flux-image-editing) by [Sayak Paul](https://huggingface.co/sayakpaul).
66
  """
 
84
 
85
  # Connect button to function
86
  generate_button.click(
87
+ get_seed,
88
+ inputs=[randomize_seed, seed],
89
+ outputs=[seed],
90
+ ).then(
91
  fn=generate,
92
  inputs=[prompt, input_image, seed],
93
  outputs=[output_image],
 
113
  )
114
  return demo
115
 
 
116
  if __name__ == "__main__":
117
  demo = launch_app()
118
  demo.launch()