Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,33 @@ from diffusers import ControlNetModel, StableDiffusionXLPipeline, AutoencoderKL
|
|
8 |
from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
|
9 |
import torch
|
10 |
import os
|
11 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
pipe = StableDiffusionXLPipeline.from_single_file(
|
16 |
"https://huggingface.co/Laxhar/noob_sdxl_beta/noob_hercules3/checkpoint/checkpoint-e2_s109089.safetensors/checkpoint-e2_s109089.safetensors",
|
17 |
use_safetensors=True,
|
@@ -25,12 +48,15 @@ MAX_IMAGE_SIZE = 1216
|
|
25 |
|
26 |
@spaces.GPU
|
27 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
|
|
|
|
28 |
|
29 |
if randomize_seed:
|
30 |
seed = random.randint(0, MAX_SEED)
|
31 |
|
32 |
generator = torch.Generator().manual_seed(seed)
|
33 |
|
|
|
34 |
output_image = pipe(
|
35 |
prompt=prompt,
|
36 |
negative_prompt=negative_prompt,
|
@@ -41,10 +67,16 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
|
|
41 |
generator=generator
|
42 |
).images[0]
|
43 |
|
44 |
-
#
|
45 |
-
output_image
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
css = """
|
50 |
#col-container {
|
@@ -54,12 +86,12 @@ css = """
|
|
54 |
"""
|
55 |
|
56 |
with gr.Blocks(css=css) as demo:
|
57 |
-
|
58 |
with gr.Column(elem_id="col-container"):
|
59 |
gr.Markdown("""
|
60 |
Text-to-Image Demo
|
61 |
using [Noob SDXL beta model](https://huggingface.co/Laxhar)
|
62 |
""")
|
|
|
63 |
with gr.Row():
|
64 |
prompt = gr.Text(
|
65 |
label="Prompt",
|
@@ -68,21 +100,16 @@ with gr.Blocks(css=css) as demo:
|
|
68 |
placeholder="Enter your prompt",
|
69 |
container=False,
|
70 |
)
|
71 |
-
|
72 |
run_button = gr.Button("Run", scale=0)
|
73 |
|
74 |
-
# PNG形式でダウンロードできるようにfile_formatを指定
|
75 |
result = gr.Image(
|
76 |
label="Result",
|
77 |
show_label=False,
|
78 |
-
type="
|
79 |
-
elem_id="output_image"
|
80 |
-
show_download_button=True,
|
81 |
-
download_filename="output.png"
|
82 |
)
|
83 |
|
84 |
with gr.Accordion("Advanced Settings", open=False):
|
85 |
-
|
86 |
negative_prompt = gr.Text(
|
87 |
label="Negative prompt",
|
88 |
max_lines=1,
|
@@ -140,4 +167,7 @@ with gr.Blocks(css=css) as demo:
|
|
140 |
outputs=[result]
|
141 |
)
|
142 |
|
143 |
-
|
|
|
|
|
|
|
|
8 |
from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
|
9 |
import torch
|
10 |
import os
|
11 |
+
import time
|
12 |
+
import glob
|
13 |
+
|
14 |
+
# 一時ファイルの保存ディレクトリ
|
15 |
+
TEMP_DIR = "temp_images"
|
16 |
+
# 一時ファイルの保持期間(秒)
|
17 |
+
FILE_RETENTION_PERIOD = 3600 # 1時間
|
18 |
|
19 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
20 |
|
21 |
+
# 一時ディレクトリの作成
|
22 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
23 |
+
|
24 |
+
def cleanup_old_files():
|
25 |
+
"""古い一時ファイルを削除する"""
|
26 |
+
current_time = time.time()
|
27 |
+
pattern = os.path.join(TEMP_DIR, "output_*.png")
|
28 |
+
|
29 |
+
for file_path in glob.glob(pattern):
|
30 |
+
try:
|
31 |
+
# ファイルの最終更新時刻を取得
|
32 |
+
file_modified_time = os.path.getmtime(file_path)
|
33 |
+
if current_time - file_modified_time > FILE_RETENTION_PERIOD:
|
34 |
+
os.remove(file_path)
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Error while cleaning up file {file_path}: {e}")
|
37 |
+
|
38 |
pipe = StableDiffusionXLPipeline.from_single_file(
|
39 |
"https://huggingface.co/Laxhar/noob_sdxl_beta/noob_hercules3/checkpoint/checkpoint-e2_s109089.safetensors/checkpoint-e2_s109089.safetensors",
|
40 |
use_safetensors=True,
|
|
|
48 |
|
49 |
@spaces.GPU
|
50 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
51 |
+
# 古い一時ファイルの削除
|
52 |
+
cleanup_old_files()
|
53 |
|
54 |
if randomize_seed:
|
55 |
seed = random.randint(0, MAX_SEED)
|
56 |
|
57 |
generator = torch.Generator().manual_seed(seed)
|
58 |
|
59 |
+
# 画像生成
|
60 |
output_image = pipe(
|
61 |
prompt=prompt,
|
62 |
negative_prompt=negative_prompt,
|
|
|
67 |
generator=generator
|
68 |
).images[0]
|
69 |
|
70 |
+
# RGBモードで保存
|
71 |
+
if output_image.mode != 'RGB':
|
72 |
+
output_image = output_image.convert('RGB')
|
73 |
+
|
74 |
+
# 一時ファイルとして保存
|
75 |
+
timestamp = int(time.time())
|
76 |
+
temp_filename = os.path.join(TEMP_DIR, f"output_{timestamp}.png")
|
77 |
+
output_image.save(temp_filename)
|
78 |
+
|
79 |
+
return temp_filename
|
80 |
|
81 |
css = """
|
82 |
#col-container {
|
|
|
86 |
"""
|
87 |
|
88 |
with gr.Blocks(css=css) as demo:
|
|
|
89 |
with gr.Column(elem_id="col-container"):
|
90 |
gr.Markdown("""
|
91 |
Text-to-Image Demo
|
92 |
using [Noob SDXL beta model](https://huggingface.co/Laxhar)
|
93 |
""")
|
94 |
+
|
95 |
with gr.Row():
|
96 |
prompt = gr.Text(
|
97 |
label="Prompt",
|
|
|
100 |
placeholder="Enter your prompt",
|
101 |
container=False,
|
102 |
)
|
|
|
103 |
run_button = gr.Button("Run", scale=0)
|
104 |
|
|
|
105 |
result = gr.Image(
|
106 |
label="Result",
|
107 |
show_label=False,
|
108 |
+
type="filepath",
|
109 |
+
elem_id="output_image"
|
|
|
|
|
110 |
)
|
111 |
|
112 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
113 |
negative_prompt = gr.Text(
|
114 |
label="Negative prompt",
|
115 |
max_lines=1,
|
|
|
167 |
outputs=[result]
|
168 |
)
|
169 |
|
170 |
+
# 起動時に古いファイルを削除
|
171 |
+
cleanup_old_files()
|
172 |
+
|
173 |
+
demo.queue().launch()
|