fastapi-stock-api / embedding_module.py
Leesn465's picture
FastAPI for Hugging Face Space: initial setup and files
748bd71
raw
history blame
722 Bytes
import numpy as np
import os
from gensim.models import KeyedVectors
MODEL_PATH_VEC = "ko.vec"
# 모델 로딩
if os.path.exists(MODEL_PATH_VEC):
print("🔁 Word2Vec 텍스트 모델 로드 중...")
model = KeyedVectors.load_word2vec_format(MODEL_PATH_VEC, binary=False)
print("✅ Word2Vec 모델 로드 완료")
else:
raise FileNotFoundError("❌ 'ko.vec' 파일을 찾을 수 없습니다.")
def embed_keywords(keywords: list[str]) -> np.ndarray:
"""
키워드 리스트를 벡터로 변환하고 평균 벡터 반환
"""
vectors = [model[word] for word in keywords if word in model]
if not vectors:
return np.zeros(model.vector_size)
return np.mean(vectors, axis=0)