aikobay commited on
Commit
e5962f6
Β·
verified Β·
1 Parent(s): 9281e21

Create testdummy.py

Browse files
Files changed (1) hide show
  1. 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}