WatNeru commited on
Commit
f1d5201
·
1 Parent(s): 2bdf0a5

fast api added

Browse files
Files changed (2) hide show
  1. app.py +89 -0
  2. requirements.txt +4 -0
app.py CHANGED
@@ -10,6 +10,9 @@ from pathlib import Path
10
  from typing import List, Dict, Any, Optional
11
 
12
  import gradio as gr
 
 
 
13
 
14
  # ZeroGPU対応: spacesパッケージをインポート(デコレータ用)
15
  try:
@@ -41,6 +44,18 @@ status_lock = threading.Lock()
41
  MODEL_TYPE = os.getenv("MODEL_TYPE", "transformers")
42
  HF_MODEL_REPO = os.getenv("HF_MODEL_REPO", "meta-llama/Llama-3.2-3B-Instruct")
43
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  def _set_status(message: str) -> None:
46
  """ステータスメッセージを更新"""
@@ -214,6 +229,76 @@ with gr.Blocks(title="LLMView Multi-Model", theme=gr.themes.Soft()) as demo:
214
  )
215
 
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  # ZeroGPU対応: 起動時に検出されるように、デコレータ付き関数を定義
218
  @spaces.GPU
219
  def _gpu_init_function():
@@ -221,6 +306,10 @@ def _gpu_init_function():
221
  pass
222
 
223
 
 
 
 
 
224
  if __name__ == "__main__":
225
  # Hugging Face Spaces用の設定
226
  # GPU要求を確実に検出させる
 
10
  from typing import List, Dict, Any, Optional
11
 
12
  import gradio as gr
13
+ from fastapi import FastAPI, HTTPException
14
+ from fastapi.responses import JSONResponse
15
+ from pydantic import BaseModel, Field
16
 
17
  # ZeroGPU対応: spacesパッケージをインポート(デコレータ用)
18
  try:
 
44
  MODEL_TYPE = os.getenv("MODEL_TYPE", "transformers")
45
  HF_MODEL_REPO = os.getenv("HF_MODEL_REPO", "meta-llama/Llama-3.2-3B-Instruct")
46
 
47
+ # FastAPI用のリクエスト/レスポンスモデル
48
+ class WordTreeRequest(BaseModel):
49
+ prompt_text: str = Field(..., description="生成に使用するプロンプト")
50
+ root_text: str = Field("", description="任意のルートテキスト")
51
+ top_k: int = Field(5, ge=1, le=50, description="取得する候補数")
52
+ max_depth: int = Field(10, ge=1, le=50, description="探索深さ")
53
+
54
+
55
+ class WordTreeResponse(BaseModel):
56
+ text: str
57
+ probability: float
58
+
59
 
60
  def _set_status(message: str) -> None:
61
  """ステータスメッセージを更新"""
 
229
  )
230
 
231
 
232
+ # FastAPIアプリを作成(外部APIアクセス用)
233
+ fastapi_app = FastAPI(
234
+ title="LLMView Multi-Model API",
235
+ description="LLMView の単語ツリー構築 API。/build_word_tree にPOSTしてください。",
236
+ version="1.0.0",
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",
246
+ "build_endpoint": "/build_word_tree",
247
+ "gradio_ui": "/",
248
+ }
249
+
250
+
251
+ @fastapi_app.get("/health")
252
+ def health() -> Dict[str, Any]:
253
+ """状態確認"""
254
+ with status_lock:
255
+ current_status = status_message
256
+
257
+ return {
258
+ "model_loaded": adapter is not None,
259
+ "status": current_status,
260
+ "model_type": MODEL_TYPE,
261
+ "model_path": HF_MODEL_REPO if MODEL_TYPE == "transformers" else None,
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(
276
+ status_code=503, detail=f"モデル準備中です: {current_status}"
277
+ )
278
+
279
+ try:
280
+ results = adapter.build_word_tree(
281
+ prompt_text=payload.prompt_text,
282
+ root_text=payload.root_text,
283
+ top_k=payload.top_k,
284
+ max_depth=payload.max_depth,
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対応: 起動時に検出されるように、デコレータ付き関数を定義
303
  @spaces.GPU
304
  def _gpu_init_function():
 
306
  pass
307
 
308
 
309
+ # GradioアプリにFastAPIを統合
310
+ demo.fastapi_app = fastapi_app
311
+
312
+
313
  if __name__ == "__main__":
314
  # Hugging Face Spaces用の設定
315
  # GPU要求を確実に検出させる
requirements.txt CHANGED
@@ -6,6 +6,10 @@
6
  # Gradio - Web UI(最新版でセキュリティ脆弱性を修正)
7
  gradio>=4.43.0
8
 
 
 
 
 
9
  # Hugging Face Spaces
10
  spaces
11
 
 
6
  # Gradio - Web UI(最新版でセキュリティ脆弱性を修正)
7
  gradio>=4.43.0
8
 
9
+ # FastAPI - 外部APIアクセス用
10
+ fastapi>=0.111.0
11
+ pydantic>=2.0.0
12
+
13
  # Hugging Face Spaces
14
  spaces
15