aimersion commited on
Commit
ca256e8
1 Parent(s): 6177b55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- import spaces
5
  import torch
6
- import os
7
  import time
8
  from diffusers import DiffusionPipeline
9
 
@@ -17,14 +15,13 @@ except ImportError:
17
  dtype = torch.float16
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
19
 
20
- # Load the diffusion pipeline without requiring an API token
21
  pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
22
 
23
  MAX_SEED = np.iinfo(np.int32).max
24
  MAX_IMAGE_SIZE = 2048
25
 
26
- @spaces.GPU()
27
- def infer(prompt, negative_prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidance_scale=7.5, progress=gr.Progress(track_tqdm=True)):
28
  start_time = time.time()
29
 
30
  if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
@@ -78,38 +75,42 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
78
  placeholder="Enter things to avoid in the image",
79
  lines=2
80
  )
81
- run_button = gr.Button("Generate Image")
82
- with gr.Column(scale=3):
 
83
  result = gr.Image(label="Generated Image")
 
84
 
85
  with gr.Accordion("Advanced Settings", open=False):
86
- with gr.Row():
87
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
88
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
89
  with gr.Row():
90
  width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
91
  height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
 
92
  with gr.Row():
93
  num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=4)
94
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=20.0, step=0.5, value=7.5)
95
 
96
  gr.Examples(
97
  examples=examples,
98
- fn=infer,
99
  inputs=[prompt, negative_prompt],
100
- outputs=[result, seed],
 
101
  cache_examples=True
102
  )
103
 
104
  run_button.click(
105
  fn=infer,
106
  inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
107
- outputs=[result, seed],
108
  )
109
 
110
  gr.Markdown("""
111
  ## Save Your Image
112
- Right-click on the image and select 'Save As' to download the generated image.
113
  """)
114
 
115
- demo.launch()
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
  import torch
 
5
  import time
6
  from diffusers import DiffusionPipeline
7
 
 
15
  dtype = torch.float16
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
 
18
+ # Load the diffusion pipeline
19
  pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
20
 
21
  MAX_SEED = np.iinfo(np.int32).max
22
  MAX_IMAGE_SIZE = 2048
23
 
24
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale, progress=gr.Progress(track_tqdm=True)):
 
25
  start_time = time.time()
26
 
27
  if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
 
75
  placeholder="Enter things to avoid in the image",
76
  lines=2
77
  )
78
+ run_button = gr.Button("Generate Image", variant="primary")
79
+
80
+ with gr.Column(scale=2):
81
  result = gr.Image(label="Generated Image")
82
+ seed_output = gr.Number(label="Seed Used")
83
 
84
  with gr.Accordion("Advanced Settings", open=False):
85
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
86
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
87
+
88
  with gr.Row():
89
  width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
90
  height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
91
+
92
  with gr.Row():
93
  num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=4)
94
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=20.0, step=0.5, value=7.5)
95
 
96
  gr.Examples(
97
  examples=examples,
 
98
  inputs=[prompt, negative_prompt],
99
+ outputs=[result, seed_output],
100
+ fn=infer,
101
  cache_examples=True
102
  )
103
 
104
  run_button.click(
105
  fn=infer,
106
  inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
107
+ outputs=[result, seed_output]
108
  )
109
 
110
  gr.Markdown("""
111
  ## Save Your Image
112
+ Right-click on the generated image and select 'Save image as' to download it.
113
  """)
114
 
115
+ if __name__ == "__main__":
116
+ demo.launch()