Lazy-import numpy/onnxruntime/transformers: fix slow startup (Starting 고착)
Browse files
app.py
CHANGED
|
@@ -12,18 +12,19 @@ import time
|
|
| 12 |
from pathlib import Path
|
| 13 |
from typing import Optional
|
| 14 |
|
| 15 |
-
import numpy as np
|
| 16 |
-
import onnxruntime as ort
|
| 17 |
from fastapi import FastAPI, HTTPException
|
| 18 |
from fastapi.middleware.cors import CORSMiddleware
|
| 19 |
from fastapi.responses import FileResponse, StreamingResponse
|
| 20 |
from fastapi.staticfiles import StaticFiles
|
| 21 |
-
from huggingface_hub import snapshot_download
|
| 22 |
from pydantic import BaseModel
|
| 23 |
-
from transformers import AutoTokenizer
|
| 24 |
|
| 25 |
MODEL_REPO = os.getenv("MODEL_REPO", "onnx-community/EXAONE-3.5-2.4B-Instruct")
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
NUM_LAYERS = 30
|
| 28 |
NUM_KV_HEADS = 8
|
| 29 |
HEAD_DIM = 80
|
|
@@ -37,9 +38,6 @@ app.add_middleware(
|
|
| 37 |
)
|
| 38 |
|
| 39 |
|
| 40 |
-
# ============================================================
|
| 41 |
-
# 모델 (gen.py의 Generator와 동일 로직)
|
| 42 |
-
# ============================================================
|
| 43 |
class Generator:
|
| 44 |
def __init__(self):
|
| 45 |
self.tokenizer = None
|
|
@@ -50,6 +48,14 @@ class Generator:
|
|
| 50 |
self.status = "initializing"
|
| 51 |
|
| 52 |
def load(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
self.status = "downloading model"
|
| 55 |
self.model_dir = Path(snapshot_download(
|
|
|
|
| 12 |
from pathlib import Path
|
| 13 |
from typing import Optional
|
| 14 |
|
|
|
|
|
|
|
| 15 |
from fastapi import FastAPI, HTTPException
|
| 16 |
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
from fastapi.responses import FileResponse, StreamingResponse
|
| 18 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 19 |
from pydantic import BaseModel
|
|
|
|
| 20 |
|
| 21 |
MODEL_REPO = os.getenv("MODEL_REPO", "onnx-community/EXAONE-3.5-2.4B-Instruct")
|
| 22 |
+
|
| 23 |
+
# 무거운 패키지는 generator.load() 안에서 지연 import
|
| 24 |
+
# (uvicorn이 먼저 뜨고 나서 백그라운드 스레드가 로딩)
|
| 25 |
+
np = None
|
| 26 |
+
ort = None
|
| 27 |
+
|
| 28 |
NUM_LAYERS = 30
|
| 29 |
NUM_KV_HEADS = 8
|
| 30 |
HEAD_DIM = 80
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
class Generator:
|
| 42 |
def __init__(self):
|
| 43 |
self.tokenizer = None
|
|
|
|
| 48 |
self.status = "initializing"
|
| 49 |
|
| 50 |
def load(self):
|
| 51 |
+
import numpy as _np
|
| 52 |
+
import onnxruntime as _ort
|
| 53 |
+
from huggingface_hub import snapshot_download
|
| 54 |
+
from transformers import AutoTokenizer
|
| 55 |
+
global np, ort
|
| 56 |
+
np = _np
|
| 57 |
+
ort = _ort
|
| 58 |
+
|
| 59 |
try:
|
| 60 |
self.status = "downloading model"
|
| 61 |
self.model_dir = Path(snapshot_download(
|