|
import os |
|
import multiprocessing |
|
import logging |
|
from fastapi import FastAPI |
|
from pydantic import BaseModel |
|
from huggingface_hub import login |
|
from sentence_transformers import SentenceTransformer |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
HF_API_TOKEN = os.getenv("HF_API_TOKEN") |
|
|
|
if multiprocessing.current_process().name == "MainProcess": |
|
if HF_API_TOKEN and HF_API_TOKEN.startswith("hf_"): |
|
logger.info("π Hugging Face API λ‘κ·ΈμΈ μ€ (MainProcess)...") |
|
login(token=HF_API_TOKEN) |
|
else: |
|
logger.warning("β οΈ HF_API_TOKENμ΄ μκ±°λ μλͺ»λ νμμ
λλ€.") |
|
|
|
|
|
app = FastAPI(title="FastAPI Multi-worker Example") |
|
|
|
|
|
embedding_model = None |
|
|
|
def get_embedding_model(): |
|
global embedding_model |
|
if embedding_model is None: |
|
logger.info("π¦ μλ² λ© λͺ¨λΈ λ‘λ μ€...") |
|
embedding_model = SentenceTransformer("jhgan/ko-sroberta-multitask") |
|
logger.info("β
μλ² λ© λͺ¨λΈ λ‘λ μλ£!") |
|
return embedding_model |
|
|
|
|
|
class Query(BaseModel): |
|
text: str |
|
|
|
|
|
@app.post("/encode") |
|
def encode_text(query: Query): |
|
model = get_embedding_model() |
|
embedding = model.encode(query.text).tolist() |
|
return {"embedding": embedding} |
|
|