WatNeru commited on
Commit
3fbc018
·
1 Parent(s): f1d5201

api整備

Browse files
Files changed (1) hide show
  1. app.py +38 -14
app.py CHANGED
@@ -64,6 +64,16 @@ def _set_status(message: str) -> None:
64
  status_message = message
65
 
66
 
 
 
 
 
 
 
 
 
 
 
67
  def initialize_model() -> None:
68
  """モデルを初期化"""
69
  global adapter
@@ -237,9 +247,9 @@ fastapi_app = FastAPI(
237
  )
238
 
239
 
240
- @fastapi_app.get("/")
241
- def root() -> Dict[str, str]:
242
- """簡易案内"""
243
  return {
244
  "message": "LLMView Multi-Model API",
245
  "status_endpoint": "/health",
@@ -250,7 +260,7 @@ def root() -> Dict[str, str]:
250
 
251
  @fastapi_app.get("/health")
252
  def health() -> Dict[str, Any]:
253
- """状態確認"""
254
  with status_lock:
255
  current_status = status_message
256
 
@@ -262,14 +272,26 @@ def health() -> Dict[str, Any]:
262
  }
263
 
264
 
265
- @spaces.GPU # ZeroGPU対応
 
 
 
 
 
 
 
 
 
 
 
266
  @fastapi_app.post("/build_word_tree", response_model=List[WordTreeResponse])
267
- def api_build_word_tree(payload: WordTreeRequest) -> List[WordTreeResponse]:
268
- """単語ツリーを構築(FastAPIエンドポイント)"""
269
  if not payload.prompt_text.strip():
270
  raise HTTPException(status_code=400, detail="prompt_text を入力してください。")
271
 
272
  if adapter is None:
 
273
  with status_lock:
274
  current_status = status_message
275
  raise HTTPException(
@@ -277,6 +299,11 @@ def api_build_word_tree(payload: WordTreeRequest) -> List[WordTreeResponse]:
277
  )
278
 
279
  try:
 
 
 
 
 
280
  results = adapter.build_word_tree(
281
  prompt_text=payload.prompt_text,
282
  root_text=payload.root_text,
@@ -285,18 +312,15 @@ def api_build_word_tree(payload: WordTreeRequest) -> List[WordTreeResponse]:
285
  )
286
 
287
  if not results:
288
- # ダミー結果を返す
289
- results = [
290
- {"text": "候補が生成されませんでした", "probability": 0.0}
291
- ]
292
 
293
  return [WordTreeResponse(**item) for item in results]
294
  except Exception as exc:
295
  import traceback
296
  traceback.print_exc()
297
- raise HTTPException(
298
- status_code=500, detail=f"エラーが発生しました: {exc}"
299
- )
300
 
301
 
302
  # ZeroGPU対応: 起動時に検出されるように、デコレータ付き関数を定義
 
64
  status_message = message
65
 
66
 
67
+ def _get_dummy_results() -> List[WordTreeResponse]:
68
+ """モデルが未準備・異常時に返すダミー候補"""
69
+ dummy_payload = [
70
+ {"text": "[eos]", "probability": 0.8},
71
+ {"text": "#dummy#候補2", "probability": 0.6},
72
+ {"text": "#dummy#候補3", "probability": 0.4},
73
+ ]
74
+ return [WordTreeResponse(**item) for item in dummy_payload]
75
+
76
+
77
  def initialize_model() -> None:
78
  """モデルを初期化"""
79
  global adapter
 
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",
 
260
 
261
  @fastapi_app.get("/health")
262
  def health() -> Dict[str, Any]:
263
+ """状態確認(元のLLMViewと同じ形式)"""
264
  with status_lock:
265
  current_status = status_message
266
 
 
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():
291
  raise HTTPException(status_code=400, detail="prompt_text を入力してください。")
292
 
293
  if adapter is None:
294
+ print("[API] build_word_tree: モデル未準備(adapter is None)")
295
  with status_lock:
296
  current_status = status_message
297
  raise HTTPException(
 
299
  )
300
 
301
  try:
302
+ print(
303
+ f"[API] build_word_tree called: prompt=\n##########################\n{payload.prompt_text}\n##########################\n', "
304
+ f"root=\n%%%%%%%%%%%%%%%%%%%\n{payload.root_text}\n%%%%%%%%%%%%%%%%%%%\n', top_k={payload.top_k}, max_depth={payload.max_depth}"
305
+ )
306
+
307
  results = adapter.build_word_tree(
308
  prompt_text=payload.prompt_text,
309
  root_text=payload.root_text,
 
312
  )
313
 
314
  if not results:
315
+ print("[API] No candidates generated, returning dummy candidates")
316
+ results = _get_dummy_results()
 
 
317
 
318
  return [WordTreeResponse(**item) for item in results]
319
  except Exception as exc:
320
  import traceback
321
  traceback.print_exc()
322
+ print(f"[API] build_word_tree error: {exc}, fallback to dummy results")
323
+ return _get_dummy_results()
 
324
 
325
 
326
  # ZeroGPU対応: 起動時に検出されるように、デコレータ付き関数を定義