Create testdummy.py
Browse files- testdummy.py +46 -0
testdummy.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import multiprocessing
|
3 |
+
import logging
|
4 |
+
from fastapi import FastAPI
|
5 |
+
from pydantic import BaseModel
|
6 |
+
from huggingface_hub import login
|
7 |
+
from sentence_transformers import SentenceTransformer
|
8 |
+
|
9 |
+
# ββββββββββββββββ Logging μ€μ ββββββββββββββββ
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
# ββββββββββββββββ Hugging Face λ‘κ·ΈμΈ ββββββββββββββββ
|
14 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
15 |
+
|
16 |
+
if multiprocessing.current_process().name == "MainProcess":
|
17 |
+
if HF_API_TOKEN and HF_API_TOKEN.startswith("hf_"):
|
18 |
+
logger.info("π Hugging Face API λ‘κ·ΈμΈ μ€ (MainProcess)...")
|
19 |
+
login(token=HF_API_TOKEN)
|
20 |
+
else:
|
21 |
+
logger.warning("β οΈ HF_API_TOKENμ΄ μκ±°λ μλͺ»λ νμμ
λλ€.")
|
22 |
+
|
23 |
+
# ββββββββββββββββ FastAPI μΈμ€ν΄μ€ ββββββββββββββββ
|
24 |
+
app = FastAPI(title="FastAPI Multi-worker Example")
|
25 |
+
|
26 |
+
# ββββββββββββββββ μλ² λ© λͺ¨λΈ lazy-load ββββββββββββββββ
|
27 |
+
embedding_model = None
|
28 |
+
|
29 |
+
def get_embedding_model():
|
30 |
+
global embedding_model
|
31 |
+
if embedding_model is None:
|
32 |
+
logger.info("π¦ μλ² λ© λͺ¨λΈ λ‘λ μ€...")
|
33 |
+
embedding_model = SentenceTransformer("jhgan/ko-sroberta-multitask")
|
34 |
+
logger.info("β
μλ² λ© λͺ¨λΈ λ‘λ μλ£!")
|
35 |
+
return embedding_model
|
36 |
+
|
37 |
+
# ββββββββββββββββ Pydantic λͺ¨λΈ ββββββββββββββββ
|
38 |
+
class Query(BaseModel):
|
39 |
+
text: str
|
40 |
+
|
41 |
+
# ββββββββββββββββ ν
μ€νΈ μλν¬μΈνΈ ββββββββββββββββ
|
42 |
+
@app.post("/encode")
|
43 |
+
def encode_text(query: Query):
|
44 |
+
model = get_embedding_model()
|
45 |
+
embedding = model.encode(query.text).tolist()
|
46 |
+
return {"embedding": embedding}
|