Spaces:
Sleeping
Sleeping
File size: 3,762 Bytes
b57e23d bd36d3a 9269049 aaf0d9a bd36d3a b57e23d bd36d3a b57e23d b2b44fd b57e23d b2b44fd b57e23d 9269049 b2b44fd 9269049 b57e23d aaf0d9a b57e23d b2b44fd bd36d3a 9b201b1 bd36d3a b2b44fd 9b201b1 bd36d3a b2b44fd bd36d3a b2b44fd bd36d3a 9b201b1 bd36d3a b2b44fd 9b201b1 bd36d3a b2b44fd bd36d3a b57e23d 9269049 b57e23d bd36d3a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import os
import sys
import shutil
from pathlib import Path
import dotenv
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from utils.logger import setup_logger
from utils.json import get_file_path_from_config
log = setup_logger(__name__)
fast_text_bin_path = get_file_path_from_config("embeddings.fasttext_bin")
fast_text_vec_path = get_file_path_from_config("embeddings.fasttext_vec")
def download_embeddings() -> None:
"""Hugging Face Datasets から Embeddings をダウンロードする"""
target_files = {
"cc.ja.300.bin": Path(fast_text_bin_path),
"cc.ja.300.vec": Path(fast_text_vec_path),
}
missing_files = [name for name, path in target_files.items() if not path.exists()]
if not missing_files:
log.info("Embeddings already exist. Skipping download.")
return
from huggingface_hub import hf_hub_download
# フォルダがなかったら新規作成
target_dir = Path(fast_text_bin_path).parent
os.makedirs(target_dir, exist_ok=True)
# huggingface_hub は local_dir 配下に .cache/huggingface を作るため先に用意
try:
os.makedirs(target_dir / ".cache" / "huggingface", exist_ok=True)
except PermissionError:
log.error(
"権限エラー: %s に .cache/huggingface を作成できません。Dockerfile の権限設定を確認してください。",
target_dir,
)
raise
log.info("Downloading embeddings from Hugging Face Datasets...")
dotenv.load_dotenv() # .envから環境変数をロード
# ダウンロード
if "cc.ja.300.bin" not in missing_files:
log.info("Embeddings binary already exists. Skipping download.")
else:
path = hf_hub_download(
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
repo_type="dataset",
filename="cc.ja.300.bin",
local_dir=str(target_dir),
token=os.getenv("HF_TOKEN"),
)
log.info(f"Embeddings downloaded: {path}")
_ensure_in_target_dir("cc.ja.300.bin", str(target_dir))
if "cc.ja.300.vec" not in missing_files:
log.info("Embeddings vector already exists. Skipping download.")
else:
path = hf_hub_download(
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
repo_type="dataset",
filename="cc.ja.300.vec",
local_dir=str(target_dir),
token=os.getenv("HF_TOKEN"),
)
log.info(f"Embeddings downloaded: {path}")
_ensure_in_target_dir("cc.ja.300.vec", str(target_dir))
return
def _ensure_in_target_dir(filename: str, target_dir: str) -> None:
"""
ダウンロードされたファイルが target_dir 直下に無い場合、
target_dir 配下(例: `.cache/huggingface`)から探して移動する。
"""
dest = Path(target_dir) / filename
if dest.exists():
return
# 既知のサブディレクトリ優先
candidates: list[Path] = []
sub = Path(target_dir) / ".cache" / "huggingface"
if sub.exists():
candidates.extend(p for p in sub.rglob(filename))
# fallback: target_dir 以下を再帰探索
if not candidates:
candidates = list(Path(target_dir).rglob(filename))
if not candidates:
log.warning("期待するファイルが見つかりません: %s", filename)
return
src = candidates[0]
try:
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.move(str(src), str(dest))
log.info("Moved %s -> %s", src, dest)
except Exception as e:
log.error("%s の移動に失敗しました: %s", filename, e)
raise
if __name__ == "__main__":
download_embeddings()
|