Spaces:
Sleeping
Sleeping
ktsn-ud commited on
Commit ·
b57e23d
1
Parent(s): be70c91
cc.ja.300.binのダウンロードタイミングをDockerビルド時に変更
Browse files- api/main.py +0 -29
- scripts/0_download_data.py +38 -0
- scripts/build_all.py +6 -1
api/main.py
CHANGED
|
@@ -11,7 +11,6 @@ from pydantic import BaseModel
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
from utils.logger import setup_logger
|
| 14 |
-
from utils.json import get_file_path_from_config
|
| 15 |
from api.search.engine import SearchEngine
|
| 16 |
import schemas.projects as schema_projects
|
| 17 |
|
|
@@ -36,39 +35,11 @@ async def get_api_key(key: str = Security(api_key_header)):
|
|
| 36 |
engine = SearchEngine()
|
| 37 |
|
| 38 |
|
| 39 |
-
def download_embeddings() -> None:
|
| 40 |
-
"""Hugging Face Datasets から Embeddings をダウンロードする"""
|
| 41 |
-
from huggingface_hub import hf_hub_download
|
| 42 |
-
|
| 43 |
-
# フォルダがなかったら新規作成
|
| 44 |
-
target_dir = "resources/embeddings"
|
| 45 |
-
os.makedirs(target_dir, exist_ok=True)
|
| 46 |
-
|
| 47 |
-
# ダウンロード
|
| 48 |
-
path = hf_hub_download(
|
| 49 |
-
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
|
| 50 |
-
repo_type="dataset",
|
| 51 |
-
filename="cc.ja.300.bin",
|
| 52 |
-
local_dir=target_dir,
|
| 53 |
-
local_dir_use_symlinks=False,
|
| 54 |
-
token=os.getenv("HF_TOKEN"),
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
log.info(f"Embeddings downloaded: {path}")
|
| 58 |
-
return
|
| 59 |
-
|
| 60 |
-
|
| 61 |
@asynccontextmanager
|
| 62 |
async def lifespan(app: FastAPI):
|
| 63 |
"""アプリケーションの起動時と終了時に実行されるコード"""
|
| 64 |
# --- 起動時処理 ---
|
| 65 |
log.info("Initializing search engine and loading assets...")
|
| 66 |
-
load_dotenv()
|
| 67 |
-
if os.path.exists(get_file_path_from_config("embeddings.fasttext_bin")):
|
| 68 |
-
log.info("FastText embeddings found locally.")
|
| 69 |
-
else:
|
| 70 |
-
log.info("FastText embeddings not found locally. Downloading...")
|
| 71 |
-
download_embeddings()
|
| 72 |
engine.initialize()
|
| 73 |
log.info("Initialization complete.")
|
| 74 |
yield
|
|
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
from utils.logger import setup_logger
|
|
|
|
| 14 |
from api.search.engine import SearchEngine
|
| 15 |
import schemas.projects as schema_projects
|
| 16 |
|
|
|
|
| 35 |
engine = SearchEngine()
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
@asynccontextmanager
|
| 39 |
async def lifespan(app: FastAPI):
|
| 40 |
"""アプリケーションの起動時と終了時に実行されるコード"""
|
| 41 |
# --- 起動時処理 ---
|
| 42 |
log.info("Initializing search engine and loading assets...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
engine.initialize()
|
| 44 |
log.info("Initialization complete.")
|
| 45 |
yield
|
scripts/0_download_data.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from utils.logger import setup_logger
|
| 4 |
+
from utils.json import get_file_path_from_config
|
| 5 |
+
|
| 6 |
+
log = setup_logger(__name__)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def download_embeddings() -> None:
|
| 10 |
+
"""Hugging Face Datasets から Embeddings をダウンロードする"""
|
| 11 |
+
from huggingface_hub import hf_hub_download
|
| 12 |
+
|
| 13 |
+
# フォルダがなかったら新規作成
|
| 14 |
+
target_dir = "resources/embeddings"
|
| 15 |
+
os.makedirs(target_dir, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
log.info("Downloading embeddings from Hugging Face Datasets...")
|
| 18 |
+
|
| 19 |
+
# ダウンロード
|
| 20 |
+
path = hf_hub_download(
|
| 21 |
+
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
|
| 22 |
+
repo_type="dataset",
|
| 23 |
+
filename="cc.ja.300.bin",
|
| 24 |
+
local_dir=target_dir,
|
| 25 |
+
local_dir_use_symlinks=False,
|
| 26 |
+
token=os.getenv("HF_TOKEN"),
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
log.info(f"Embeddings downloaded: {path}")
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
fast_text_bin_path = get_file_path_from_config("embeddings.fasttext_bin")
|
| 35 |
+
if os.path.exists(fast_text_bin_path):
|
| 36 |
+
log.info("Embeddings already exist. Skipping download.")
|
| 37 |
+
else:
|
| 38 |
+
download_embeddings()
|
scripts/build_all.py
CHANGED
|
@@ -24,6 +24,9 @@ def run_step(cmd: list[str], allow_fail: bool = False):
|
|
| 24 |
def main():
|
| 25 |
files = field_getter("config/files.json")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
# Step 1: Sudachi user dict (optional but recommended before tokenization)
|
| 28 |
run_step([sys.executable, "scripts/1_build_dict.py"], allow_fail=True)
|
| 29 |
|
|
@@ -42,7 +45,9 @@ def main():
|
|
| 42 |
# Step 6: embeddings (.vec がある場合のみ)
|
| 43 |
vec_path = files("embeddings.fasttext_vec")
|
| 44 |
if os.path.exists(vec_path):
|
| 45 |
-
run_step(
|
|
|
|
|
|
|
| 46 |
else:
|
| 47 |
log.info(".vec が見つからないため Step 6 をスキップします: %s", vec_path)
|
| 48 |
|
|
|
|
| 24 |
def main():
|
| 25 |
files = field_getter("config/files.json")
|
| 26 |
|
| 27 |
+
# Step 0: Download embeddings
|
| 28 |
+
run_step([sys.executable, "scripts/0_download_data.py"], allow_fail=True)
|
| 29 |
+
|
| 30 |
# Step 1: Sudachi user dict (optional but recommended before tokenization)
|
| 31 |
run_step([sys.executable, "scripts/1_build_dict.py"], allow_fail=True)
|
| 32 |
|
|
|
|
| 45 |
# Step 6: embeddings (.vec がある場合のみ)
|
| 46 |
vec_path = files("embeddings.fasttext_vec")
|
| 47 |
if os.path.exists(vec_path):
|
| 48 |
+
run_step(
|
| 49 |
+
[sys.executable, "scripts/6_build_word_embeddings.py"]
|
| 50 |
+
) # needs tf_token, bm25_meta
|
| 51 |
else:
|
| 52 |
log.info(".vec が見つからないため Step 6 をスキップします: %s", vec_path)
|
| 53 |
|