el-el-san commited on
Commit
9ca4cfe
1 Parent(s): e26a80f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -16
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import numpy as np
3
  import PIL.Image
@@ -5,10 +6,9 @@ from PIL import Image
5
  import random
6
  from diffusers import ControlNetModel, StableDiffusionXLPipeline, AutoencoderKL
7
  from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
8
- import cv2
9
  import torch
10
  import os
11
- import uuid # UUIDを使用するために追加
12
 
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
 
@@ -41,16 +41,10 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
41
  generator=generator
42
  ).images[0]
43
 
44
- # UUIDを使用して一意のファイル名を生成
45
- unique_filename = f"output_image_{uuid.uuid4().hex}.png"
46
- output_image.save(unique_filename, format="PNG")
47
 
48
- return unique_filename # ファイルパスを返す
49
-
50
- def cleanup(filename):
51
- if os.path.exists(filename):
52
- os.remove(filename)
53
- print(f"Deleted temporary file: {filename}")
54
 
55
  css = """
56
  #col-container {
@@ -77,7 +71,8 @@ with gr.Blocks(css=css) as demo:
77
 
78
  run_button = gr.Button("Run", scale=0)
79
 
80
- result = gr.Image(label="Result", show_label=False, type="filepath")
 
81
 
82
  with gr.Accordion("Advanced Settings", open=False):
83
 
@@ -136,10 +131,6 @@ with gr.Blocks(css=css) as demo:
136
  fn=infer,
137
  inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
138
  outputs=[result]
139
- ).then(
140
- fn=cleanup,
141
- inputs=[result],
142
- outputs=[]
143
  )
144
 
145
  demo.queue().launch()
 
1
+ import space
2
  import gradio as gr
3
  import numpy as np
4
  import PIL.Image
 
6
  import random
7
  from diffusers import ControlNetModel, StableDiffusionXLPipeline, AutoencoderKL
8
  from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
 
9
  import torch
10
  import os
11
+ import uuid
12
 
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
 
 
41
  generator=generator
42
  ).images[0]
43
 
44
+ # PNGに変換する
45
+ output_image = output_image.convert("RGBA")
 
46
 
47
+ return output_image # 画像オブジェクトを直接返す
 
 
 
 
 
48
 
49
  css = """
50
  #col-container {
 
71
 
72
  run_button = gr.Button("Run", scale=0)
73
 
74
+ # show_download_button=True でPNG形式でダウンロードできるようにする
75
+ result = gr.Image(label="Result", show_label=False, type="pil", elem_id="output_image", show_download_button=True)
76
 
77
  with gr.Accordion("Advanced Settings", open=False):
78
 
 
131
  fn=infer,
132
  inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
133
  outputs=[result]
 
 
 
 
134
  )
135
 
136
  demo.queue().launch()