File size: 4,857 Bytes
6cbfd60 13f78b2 6cbfd60 13f78b2 6cbfd60 13f78b2 6cbfd60 13f78b2 6cbfd60 | 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #!/usr/bin/env python3
"""
intent_positions.json (์๋ฒ ๋ฉ + UMAP ๊ธฐ๋ฐ) ์ฐ์ถ ์คํฌ๋ฆฝํธ.
L1 grid ๋ฐฑ์
๊ณผ ๊ฐ์ ํ์์ผ๋ก ์ฐ์ถํ๋ฏ๋ก ์ฐ์ถ ํ ๋ฎ์ด์ฐ๋ฉด
์๋ฒยทํด๋ผ์ด์ธํธ ์ฝ๋ ๋ณ๊ฒฝ ์์ด ์์ฐ ํ๋ฉด์ด ์ ์ขํ๋ก ๋์.
์ฌ์ ์ค์น ํ์:
pip install sentence-transformers umap-learn
์คํ:
cd roadshow-server-v3
python scripts/build_intent_positions_embedding.py
์ต์
:
--model sentence-transformer ๋ชจ๋ธ ์ด๋ฆ (๊ธฐ๋ณธ: jhgan/ko-sroberta-multitask)
--neighbors UMAP n_neighbors (๊ธฐ๋ณธ: 15)
--min-dist UMAP min_dist (๊ธฐ๋ณธ: 0.15)
--seed UMAP random_state (๊ธฐ๋ณธ: 42)
"""
import argparse
import json
import sys
from datetime import datetime
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from core.engines import config
SCENARIO_ID = "cs-myk-v3"
SCENARIO_DIR = Path(__file__).parent.parent / "scenarios" / SCENARIO_ID
L1_COLORS = {
"INT-1000": "#3b82f6",
"INT-2000": "#10b981",
"INT-3000": "#eab308",
"INT-4000": "#a855f7",
"INT-5000": "#ef4444",
"INT-6000": "#f97316",
"INT-7000": "#1f2937",
}
def _normalize_to_range(values, target_min=-1.0, target_max=1.0):
import numpy as np
v = np.asarray(values, dtype=float)
vmin, vmax = v.min(axis=0), v.max(axis=0)
span = (vmax - vmin)
span[span == 0] = 1.0
return (v - vmin) / span * (target_max - target_min) + target_min
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--model", default="jhgan/ko-sroberta-multitask")
parser.add_argument("--neighbors", type=int, default=15)
parser.add_argument("--min-dist", type=float, default=0.15)
parser.add_argument("--seed", type=int, default=42)
args = parser.parse_args()
try:
import numpy as np
from sentence_transformers import SentenceTransformer
import umap
except ImportError as e:
raise SystemExit(
"Missing dependencies. Install with:\n"
" pip install sentence-transformers umap-learn\n"
f"({e})"
)
intents_data = config.get_taxonomy(SCENARIO_ID)
intents = intents_data["intents"] if isinstance(intents_data, dict) else intents_data
print(f"Loaded {len(intents)} intents")
# 1. ์๋ฒ ๋ฉ
print(f"Loading model: {args.model}")
model = SentenceTransformer(args.model)
texts = [f"{it['name']} ({it['L1_name']} > {it['L2_name']})" for it in intents]
print(f"Encoding {len(texts)} texts ...")
emb = model.encode(texts, normalize_embeddings=True, show_progress_bar=True)
print(f"Embeddings shape: {emb.shape}")
# 2. UMAP ์ฐจ์ ์ถ์
print(f"UMAP: n_neighbors={args.neighbors}, min_dist={args.min_dist}")
reducer = umap.UMAP(
n_components=2,
n_neighbors=args.neighbors,
min_dist=args.min_dist,
metric="cosine",
random_state=args.seed,
)
coords = reducer.fit_transform(emb)
coords_norm = _normalize_to_range(coords, -1.0, 1.0)
# 3. payload ๊ตฌ์ฑ
intent_positions = [
{
"intent_id": it["id"],
"L1_id": it["L1_id"],
"x": round(float(coords_norm[i, 0]), 4),
"y": round(float(coords_norm[i, 1]), 4),
}
for i, it in enumerate(intents)
]
# L1 zone centroid (= ๊ฐ์ L1 ์ ๋ค์ ํ๊ท )
from collections import defaultdict
by_l1: dict[str, list[tuple[float, float]]] = defaultdict(list)
for p in intent_positions:
by_l1[p["L1_id"]].append((p["x"], p["y"]))
l1_zones = []
seen_l1_names = {it["L1_id"]: it["L1_name"] for it in intents}
for l1_id, pts in by_l1.items():
cx = round(sum(x for x, _ in pts) / len(pts), 4)
cy = round(sum(y for _, y in pts) / len(pts), 4)
l1_zones.append({
"L1_id": l1_id,
"L1_name": seen_l1_names.get(l1_id, l1_id),
"centroid": {"x": cx, "y": cy},
"color": L1_COLORS.get(l1_id, "#94a3b8"),
})
payload = {
"scenario_id": "cs-myk-v3",
"embedding_model": args.model,
"reducer": "umap",
"reducer_params": {
"n_neighbors": args.neighbors,
"min_dist": args.min_dist,
"metric": "cosine",
"random_state": args.seed,
},
"coord_range": [-1, 1],
"generated_at": datetime.utcnow().isoformat() + "Z",
"intents": intent_positions,
"l1_zones": l1_zones,
}
out_path = SCENARIO_DIR / "intent_positions.json"
with open(out_path, "w", encoding="utf-8") as f:
json.dump(payload, f, ensure_ascii=False, indent=2)
print(f"Wrote {out_path}")
if __name__ == "__main__":
main()
|