aimersion commited on
Commit
d277227
1 Parent(s): 35d32f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -22
app.py CHANGED
@@ -3,7 +3,6 @@ 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
 
@@ -24,7 +23,7 @@ MAX_SEED = np.iinfo(np.int32).max
24
  MAX_IMAGE_SIZE = 2048
25
 
26
  @spaces.GPU()
27
- def infer(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:
@@ -32,11 +31,13 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_in
32
 
33
  if randomize_seed:
34
  seed = random.randint(0, MAX_SEED)
35
- generator = torch.Generator(device=device).manual_seed(seed) # Ensure generator is on the correct device
36
 
37
  try:
 
38
  image = pipe(
39
  prompt=prompt,
 
40
  width=width,
41
  height=height,
42
  num_inference_steps=num_inference_steps,
@@ -47,12 +48,11 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_in
47
  print(f"Error generating image: {e}")
48
  return None, seed, f"Error: {str(e)}"
49
 
50
- # Check if it took too long
51
  if time.time() - start_time > 60: # 60 seconds timeout
52
  return None, seed, "Image generation took too long and was cancelled."
53
 
54
  return image, seed, None
55
-
56
  examples = [
57
  "a tiny astronaut hatching from an egg on the moon",
58
  "a cat holding a sign that says hello world",
@@ -62,29 +62,47 @@ examples = [
62
  css = """
63
  #col-container {
64
  margin: 0 auto;
65
- max-width: 520px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  }
67
  """
68
 
69
  with gr.Blocks(css=css) as demo:
70
-
71
  with gr.Column(elem_id="col-container"):
72
- gr.Markdown(f"""# Custom Image Creator
73
- 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
74
- [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1)]
75
- """)
76
 
77
- with gr.Row():
78
- prompt = gr.Textbox(
79
- label="Prompt",
80
- show_label=False,
81
- max_lines=1,
82
- placeholder="Enter your prompt",
83
- container=False,
84
- )
85
- run_button = gr.Button("Run", scale=0)
 
 
 
 
86
 
87
- result = gr.Image(label="Result", show_label=False)
88
 
89
  with gr.Accordion("Advanced Settings", open=False):
90
  seed = gr.Slider(
@@ -93,6 +111,7 @@ with gr.Blocks(css=css) as demo:
93
  maximum=MAX_SEED,
94
  step=1,
95
  value=0,
 
96
  )
97
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
 
@@ -103,6 +122,7 @@ with gr.Blocks(css=css) as demo:
103
  maximum=MAX_IMAGE_SIZE,
104
  step=32,
105
  value=1024,
 
106
  )
107
  height = gr.Slider(
108
  label="Height",
@@ -110,6 +130,7 @@ with gr.Blocks(css=css) as demo:
110
  maximum=MAX_IMAGE_SIZE,
111
  step=32,
112
  value=1024,
 
113
  )
114
 
115
  with gr.Row():
@@ -119,6 +140,7 @@ with gr.Blocks(css=css) as demo:
119
  maximum=50,
120
  step=1,
121
  value=4,
 
122
  )
123
  guidance_scale = gr.Slider(
124
  label="Guidance Scale",
@@ -126,6 +148,7 @@ with gr.Blocks(css=css) as demo:
126
  maximum=20.0,
127
  step=0.5,
128
  value=7.5,
 
129
  )
130
 
131
  gr.Examples(
@@ -138,7 +161,7 @@ with gr.Blocks(css=css) as demo:
138
 
139
  run_button.click(
140
  fn=infer,
141
- inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
142
  outputs=[result, seed],
143
  )
144
 
 
3
  import random
4
  import spaces
5
  import torch
 
6
  import time
7
  from diffusers import DiffusionPipeline
8
 
 
23
  MAX_IMAGE_SIZE = 2048
24
 
25
  @spaces.GPU()
26
+ def infer(prompt, negative_prompt=None, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidance_scale=7.5, progress=gr.Progress(track_tqdm=True)):
27
  start_time = time.time()
28
 
29
  if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
 
31
 
32
  if randomize_seed:
33
  seed = random.randint(0, MAX_SEED)
34
+ generator = torch.Generator(device=device).manual_seed(seed)
35
 
36
  try:
37
+ # Include negative prompts in the diffusion pipeline call
38
  image = pipe(
39
  prompt=prompt,
40
+ negative_prompt=negative_prompt, # Using the negative prompt here
41
  width=width,
42
  height=height,
43
  num_inference_steps=num_inference_steps,
 
48
  print(f"Error generating image: {e}")
49
  return None, seed, f"Error: {str(e)}"
50
 
 
51
  if time.time() - start_time > 60: # 60 seconds timeout
52
  return None, seed, "Image generation took too long and was cancelled."
53
 
54
  return image, seed, None
55
+
56
  examples = [
57
  "a tiny astronaut hatching from an egg on the moon",
58
  "a cat holding a sign that says hello world",
 
62
  css = """
63
  #col-container {
64
  margin: 0 auto;
65
+ max-width: 640px;
66
+ padding: 20px;
67
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
68
+ border-radius: 10px;
69
+ background-color: #f8f9fa;
70
+ }
71
+ #run-button {
72
+ background-color: #007bff;
73
+ color: white;
74
+ border: none;
75
+ padding: 10px 20px;
76
+ font-size: 16px;
77
+ border-radius: 5px;
78
+ }
79
+ #run-button:hover {
80
+ background-color: #0056b3;
81
  }
82
  """
83
 
84
  with gr.Blocks(css=css) as demo:
 
85
  with gr.Column(elem_id="col-container"):
86
+ gr.Markdown("""
87
+ # Custom Image Creator
88
+ A 12B param rectified flow transformer from [FLUX.1](https://blackforestlabs.ai/) for 4-step generation.
89
+ """, elem_id="title")
90
 
91
+ prompt = gr.Textbox(
92
+ label="Prompt",
93
+ show_label=False,
94
+ max_lines=1,
95
+ placeholder="Enter your prompt...",
96
+ )
97
+ negative_prompt = gr.Textbox(
98
+ label="Negative Prompt",
99
+ show_label=False,
100
+ max_lines=1,
101
+ placeholder="Enter negative prompts (what to avoid)...",
102
+ )
103
+ run_button = gr.Button("Run", elem_id="run-button")
104
 
105
+ result = gr.Image(label="Result", show_label=False, interactive=True)
106
 
107
  with gr.Accordion("Advanced Settings", open=False):
108
  seed = gr.Slider(
 
111
  maximum=MAX_SEED,
112
  step=1,
113
  value=0,
114
+ tooltip="Seed value for reproducibility. Randomize for unique results."
115
  )
116
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
117
 
 
122
  maximum=MAX_IMAGE_SIZE,
123
  step=32,
124
  value=1024,
125
+ tooltip="Adjust the width of the generated image."
126
  )
127
  height = gr.Slider(
128
  label="Height",
 
130
  maximum=MAX_IMAGE_SIZE,
131
  step=32,
132
  value=1024,
133
+ tooltip="Adjust the height of the generated image."
134
  )
135
 
136
  with gr.Row():
 
140
  maximum=50,
141
  step=1,
142
  value=4,
143
+ tooltip="Controls the quality and coherence of the output."
144
  )
145
  guidance_scale = gr.Slider(
146
  label="Guidance Scale",
 
148
  maximum=20.0,
149
  step=0.5,
150
  value=7.5,
151
+ tooltip="Higher values result in outputs closer to the prompt."
152
  )
153
 
154
  gr.Examples(
 
161
 
162
  run_button.click(
163
  fn=infer,
164
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
165
  outputs=[result, seed],
166
  )
167