Spaces:
Sleeping
Sleeping
ktsn-ud commited on
Commit ·
b2b44fd
1
Parent(s): 14b78aa
データ更新のエンドポイントを作成し,定期的に叩くGitHubワークフローを追加
Browse files- .github/workflows/update_data.yaml +18 -0
- app/main.py +48 -0
- scripts/0_download_data.py +18 -8
.github/workflows/update_data.yaml
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: update data
|
| 2 |
+
on:
|
| 3 |
+
schedule:
|
| 4 |
+
- cron: '0 18 * * *' # 毎日3時(JST)に実行
|
| 5 |
+
workflow_dispatch:
|
| 6 |
+
|
| 7 |
+
jobs:
|
| 8 |
+
call:
|
| 9 |
+
runs-on: ubuntu-latest
|
| 10 |
+
steps:
|
| 11 |
+
- name: Trigger HF Space FastAPI Endpoint
|
| 12 |
+
run: |
|
| 13 |
+
curl --fail --show-error --silent -X 'POST' \
|
| 14 |
+
'${{ vars.FASTAPI_URL }}/tasks/update' \
|
| 15 |
+
-H 'accept: application/json' \
|
| 16 |
+
-H 'Authorization: Bearer ${{ secrets.HF_TOKEN }}' \
|
| 17 |
+
-H 'X-API-KEY: ${{ secrets.API_SECRET_KEY }}' \
|
| 18 |
+
-d ''
|
app/main.py
CHANGED
|
@@ -2,6 +2,8 @@ import os
|
|
| 2 |
import sys
|
| 3 |
import json
|
| 4 |
import gzip
|
|
|
|
|
|
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
|
| 7 |
from fastapi import FastAPI, Response, Depends, HTTPException, Security, Query
|
|
@@ -154,6 +156,10 @@ class SearchRequest(BaseModel):
|
|
| 154 |
debug: bool = False
|
| 155 |
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
@app.post(
|
| 158 |
"/api/search",
|
| 159 |
response_model=ProjectIds,
|
|
@@ -195,3 +201,45 @@ def search(request: SearchRequest):
|
|
| 195 |
pass
|
| 196 |
|
| 197 |
return ProjectIds(projectIds=ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import sys
|
| 3 |
import json
|
| 4 |
import gzip
|
| 5 |
+
import subprocess
|
| 6 |
+
from pathlib import Path
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
|
| 9 |
from fastapi import FastAPI, Response, Depends, HTTPException, Security, Query
|
|
|
|
| 156 |
debug: bool = False
|
| 157 |
|
| 158 |
|
| 159 |
+
class TaskUpdateResponse(BaseModel):
|
| 160 |
+
message: str
|
| 161 |
+
|
| 162 |
+
|
| 163 |
@app.post(
|
| 164 |
"/api/search",
|
| 165 |
response_model=ProjectIds,
|
|
|
|
| 201 |
pass
|
| 202 |
|
| 203 |
return ProjectIds(projectIds=ids)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
@app.post(
|
| 207 |
+
"/tasks/update",
|
| 208 |
+
response_model=TaskUpdateResponse,
|
| 209 |
+
dependencies=[Depends(get_api_key)],
|
| 210 |
+
)
|
| 211 |
+
def update_tasks():
|
| 212 |
+
project_root = Path(__file__).resolve().parents[1]
|
| 213 |
+
script_path = project_root / "scripts" / "build_all.py"
|
| 214 |
+
if not script_path.exists():
|
| 215 |
+
log.error("Requested update script not found: %s", script_path)
|
| 216 |
+
raise HTTPException(status_code=500, detail="Update script not found.")
|
| 217 |
+
|
| 218 |
+
try:
|
| 219 |
+
result = subprocess.run(
|
| 220 |
+
[sys.executable, str(script_path)],
|
| 221 |
+
check=True,
|
| 222 |
+
capture_output=True,
|
| 223 |
+
text=True,
|
| 224 |
+
cwd=str(project_root),
|
| 225 |
+
)
|
| 226 |
+
except subprocess.CalledProcessError as exc:
|
| 227 |
+
stdout = exc.stdout.strip() if exc.stdout else ""
|
| 228 |
+
stderr = exc.stderr.strip() if exc.stderr else ""
|
| 229 |
+
if stdout:
|
| 230 |
+
log.error("build_all.py stdout:\n%s", stdout)
|
| 231 |
+
if stderr:
|
| 232 |
+
log.error("build_all.py stderr:\n%s", stderr)
|
| 233 |
+
raise HTTPException(
|
| 234 |
+
status_code=500,
|
| 235 |
+
detail="Data update failed while running build_all.py.",
|
| 236 |
+
) from exc
|
| 237 |
+
|
| 238 |
+
stdout = result.stdout.strip() if result.stdout else ""
|
| 239 |
+
stderr = result.stderr.strip() if result.stderr else ""
|
| 240 |
+
if stdout:
|
| 241 |
+
log.info("build_all.py stdout:\n%s", stdout)
|
| 242 |
+
if stderr:
|
| 243 |
+
log.warning("build_all.py stderr:\n%s", stderr)
|
| 244 |
+
|
| 245 |
+
return TaskUpdateResponse(message="Data update completed.")
|
scripts/0_download_data.py
CHANGED
|
@@ -17,14 +17,24 @@ fast_text_vec_path = get_file_path_from_config("embeddings.fasttext_vec")
|
|
| 17 |
|
| 18 |
def download_embeddings() -> None:
|
| 19 |
"""Hugging Face Datasets から Embeddings をダウンロードする"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
from huggingface_hub import hf_hub_download
|
| 21 |
|
| 22 |
# フォルダがなかったら新規作成
|
| 23 |
-
target_dir =
|
| 24 |
os.makedirs(target_dir, exist_ok=True)
|
| 25 |
# huggingface_hub は local_dir 配下に .cache/huggingface を作るため先に用意
|
| 26 |
try:
|
| 27 |
-
os.makedirs(
|
| 28 |
except PermissionError:
|
| 29 |
log.error(
|
| 30 |
"権限エラー: %s に .cache/huggingface を作成できません。Dockerfile の権限設定を確認してください。",
|
|
@@ -37,31 +47,31 @@ def download_embeddings() -> None:
|
|
| 37 |
dotenv.load_dotenv() # .envから環境変数をロード
|
| 38 |
|
| 39 |
# ダウンロード
|
| 40 |
-
if
|
| 41 |
log.info("Embeddings binary already exists. Skipping download.")
|
| 42 |
else:
|
| 43 |
path = hf_hub_download(
|
| 44 |
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
|
| 45 |
repo_type="dataset",
|
| 46 |
filename="cc.ja.300.bin",
|
| 47 |
-
local_dir=target_dir,
|
| 48 |
token=os.getenv("HF_TOKEN"),
|
| 49 |
)
|
| 50 |
log.info(f"Embeddings downloaded: {path}")
|
| 51 |
-
_ensure_in_target_dir("cc.ja.300.bin", target_dir)
|
| 52 |
|
| 53 |
-
if
|
| 54 |
log.info("Embeddings vector already exists. Skipping download.")
|
| 55 |
else:
|
| 56 |
path = hf_hub_download(
|
| 57 |
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
|
| 58 |
repo_type="dataset",
|
| 59 |
filename="cc.ja.300.vec",
|
| 60 |
-
local_dir=target_dir,
|
| 61 |
token=os.getenv("HF_TOKEN"),
|
| 62 |
)
|
| 63 |
log.info(f"Embeddings downloaded: {path}")
|
| 64 |
-
_ensure_in_target_dir("cc.ja.300.vec", target_dir)
|
| 65 |
|
| 66 |
return
|
| 67 |
|
|
|
|
| 17 |
|
| 18 |
def download_embeddings() -> None:
|
| 19 |
"""Hugging Face Datasets から Embeddings をダウンロードする"""
|
| 20 |
+
target_files = {
|
| 21 |
+
"cc.ja.300.bin": Path(fast_text_bin_path),
|
| 22 |
+
"cc.ja.300.vec": Path(fast_text_vec_path),
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
missing_files = [name for name, path in target_files.items() if not path.exists()]
|
| 26 |
+
if not missing_files:
|
| 27 |
+
log.info("Embeddings already exist. Skipping download.")
|
| 28 |
+
return
|
| 29 |
+
|
| 30 |
from huggingface_hub import hf_hub_download
|
| 31 |
|
| 32 |
# フォルダがなかったら新規作成
|
| 33 |
+
target_dir = Path(fast_text_bin_path).parent
|
| 34 |
os.makedirs(target_dir, exist_ok=True)
|
| 35 |
# huggingface_hub は local_dir 配下に .cache/huggingface を作るため先に用意
|
| 36 |
try:
|
| 37 |
+
os.makedirs(target_dir / ".cache" / "huggingface", exist_ok=True)
|
| 38 |
except PermissionError:
|
| 39 |
log.error(
|
| 40 |
"権限エラー: %s に .cache/huggingface を作成できません。Dockerfile の権限設定を確認してください。",
|
|
|
|
| 47 |
dotenv.load_dotenv() # .envから環境変数をロード
|
| 48 |
|
| 49 |
# ダウンロード
|
| 50 |
+
if "cc.ja.300.bin" not in missing_files:
|
| 51 |
log.info("Embeddings binary already exists. Skipping download.")
|
| 52 |
else:
|
| 53 |
path = hf_hub_download(
|
| 54 |
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
|
| 55 |
repo_type="dataset",
|
| 56 |
filename="cc.ja.300.bin",
|
| 57 |
+
local_dir=str(target_dir),
|
| 58 |
token=os.getenv("HF_TOKEN"),
|
| 59 |
)
|
| 60 |
log.info(f"Embeddings downloaded: {path}")
|
| 61 |
+
_ensure_in_target_dir("cc.ja.300.bin", str(target_dir))
|
| 62 |
|
| 63 |
+
if "cc.ja.300.vec" not in missing_files:
|
| 64 |
log.info("Embeddings vector already exists. Skipping download.")
|
| 65 |
else:
|
| 66 |
path = hf_hub_download(
|
| 67 |
repo_id=os.getenv("HF_EMBEDDINGS_REPO_ID"),
|
| 68 |
repo_type="dataset",
|
| 69 |
filename="cc.ja.300.vec",
|
| 70 |
+
local_dir=str(target_dir),
|
| 71 |
token=os.getenv("HF_TOKEN"),
|
| 72 |
)
|
| 73 |
log.info(f"Embeddings downloaded: {path}")
|
| 74 |
+
_ensure_in_target_dir("cc.ja.300.vec", str(target_dir))
|
| 75 |
|
| 76 |
return
|
| 77 |
|