Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -91,31 +91,6 @@ def is_first_time_load():
|
|
91 |
return True
|
92 |
return False
|
93 |
|
94 |
-
# スタートアップ時のGPUリソース確保
|
95 |
-
if IN_HF_SPACE and 'spaces' in globals() and not cpu_fallback_mode:
|
96 |
-
# スタートアップ時にGPUリソースを確保するダミー関数
|
97 |
-
@spaces.GPU(duration=300)
|
98 |
-
def reserve_gpu_resource():
|
99 |
-
"""スタートアップ時にGPUリソースを確保するダミー関数"""
|
100 |
-
print("GPU resources reserved for the application")
|
101 |
-
# 実際にはここで小さなGPU操作を行ってリソースを確保
|
102 |
-
if torch.cuda.is_available():
|
103 |
-
try:
|
104 |
-
test_tensor = torch.zeros(1, device='cuda')
|
105 |
-
test_tensor = test_tensor + 1
|
106 |
-
del test_tensor
|
107 |
-
torch.cuda.empty_cache()
|
108 |
-
return True
|
109 |
-
except Exception as e:
|
110 |
-
print(f"GPU予約中にエラー: {e}")
|
111 |
-
global cpu_fallback_mode
|
112 |
-
cpu_fallback_mode = True
|
113 |
-
return False
|
114 |
-
return False
|
115 |
-
|
116 |
-
# アプリ起動前にGPUリソースを確保(最小限の操作のみ)
|
117 |
-
reserve_gpu_resource()
|
118 |
-
|
119 |
|
120 |
# GPU制限を超えたかどうかを確認する関数
|
121 |
def check_gpu_quota_exceeded():
|
@@ -1290,7 +1265,7 @@ def worker_with_temp_files(
|
|
1290 |
|
1291 |
# 統合版プロセス関数 - GPU/CPU両方に対応(Spaces環境向け改良版)
|
1292 |
if IN_HF_SPACE and 'spaces' in globals():
|
1293 |
-
@spaces.GPU(duration=
|
1294 |
def process_with_temp(image_mask_dict, lora_multiplier=1.0):
|
1295 |
"""一時ファイルを使用する処理メインフロー(GPU/CPU対応)"""
|
1296 |
global stream, cpu_fallback_mode
|
@@ -1590,8 +1565,32 @@ else:
|
|
1590 |
cleanup_temp_files(temp_dir)
|
1591 |
raise e
|
1592 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1593 |
# 処理中止・クリーンアップ関数(GPU/CPU共通)
|
1594 |
def end_process_with_cleanup():
|
|
|
1595 |
"""処理を中止し、一時ファイルを削除する"""
|
1596 |
global stream
|
1597 |
|
@@ -1640,7 +1639,7 @@ with block:
|
|
1640 |
|
1641 |
with gr.Column():
|
1642 |
preview_image = gr.Image(label="生成プレビュー", visible=False)
|
1643 |
-
result_frame = gr.Image(label="生成結果", visible=False, type="pil",height="60vh")
|
1644 |
|
1645 |
progress_desc = gr.Markdown("", elem_classes="no-generating-animation")
|
1646 |
progress_bar = gr.HTML("", elem_classes="no-generating-animation")
|
|
|
91 |
return True
|
92 |
return False
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
# GPU制限を超えたかどうかを確認する関数
|
96 |
def check_gpu_quota_exceeded():
|
|
|
1265 |
|
1266 |
# 統合版プロセス関数 - GPU/CPU両方に対応(Spaces環境向け改良版)
|
1267 |
if IN_HF_SPACE and 'spaces' in globals():
|
1268 |
+
@spaces.GPU(duration=120)
|
1269 |
def process_with_temp(image_mask_dict, lora_multiplier=1.0):
|
1270 |
"""一時ファイルを使用する処理メインフロー(GPU/CPU対応)"""
|
1271 |
global stream, cpu_fallback_mode
|
|
|
1565 |
cleanup_temp_files(temp_dir)
|
1566 |
raise e
|
1567 |
|
1568 |
+
|
1569 |
+
# 処理終了時に明示的にGPUメモリを解放
|
1570 |
+
def cleanup_gpu_resources():
|
1571 |
+
"""GPUリソースを明示的に解放する"""
|
1572 |
+
global models
|
1573 |
+
|
1574 |
+
# モデルを全てCPUに移動
|
1575 |
+
for model_name, model in models.items():
|
1576 |
+
try:
|
1577 |
+
if model is not None and hasattr(model, 'to') and callable(model.to):
|
1578 |
+
model.to('cpu')
|
1579 |
+
print(f"{model_name}をCPUに移動しました")
|
1580 |
+
except Exception as e:
|
1581 |
+
print(f"{model_name}のCPU移動中にエラー: {e}")
|
1582 |
+
|
1583 |
+
# キャッシュクリア
|
1584 |
+
try:
|
1585 |
+
torch.cuda.empty_cache()
|
1586 |
+
gc.collect()
|
1587 |
+
print("GPUキャッシュをクリアしました")
|
1588 |
+
except Exception as e:
|
1589 |
+
print(f"GPUキャッシュクリア中にエラー: {e}")
|
1590 |
+
|
1591 |
# 処理中止・クリーンアップ関数(GPU/CPU共通)
|
1592 |
def end_process_with_cleanup():
|
1593 |
+
cleanup_gpu_resources()
|
1594 |
"""処理を中止し、一時ファイルを削除する"""
|
1595 |
global stream
|
1596 |
|
|
|
1639 |
|
1640 |
with gr.Column():
|
1641 |
preview_image = gr.Image(label="生成プレビュー", visible=False)
|
1642 |
+
result_frame = gr.Image(label="生成結果", visible=False, type="pil", height="60vh")
|
1643 |
|
1644 |
progress_desc = gr.Markdown("", elem_classes="no-generating-animation")
|
1645 |
progress_bar = gr.HTML("", elem_classes="no-generating-animation")
|