educrpg commited on
Commit
b9d7680
Β·
verified Β·
1 Parent(s): 8c62f71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -3,8 +3,7 @@ import numpy as np
3
  import random
4
  import spaces
5
  import torch
6
- from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
7
- from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
8
 
9
  dtype = torch.bfloat16
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -12,7 +11,7 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
12
  pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
13
 
14
  MAX_SEED = np.iinfo(np.int32).max
15
- MAX_IMAGE_SIZE = 2048
16
 
17
  @spaces.GPU(duration=190)
18
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
@@ -20,15 +19,15 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
20
  seed = random.randint(0, MAX_SEED)
21
  generator = torch.Generator().manual_seed(seed)
22
  image = pipe(
23
- prompt = prompt,
24
- width = width,
25
- height = height,
26
- num_inference_steps = num_inference_steps,
27
- generator = generator,
28
  guidance_scale=guidance_scale
29
  ).images[0]
30
  return image, seed
31
-
32
  examples = [
33
  "a tiny astronaut hatching from an egg on the moon",
34
  "a cat holding a sign that says hello world",
@@ -113,18 +112,18 @@ with gr.Blocks(css=css) as demo:
113
  )
114
 
115
  gr.Examples(
116
- examples = examples,
117
- fn = infer,
118
- inputs = [prompt],
119
- outputs = [result, seed],
120
  cache_examples="lazy"
121
  )
122
 
123
  gr.on(
124
  triggers=[run_button.click, prompt.submit],
125
- fn = infer,
126
- inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
127
- outputs = [result, seed]
128
  )
129
 
130
  # Adding image input options at the bottom
@@ -137,18 +136,21 @@ with gr.Blocks(css=css) as demo:
137
 
138
  additional_image_output = gr.Image(label="Selected Image", show_label=False)
139
 
140
- def select_image(uploaded_image, image_url, use_generated=False):
141
  if use_generated:
142
- return result
143
  elif uploaded_image is not None:
144
  return uploaded_image
145
  elif image_url:
146
- return gr.Image.load(image_url)
 
 
 
147
  return None
148
 
149
  # Updated click and change triggers
150
  use_generated_image.click(lambda: select_image(None, None, True), inputs=[], outputs=[additional_image_output])
151
- uploaded_image.change(select_image, inputs=[uploaded_image, image_url], outputs=[additional_image_output])
152
- image_url.submit(select_image, inputs=[uploaded_image, image_url], outputs=[additional_image_output])
153
 
154
  demo.launch()
 
3
  import random
4
  import spaces
5
  import torch
6
+ from diffusers import DiffusionPipeline
 
7
 
8
  dtype = torch.bfloat16
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
11
  pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
12
 
13
  MAX_SEED = np.iinfo(np.int32).max
14
+ MAX_IMAGE_SIZE = 2048
15
 
16
  @spaces.GPU(duration=190)
17
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
 
19
  seed = random.randint(0, MAX_SEED)
20
  generator = torch.Generator().manual_seed(seed)
21
  image = pipe(
22
+ prompt=prompt,
23
+ width=width,
24
+ height=height,
25
+ num_inference_steps=num_inference_steps,
26
+ generator=generator,
27
  guidance_scale=guidance_scale
28
  ).images[0]
29
  return image, seed
30
+
31
  examples = [
32
  "a tiny astronaut hatching from an egg on the moon",
33
  "a cat holding a sign that says hello world",
 
112
  )
113
 
114
  gr.Examples(
115
+ examples=examples,
116
+ fn=infer,
117
+ inputs=[prompt],
118
+ outputs=[result, seed],
119
  cache_examples="lazy"
120
  )
121
 
122
  gr.on(
123
  triggers=[run_button.click, prompt.submit],
124
+ fn=infer,
125
+ inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
126
+ outputs=[result, seed]
127
  )
128
 
129
  # Adding image input options at the bottom
 
136
 
137
  additional_image_output = gr.Image(label="Selected Image", show_label=False)
138
 
139
+ def select_image(uploaded_image, image_url, use_generated):
140
  if use_generated:
141
+ return result.value # Return the value of the generated image
142
  elif uploaded_image is not None:
143
  return uploaded_image
144
  elif image_url:
145
+ try:
146
+ return gr.Image.load(image_url)
147
+ except Exception as e:
148
+ return f"Failed to load image from URL: {e}"
149
  return None
150
 
151
  # Updated click and change triggers
152
  use_generated_image.click(lambda: select_image(None, None, True), inputs=[], outputs=[additional_image_output])
153
+ uploaded_image.change(select_image, inputs=[uploaded_image, image_url, gr.State(False)], outputs=[additional_image_output])
154
+ image_url.submit(select_image, inputs=[uploaded_image, image_url, gr.State(False)], outputs=[additional_image_output])
155
 
156
  demo.launch()