nirajandhakal commited on
Commit
77847a2
·
verified ·
1 Parent(s): 5617b18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py CHANGED
@@ -4,6 +4,8 @@ import random
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
 
 
7
 
8
  dtype = torch.bfloat16
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -28,6 +30,12 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
28
  ).images[0]
29
  return image, seed
30
 
 
 
 
 
 
 
31
  examples = [
32
  "a galaxy swirling with vibrant blue and purple hues",
33
  "a futuristic cityscape under a dark sky",
@@ -153,6 +161,21 @@ with gr.Blocks(css=css) as demo:
153
  value=28,
154
  )
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  gr.Examples(
157
  examples=examples,
158
  fn=infer,
@@ -161,6 +184,14 @@ with gr.Blocks(css=css) as demo:
161
  cache_examples="lazy"
162
  )
163
 
 
 
 
 
 
 
 
 
164
  gr.on(
165
  triggers=[run_button.click, prompt.submit],
166
  fn=infer,
 
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
7
+ from PIL import Image
8
+ import io
9
 
10
  dtype = torch.bfloat16
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
30
  ).images[0]
31
  return image, seed
32
 
33
+ def download_image(image, file_format):
34
+ img_byte_arr = io.BytesIO()
35
+ image.save(img_byte_arr, format=file_format)
36
+ img_byte_arr = img_byte_arr.getvalue()
37
+ return img_byte_arr
38
+
39
  examples = [
40
  "a galaxy swirling with vibrant blue and purple hues",
41
  "a futuristic cityscape under a dark sky",
 
161
  value=28,
162
  )
163
 
164
+ download_format = gr.Radio(
165
+ label="Download Format",
166
+ choices=["PNG", "JPEG", "SVG"],
167
+ value="PNG",
168
+ type="value",
169
+ )
170
+
171
+ download_button = gr.Button("Download Image")
172
+
173
+ download_button.click(
174
+ fn=download_image,
175
+ inputs=[result, download_format],
176
+ outputs=gr.File(label="Download"),
177
+ )
178
+
179
  gr.Examples(
180
  examples=examples,
181
  fn=infer,
 
184
  cache_examples="lazy"
185
  )
186
 
187
+ share_button = gr.Button("Share on Twitter")
188
+ share_button.click(
189
+ fn=lambda img: f"https://twitter.com/intent/post?url={img}",
190
+ inputs=[result],
191
+ outputs=None,
192
+ _js="(img) => window.open(img, '_blank')"
193
+ )
194
+
195
  gr.on(
196
  triggers=[run_button.click, prompt.submit],
197
  fn=infer,