Fix GPU detection by adding module-level GPU request and decorator on root endpoint
Browse files
app.py
CHANGED
|
@@ -190,6 +190,18 @@ def initialize_model() -> None:
|
|
| 190 |
# Space 起動時にバックグラウンドで初期化
|
| 191 |
threading.Thread(target=initialize_model, daemon=True).start()
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
app = FastAPI(
|
| 195 |
title="LLMView Word Tree API",
|
|
@@ -198,18 +210,8 @@ app = FastAPI(
|
|
| 198 |
)
|
| 199 |
|
| 200 |
|
| 201 |
-
@app.on_event("startup")
|
| 202 |
-
async def startup_event():
|
| 203 |
-
"""アプリ起動時の処理(ZeroGPU要求)"""
|
| 204 |
-
if SPACES_AVAILABLE:
|
| 205 |
-
try:
|
| 206 |
-
spaces.GPU()
|
| 207 |
-
print("[SPACE] GPU要求をstartup eventで送信しました")
|
| 208 |
-
except Exception as e:
|
| 209 |
-
print(f"[SPACE] GPU要求エラー: {e}")
|
| 210 |
-
|
| 211 |
-
|
| 212 |
@app.get("/")
|
|
|
|
| 213 |
def root() -> Dict[str, str]:
|
| 214 |
"""簡易案内"""
|
| 215 |
return {
|
|
|
|
| 190 |
# Space 起動時にバックグラウンドで初期化
|
| 191 |
threading.Thread(target=initialize_model, daemon=True).start()
|
| 192 |
|
| 193 |
+
# ZeroGPU対応: モジュールレベルでGPU要求(起動時に検出されるように)
|
| 194 |
+
if SPACES_AVAILABLE:
|
| 195 |
+
try:
|
| 196 |
+
# spaces.GPU() を呼び出してデコレータを取得し、ダミー関数に適用
|
| 197 |
+
gpu_decorator = spaces.GPU()
|
| 198 |
+
@gpu_decorator
|
| 199 |
+
def _gpu_request_dummy():
|
| 200 |
+
"""GPU要求用のダミー関数(Space起動時に検出される)"""
|
| 201 |
+
pass
|
| 202 |
+
print("[SPACE] GPU要求をモジュールレベルで送信しました")
|
| 203 |
+
except Exception as e:
|
| 204 |
+
print(f"[SPACE] GPU要求エラー: {e}")
|
| 205 |
|
| 206 |
app = FastAPI(
|
| 207 |
title="LLMView Word Tree API",
|
|
|
|
| 210 |
)
|
| 211 |
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
@app.get("/")
|
| 214 |
+
@spaces.GPU # ZeroGPU対応: root エンドポイントにも適用して起動時に検出されるように
|
| 215 |
def root() -> Dict[str, str]:
|
| 216 |
"""簡易案内"""
|
| 217 |
return {
|