WatNeru commited on
Commit
0dac8e2
·
1 Parent(s): 3fbc018
Files changed (1) hide show
  1. app.py +9 -41
app.py CHANGED
@@ -239,26 +239,16 @@ with gr.Blocks(title="LLMView Multi-Model", theme=gr.themes.Soft()) as demo:
239
  )
240
 
241
 
242
- # FastAPIアプリ作成(外部APIアクセス用)
243
- fastapi_app = FastAPI(
244
- title="LLMView Multi-Model API",
245
- description="LLMView 単語ツリ構築 API。/build_word_tree POSTしてくだい。",
246
- version="1.0.0",
247
- )
248
-
249
-
250
- @fastapi_app.get("/api")
251
- def api_root() -> Dict[str, str]:
252
- """API簡易案内"""
253
- return {
254
- "message": "LLMView Multi-Model API",
255
- "status_endpoint": "/health",
256
- "build_endpoint": "/build_word_tree",
257
- "gradio_ui": "/",
258
- }
259
 
260
 
261
- @fastapi_app.get("/health")
 
 
262
  def health() -> Dict[str, Any]:
263
  """状態確認(元のLLMViewと同じ形式)"""
264
  with status_lock:
@@ -272,19 +262,8 @@ def health() -> Dict[str, Any]:
272
  }
273
 
274
 
275
- @fastapi_app.on_event("startup")
276
- async def startup_event():
277
- """アプリ起動時の処理(GPU要求を確実に検出させる)"""
278
- if SPACES_AVAILABLE:
279
- try:
280
- _gpu_init_function()
281
- print("[SPACE] GPU要求をstartup eventで送信しました")
282
- except Exception as e:
283
- print(f"[SPACE] GPU要求エラー: {e}")
284
-
285
-
286
  @spaces.GPU # ZeroGPU対応: デコレータを先に適用(Space起動時に検出される)
287
- @fastapi_app.post("/build_word_tree", response_model=List[WordTreeResponse])
288
  def build_word_tree(payload: WordTreeRequest) -> List[WordTreeResponse]:
289
  """単語ツリーを構築(元のLLMViewと同じAPI)"""
290
  if not payload.prompt_text.strip():
@@ -323,17 +302,6 @@ def build_word_tree(payload: WordTreeRequest) -> List[WordTreeResponse]:
323
  return _get_dummy_results()
324
 
325
 
326
- # ZeroGPU対応: 起動時に検出されるように、デコレータ付き関数を定義
327
- @spaces.GPU
328
- def _gpu_init_function():
329
- """GPU初期化用のダミー関数(Space起動時に検出される)"""
330
- pass
331
-
332
-
333
- # GradioアプリにFastAPIを統合
334
- demo.fastapi_app = fastapi_app
335
-
336
-
337
  if __name__ == "__main__":
338
  # Hugging Face Spaces用の設定
339
  # GPU要求を確実に検出させる
 
239
  )
240
 
241
 
242
+ # ZeroGPU対応: 起動時に検出されるように、デコレータ付き関数定義
243
+ @spaces.GPU
244
+ def _gpu_init_function():
245
+ """GPU初期化用ダミ関数(Space起動時検出れる)"""
246
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
 
249
+ # GradioアプリのFastAPIインスタンスに直接ルートを追加
250
+ # Gradioアプリは内部でFastAPIインスタンスを持っているので、それに直接ルートを追加
251
+ @demo.app.get("/health")
252
  def health() -> Dict[str, Any]:
253
  """状態確認(元のLLMViewと同じ形式)"""
254
  with status_lock:
 
262
  }
263
 
264
 
 
 
 
 
 
 
 
 
 
 
 
265
  @spaces.GPU # ZeroGPU対応: デコレータを先に適用(Space起動時に検出される)
266
+ @demo.app.post("/build_word_tree", response_model=List[WordTreeResponse])
267
  def build_word_tree(payload: WordTreeRequest) -> List[WordTreeResponse]:
268
  """単語ツリーを構築(元のLLMViewと同じAPI)"""
269
  if not payload.prompt_text.strip():
 
302
  return _get_dummy_results()
303
 
304
 
 
 
 
 
 
 
 
 
 
 
 
305
  if __name__ == "__main__":
306
  # Hugging Face Spaces用の設定
307
  # GPU要求を確実に検出させる