Commit 34816b3b authored by svc-mlops's avatar svc-mlops

Khởi tạo classify_topic (mô phỏng) v1.0.0

parents
Pipeline #1 failed with stages
in 11 minutes
File added
File added
File added
File added
File added
File added
# Mô phỏng CI hiện tại của bên serving (sprint 1 giữ nguyên luồng này).
# GitLab 11.7.5: chỉ dùng only / except / when, không dùng rules / needs / workflow.
image: k3d-nexus:5000/ci-base:1.0.0
variables:
MINIO_PATH: "models/hieuhm18/classify_topic"
stages:
- validate
- publish
- deploy-uat
- smoke-test
- promote-prod
validate:
stage: validate
script:
- python3 scripts/validate_repo.py
- python3 -m pytest -q tests/
publish:
stage: publish
only:
- master
script:
- export VERSION_NAME=$(tr -d '[:space:]' < VERSION)
- test -n "$VERSION_NAME"
- echo "Version = v${VERSION_NAME} (commit ${CI_COMMIT_SHORT_SHA})"
- . scripts/minio_env.sh
- python3 scripts/package_runtime.py "v${VERSION_NAME}" staging
- echo "$CI_COMMIT_SHORT_SHA" > staging/SOURCE_COMMIT
- EXIST=$(mc cat "myminio/${MINIO_PATH}/v${VERSION_NAME}/SOURCE_COMMIT" 2>/dev/null || true)
- if [ -n "$EXIST" ] && [ "$EXIST" != "$CI_COMMIT_SHORT_SHA" ]; then echo "v${VERSION_NAME} đã có trên MinIO từ commit $EXIST"; exit 1; fi
- mc mirror --overwrite staging/ "myminio/${MINIO_PATH}/v${VERSION_NAME}/"
deploy-uat:
stage: deploy-uat
only:
- master
script:
- export VERSION_NAME=$(tr -d '[:space:]' < VERSION)
- python3 scripts/set_version.py deploy/classify-topic/overlays/uat "v${VERSION_NAME}"
- bash scripts/commit_version.sh uat "v${VERSION_NAME}" || echo "(không commit được bump, vẫn apply)"
- bash scripts/kretry.sh kubectl apply -k deploy/classify-topic/overlays/uat
- bash scripts/kretry.sh kubectl -n uat rollout status deployment/classify-topic-uat --timeout=600s
smoke-test:
stage: smoke-test
only:
- master
script:
- SVC_IP=$(bash scripts/kretry.sh kubectl -n uat get svc classify-topic-uat -o jsonpath='{.spec.clusterIP}')
- echo "Service UAT = $SVC_IP"
- python3 scripts/smoke_test.py "http://${SVC_IP}:8200/v2/models/ensemble_PhoBERT/infer" 2
promote-prod:
stage: promote-prod
only:
- master
when: manual
script:
- export VERSION_NAME=$(tr -d '[:space:]' < VERSION)
- python3 scripts/set_version.py deploy/classify-topic/overlays/prod "v${VERSION_NAME}"
- bash scripts/commit_version.sh prod "v${VERSION_NAME}" || echo "(không commit được bump, vẫn apply)"
- bash scripts/kretry.sh kubectl apply -k deploy/classify-topic/overlays/prod
- bash scripts/kretry.sh kubectl -n ocr rollout status deployment/classify-topic --timeout=900s
# classify_topic (bản mô phỏng)
Repo này được đẩy lên GitLab mô phỏng làm repo `ocr_ai_sme/classify_topic`.
- `model_repository/`: ensemble Triton `preprocess_PhoBERT → PhoBERT → postprocess_PhoBERT`. Model nhỏ thay PhoBERT, cây nhãn 4/12/24/48.
- `deploy/classify-topic/`: Kustomize base + overlay `uat` (ns `uat`), `prod` (ns `ocr`).
- `.gitlab-ci.yml`: CI hiện tại của bên serving (validate → publish → deploy-uat → smoke-test → promote-prod bấm tay).
- `dags/`: DAG `train_pipeline` cho Airflow trên cụm vận hành (sprint 1). Airflow kéo thư mục này bằng git-sync.
- `scripts/`, `tests/`: kiểm repo, đóng gói, bump version, smoke test.
"""Client gọi API bước trên máy GPU 106 (cùng tên hàm với module chatbot_api mà DAG tạm đang dùng).
Cấu hình qua biến môi trường: MOCK106_URL, MOCK106_TOKEN.
"""
from __future__ import annotations
import os
import time
import requests
from airflow.exceptions import AirflowException
def _base() -> str:
return os.environ.get("MOCK106_URL", "http://train106.sim:8000").rstrip("/")
def _headers() -> dict:
return {"X-API-Key": os.environ.get("MOCK106_TOKEN", "")}
def check_service() -> dict:
r = requests.get(f"{_base()}/health", timeout=15)
r.raise_for_status()
info = r.json()
print(f"106: {info}")
return info
def trigger_step(step: str, bot_id: str, dataset: str, config_overrides: dict | None = None,
cancel_running: bool = False) -> str:
body = {"step": step, "bot_id": bot_id, "dataset": dataset,
"config_overrides": config_overrides, "cancel_running": cancel_running}
r = requests.post(f"{_base()}/jobs", json=body, headers=_headers(), timeout=120)
if r.status_code == 409:
raise AirflowException(f"106 đang bận: {r.json().get('detail')}. Đặt cancel_running=true để huỷ job cũ")
r.raise_for_status()
jid = r.json()["job_id"]
print(f"đã gửi bước {step} lên 106: job {jid}")
return jid
def get_job_log(job_id: str) -> str:
r = requests.get(f"{_base()}/jobs/{job_id}/log", headers=_headers(), timeout=30)
r.raise_for_status()
return r.text
def wait_job(job_id: str, timeout_sec: int, poll_sec: int = 5) -> dict:
deadline = time.time() + timeout_sec
printed = 0
while time.time() < deadline:
r = requests.get(f"{_base()}/jobs/{job_id}", headers=_headers(), timeout=30)
r.raise_for_status()
info = r.json()
lines = get_job_log(job_id).splitlines()
for line in lines[printed:]:
print(f" 106| {line}")
printed = len(lines)
if info["status"] == "succeeded":
return info
if info["status"] in ("failed", "cancelled"):
raise AirflowException(f"job {job_id} ({info['step']}) {info['status']}: {info.get('error')}")
time.sleep(poll_sec)
raise AirflowException(f"job {job_id} quá {timeout_sec}s")
def verify_paths(paths: list[str]) -> dict:
r = requests.post(f"{_base()}/verify", json={"paths": paths}, headers=_headers(), timeout=60)
r.raise_for_status()
return r.json()
def download_artifact(name: str, dest: str, bot_id: str) -> int:
os.makedirs(os.path.dirname(dest) or ".", exist_ok=True)
with requests.get(f"{_base()}/artifacts/{name}", params={"bot_id": bot_id},
headers=_headers(), stream=True, timeout=600) as r:
r.raise_for_status()
with open(dest, "wb") as f:
for chunk in r.iter_content(1 << 20):
f.write(chunk)
return os.path.getsize(dest)
This diff is collapsed.
# Triton 23.03 + initContainer tải gói model theo MODEL_VERSION từ MinIO.
# Mô phỏng: bỏ schedulerName kai-scheduler và nodeAffinity GPU (cụm k3d không có GPU), giữ annotation gpu-fraction.
apiVersion: apps/v1
kind: Deployment
metadata:
name: classify-topic
labels:
app: classify-topic
spec:
replicas: 1
selector:
matchLabels:
app: classify-topic
template:
metadata:
labels:
app: classify-topic
annotations:
gpu-fraction: "0.15"
spec:
initContainers:
- name: fetch-model
image: k3d-nexus:5000/ci-base:1.0.0
command: ["sh", "-c"]
args:
- >-
mc alias set m http://minio-service.storage.svc:9000 "$MINIO_USER" "$MINIO_KEY" >/dev/null &&
mc mirror --overwrite "m/${MINIO_PATH}/${MODEL_VERSION}/" /workspace/ &&
test -f /workspace/model_repository/PhoBERT/1/model.onnx &&
echo "Đã tải ${MODEL_VERSION}"
envFrom:
- configMapRef:
name: classify-topic-version
env:
- name: MINIO_PATH
value: models/hieuhm18/classify_topic
- name: MINIO_USER
valueFrom:
secretKeyRef: {name: minio-creds, key: username}
- name: MINIO_KEY
valueFrom:
secretKeyRef: {name: minio-creds, key: password}
volumeMounts:
- {name: workspace, mountPath: /workspace}
containers:
- name: triton
image: k3d-nexus:5000/tritonserver:23.03-py3
command: ["tritonserver"]
args:
- --model-repository=/workspace/model_repository
- --http-port=8200
- --grpc-port=8201
- --metrics-port=8299
envFrom:
- configMapRef:
name: classify-topic-version
ports:
- {name: http, containerPort: 8200}
- {name: metrics, containerPort: 8299}
readinessProbe:
httpGet: {path: /v2/health/ready, port: 8200}
periodSeconds: 5
resources:
requests: {cpu: 250m, memory: 512Mi}
limits: {memory: 3Gi}
volumeMounts:
- {name: workspace, mountPath: /workspace}
volumes:
- name: workspace
emptyDir: {}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
apiVersion: v1
kind: Service
metadata:
name: classify-topic
spec:
type: NodePort
selector:
app: classify-topic
ports:
- {name: http, port: 8200, targetPort: 8200}
- {name: metrics, port: 8299, targetPort: 8299}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: ocr
resources:
- ../../base
- pdb.yaml
configMapGenerator:
- name: classify-topic-version
literals:
- MODEL_VERSION=v1.0.0
patches:
- path: patch-gpu.yaml
- path: patch-service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: classify-topic
spec:
replicas: 2
template:
metadata:
annotations:
gpu-fraction: "0.3"
apiVersion: v1
kind: Service
metadata:
name: classify-topic
spec:
ports:
- {name: http, port: 8200, nodePort: 8215}
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: classify-topic
spec:
minAvailable: 1
selector:
matchLabels:
app: classify-topic
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: uat
nameSuffix: -uat
resources:
- ../../base
configMapGenerator:
- name: classify-topic-version
literals:
- MODEL_VERSION=v1.0.0
patches:
- path: patch-gpu.yaml
- path: patch-service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: classify-topic
spec:
replicas: 1
template:
metadata:
annotations:
gpu-fraction: "0.15"
apiVersion: v1
kind: Service
metadata:
name: classify-topic
spec:
ports:
- {name: http, port: 8200, nodePort: 8216}
# Bản mô phỏng: model nhỏ thay PhoBERT, chạy CPU (cụm thật chạy GPU L40S).
name: "PhoBERT"
platform: "onnxruntime_onnx"
max_batch_size: 0
input [
{ name: "input_ids" data_type: TYPE_INT64 dims: [ 1, 32 ] }
]
output [
{ name: "output_l1" data_type: TYPE_FP32 dims: [ 1, 5 ] },
{ name: "output_l2" data_type: TYPE_FP32 dims: [ 1, 13 ] },
{ name: "output_l3" data_type: TYPE_FP32 dims: [ 1, 25 ] },
{ name: "output_l4" data_type: TYPE_FP32 dims: [ 1, 49 ] }
]
instance_group [ { count: 2 kind: KIND_CPU } ]
name: "ensemble_PhoBERT"
platform: "ensemble"
max_batch_size: 0
input [
{ name: "INPUT_TEXT" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "BOT_ID" data_type: TYPE_STRING dims: [ 1 ] }
]
output [
{ name: "TEXT_LABEL_L1" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L2" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L3" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L4" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_CONF_L1" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L2" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L3" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L4" data_type: TYPE_FP32 dims: [ 1 ] }
]
ensemble_scheduling {
step [
{
model_name: "preprocess_PhoBERT"
model_version: -1
input_map { key: "INPUT_TEXT" value: "INPUT_TEXT" }
input_map { key: "BOT_ID" value: "BOT_ID" }
output_map { key: "INPUT_IDS" value: "input_ids_t" }
},
{
model_name: "PhoBERT"
model_version: -1
input_map { key: "input_ids" value: "input_ids_t" }
output_map { key: "output_l1" value: "probs_l1" }
output_map { key: "output_l2" value: "probs_l2" }
output_map { key: "output_l3" value: "probs_l3" }
output_map { key: "output_l4" value: "probs_l4" }
},
{
model_name: "postprocess_PhoBERT"
model_version: -1
input_map { key: "BOT_ID" value: "BOT_ID" }
input_map { key: "INPUT_LABELS_L1" value: "probs_l1" }
input_map { key: "INPUT_LABELS_L2" value: "probs_l2" }
input_map { key: "INPUT_LABELS_L3" value: "probs_l3" }
input_map { key: "INPUT_LABELS_L4" value: "probs_l4" }
output_map { key: "TEXT_LABEL_L1" value: "TEXT_LABEL_L1" }
output_map { key: "TEXT_LABEL_L2" value: "TEXT_LABEL_L2" }
output_map { key: "TEXT_LABEL_L3" value: "TEXT_LABEL_L3" }
output_map { key: "TEXT_LABEL_L4" value: "TEXT_LABEL_L4" }
output_map { key: "TEXT_CONF_L1" value: "TEXT_CONF_L1" }
output_map { key: "TEXT_CONF_L2" value: "TEXT_CONF_L2" }
output_map { key: "TEXT_CONF_L3" value: "TEXT_CONF_L3" }
output_map { key: "TEXT_CONF_L4" value: "TEXT_CONF_L4" }
}
]
}
"""Khối postprocess: argmax 4 cấp, id -> tên nhãn theo bot, kiểm cây nhãn (con không thuộc cha -> "nan")."""
import glob
import json
import os
import numpy as np
import triton_python_backend_utils as pb_utils
LEVELS = (1, 2, 3, 4)
class TritonPythonModel:
def initialize(self, args):
base = os.path.join(args["model_repository"], "tokenizer")
self.tok = {}
for bot_dir in sorted(glob.glob(os.path.join(base, "*"))):
bot = os.path.basename(bot_dir)
levels = {}
for k in LEVELS:
with open(os.path.join(bot_dir, "label_level%d_tokenizer.json" % k), encoding="utf-8") as f:
levels[k] = json.load(f)
self.tok[bot] = levels
pb_utils.Logger.log_info("postprocess: nạp tokenizer cho bot %s" % sorted(self.tok))
def execute(self, requests):
responses = []
for req in requests:
bot = pb_utils.get_input_tensor_by_name(req, "BOT_ID").as_numpy().reshape(-1)[0]
bot = bot.decode("utf-8") if isinstance(bot, bytes) else str(bot)
if bot not in self.tok:
err = pb_utils.TritonError("BOT_ID %s chưa có tokenizer nhãn" % bot)
responses.append(pb_utils.InferenceResponse(output_tensors=[], error=err))
continue
labels, confs = [], []
parent_label = None
for k in LEVELS:
probs = pb_utils.get_input_tensor_by_name(req, "INPUT_LABELS_L%d" % k).as_numpy().reshape(-1)
idx = int(np.argmax(probs))
name = self.tok[bot][k]["labels"][idx]
parent = self.tok[bot][k]["parent"].get(name)
if k > 1 and (parent_label in (None, "nan") or parent != parent_label):
name = "nan"
labels.append(name)
confs.append(float(probs[idx]))
parent_label = name
out = [pb_utils.Tensor("TEXT_LABEL_L%d" % k, np.array([labels[k - 1].encode("utf-8")], dtype=np.object_))
for k in LEVELS]
out += [pb_utils.Tensor("TEXT_CONF_L%d" % k, np.array([confs[k - 1]], dtype=np.float32)) for k in LEVELS]
responses.append(pb_utils.InferenceResponse(output_tensors=out))
return responses
name: "postprocess_PhoBERT"
backend: "python"
max_batch_size: 0
input [
{ name: "BOT_ID" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "INPUT_LABELS_L1" data_type: TYPE_FP32 dims: [ 1, 5 ] },
{ name: "INPUT_LABELS_L2" data_type: TYPE_FP32 dims: [ 1, 13 ] },
{ name: "INPUT_LABELS_L3" data_type: TYPE_FP32 dims: [ 1, 25 ] },
{ name: "INPUT_LABELS_L4" data_type: TYPE_FP32 dims: [ 1, 49 ] }
]
output [
{ name: "TEXT_LABEL_L1" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L2" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L3" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L4" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_CONF_L1" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L2" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L3" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L4" data_type: TYPE_FP32 dims: [ 1 ] }
]
instance_group [ { count: 1 kind: KIND_CPU } ]
{
"bot_id": "2",
"level": 1,
"labels": [
"nan",
"dien_nuoc",
"hoadon_dientu",
"tai_khoan",
"the_cao"
],
"parent": {}
}
\ No newline at end of file
{
"bot_id": "2",
"level": 2,
"labels": [
"nan",
"dang_ky_tu_dong",
"dang_nhap",
"dieu_chinh_hoadon",
"huy_hoadon",
"khoa_tai_khoan",
"loi_the",
"mo_tai_khoan",
"mua_the",
"nap_the",
"thanh_toan",
"tra_cuu_hoadon",
"tra_cuu_no"
],
"parent": {
"tra_cuu_hoadon": "hoadon_dientu",
"dieu_chinh_hoadon": "hoadon_dientu",
"huy_hoadon": "hoadon_dientu",
"nap_the": "the_cao",
"loi_the": "the_cao",
"mua_the": "the_cao",
"mo_tai_khoan": "tai_khoan",
"dang_nhap": "tai_khoan",
"khoa_tai_khoan": "tai_khoan",
"thanh_toan": "dien_nuoc",
"tra_cuu_no": "dien_nuoc",
"dang_ky_tu_dong": "dien_nuoc"
}
}
\ No newline at end of file
{
"bot_id": "2",
"level": 3,
"labels": [
"nan",
"dang_ky_tu_dong__huong_dan",
"dang_ky_tu_dong__khieu_nai",
"dang_nhap__huong_dan",
"dang_nhap__khieu_nai",
"dieu_chinh_hoadon__huong_dan",
"dieu_chinh_hoadon__khieu_nai",
"huy_hoadon__huong_dan",
"huy_hoadon__khieu_nai",
"khoa_tai_khoan__huong_dan",
"khoa_tai_khoan__khieu_nai",
"loi_the__huong_dan",
"loi_the__khieu_nai",
"mo_tai_khoan__huong_dan",
"mo_tai_khoan__khieu_nai",
"mua_the__huong_dan",
"mua_the__khieu_nai",
"nap_the__huong_dan",
"nap_the__khieu_nai",
"thanh_toan__huong_dan",
"thanh_toan__khieu_nai",
"tra_cuu_hoadon__huong_dan",
"tra_cuu_hoadon__khieu_nai",
"tra_cuu_no__huong_dan",
"tra_cuu_no__khieu_nai"
],
"parent": {
"tra_cuu_hoadon__huong_dan": "tra_cuu_hoadon",
"tra_cuu_hoadon__khieu_nai": "tra_cuu_hoadon",
"dieu_chinh_hoadon__huong_dan": "dieu_chinh_hoadon",
"dieu_chinh_hoadon__khieu_nai": "dieu_chinh_hoadon",
"huy_hoadon__huong_dan": "huy_hoadon",
"huy_hoadon__khieu_nai": "huy_hoadon",
"nap_the__huong_dan": "nap_the",
"nap_the__khieu_nai": "nap_the",
"loi_the__huong_dan": "loi_the",
"loi_the__khieu_nai": "loi_the",
"mua_the__huong_dan": "mua_the",
"mua_the__khieu_nai": "mua_the",
"mo_tai_khoan__huong_dan": "mo_tai_khoan",
"mo_tai_khoan__khieu_nai": "mo_tai_khoan",
"dang_nhap__huong_dan": "dang_nhap",
"dang_nhap__khieu_nai": "dang_nhap",
"khoa_tai_khoan__huong_dan": "khoa_tai_khoan",
"khoa_tai_khoan__khieu_nai": "khoa_tai_khoan",
"thanh_toan__huong_dan": "thanh_toan",
"thanh_toan__khieu_nai": "thanh_toan",
"tra_cuu_no__huong_dan": "tra_cuu_no",
"tra_cuu_no__khieu_nai": "tra_cuu_no",
"dang_ky_tu_dong__huong_dan": "dang_ky_tu_dong",
"dang_ky_tu_dong__khieu_nai": "dang_ky_tu_dong"
}
}
\ No newline at end of file
{
"bot_id": "2",
"level": 4,
"labels": [
"nan",
"dang_ky_tu_dong__huong_dan__online",
"dang_ky_tu_dong__huong_dan__tai_quay",
"dang_ky_tu_dong__khieu_nai__online",
"dang_ky_tu_dong__khieu_nai__tai_quay",
"dang_nhap__huong_dan__online",
"dang_nhap__huong_dan__tai_quay",
"dang_nhap__khieu_nai__online",
"dang_nhap__khieu_nai__tai_quay",
"dieu_chinh_hoadon__huong_dan__online",
"dieu_chinh_hoadon__huong_dan__tai_quay",
"dieu_chinh_hoadon__khieu_nai__online",
"dieu_chinh_hoadon__khieu_nai__tai_quay",
"huy_hoadon__huong_dan__online",
"huy_hoadon__huong_dan__tai_quay",
"huy_hoadon__khieu_nai__online",
"huy_hoadon__khieu_nai__tai_quay",
"khoa_tai_khoan__huong_dan__online",
"khoa_tai_khoan__huong_dan__tai_quay",
"khoa_tai_khoan__khieu_nai__online",
"khoa_tai_khoan__khieu_nai__tai_quay",
"loi_the__huong_dan__online",
"loi_the__huong_dan__tai_quay",
"loi_the__khieu_nai__online",
"loi_the__khieu_nai__tai_quay",
"mo_tai_khoan__huong_dan__online",
"mo_tai_khoan__huong_dan__tai_quay",
"mo_tai_khoan__khieu_nai__online",
"mo_tai_khoan__khieu_nai__tai_quay",
"mua_the__huong_dan__online",
"mua_the__huong_dan__tai_quay",
"mua_the__khieu_nai__online",
"mua_the__khieu_nai__tai_quay",
"nap_the__huong_dan__online",
"nap_the__huong_dan__tai_quay",
"nap_the__khieu_nai__online",
"nap_the__khieu_nai__tai_quay",
"thanh_toan__huong_dan__online",
"thanh_toan__huong_dan__tai_quay",
"thanh_toan__khieu_nai__online",
"thanh_toan__khieu_nai__tai_quay",
"tra_cuu_hoadon__huong_dan__online",
"tra_cuu_hoadon__huong_dan__tai_quay",
"tra_cuu_hoadon__khieu_nai__online",
"tra_cuu_hoadon__khieu_nai__tai_quay",
"tra_cuu_no__huong_dan__online",
"tra_cuu_no__huong_dan__tai_quay",
"tra_cuu_no__khieu_nai__online",
"tra_cuu_no__khieu_nai__tai_quay"
],
"parent": {
"tra_cuu_hoadon__huong_dan__online": "tra_cuu_hoadon__huong_dan",
"tra_cuu_hoadon__huong_dan__tai_quay": "tra_cuu_hoadon__huong_dan",
"tra_cuu_hoadon__khieu_nai__online": "tra_cuu_hoadon__khieu_nai",
"tra_cuu_hoadon__khieu_nai__tai_quay": "tra_cuu_hoadon__khieu_nai",
"dieu_chinh_hoadon__huong_dan__online": "dieu_chinh_hoadon__huong_dan",
"dieu_chinh_hoadon__huong_dan__tai_quay": "dieu_chinh_hoadon__huong_dan",
"dieu_chinh_hoadon__khieu_nai__online": "dieu_chinh_hoadon__khieu_nai",
"dieu_chinh_hoadon__khieu_nai__tai_quay": "dieu_chinh_hoadon__khieu_nai",
"huy_hoadon__huong_dan__online": "huy_hoadon__huong_dan",
"huy_hoadon__huong_dan__tai_quay": "huy_hoadon__huong_dan",
"huy_hoadon__khieu_nai__online": "huy_hoadon__khieu_nai",
"huy_hoadon__khieu_nai__tai_quay": "huy_hoadon__khieu_nai",
"nap_the__huong_dan__online": "nap_the__huong_dan",
"nap_the__huong_dan__tai_quay": "nap_the__huong_dan",
"nap_the__khieu_nai__online": "nap_the__khieu_nai",
"nap_the__khieu_nai__tai_quay": "nap_the__khieu_nai",
"loi_the__huong_dan__online": "loi_the__huong_dan",
"loi_the__huong_dan__tai_quay": "loi_the__huong_dan",
"loi_the__khieu_nai__online": "loi_the__khieu_nai",
"loi_the__khieu_nai__tai_quay": "loi_the__khieu_nai",
"mua_the__huong_dan__online": "mua_the__huong_dan",
"mua_the__huong_dan__tai_quay": "mua_the__huong_dan",
"mua_the__khieu_nai__online": "mua_the__khieu_nai",
"mua_the__khieu_nai__tai_quay": "mua_the__khieu_nai",
"mo_tai_khoan__huong_dan__online": "mo_tai_khoan__huong_dan",
"mo_tai_khoan__huong_dan__tai_quay": "mo_tai_khoan__huong_dan",
"mo_tai_khoan__khieu_nai__online": "mo_tai_khoan__khieu_nai",
"mo_tai_khoan__khieu_nai__tai_quay": "mo_tai_khoan__khieu_nai",
"dang_nhap__huong_dan__online": "dang_nhap__huong_dan",
"dang_nhap__huong_dan__tai_quay": "dang_nhap__huong_dan",
"dang_nhap__khieu_nai__online": "dang_nhap__khieu_nai",
"dang_nhap__khieu_nai__tai_quay": "dang_nhap__khieu_nai",
"khoa_tai_khoan__huong_dan__online": "khoa_tai_khoan__huong_dan",
"khoa_tai_khoan__huong_dan__tai_quay": "khoa_tai_khoan__huong_dan",
"khoa_tai_khoan__khieu_nai__online": "khoa_tai_khoan__khieu_nai",
"khoa_tai_khoan__khieu_nai__tai_quay": "khoa_tai_khoan__khieu_nai",
"thanh_toan__huong_dan__online": "thanh_toan__huong_dan",
"thanh_toan__huong_dan__tai_quay": "thanh_toan__huong_dan",
"thanh_toan__khieu_nai__online": "thanh_toan__khieu_nai",
"thanh_toan__khieu_nai__tai_quay": "thanh_toan__khieu_nai",
"tra_cuu_no__huong_dan__online": "tra_cuu_no__huong_dan",
"tra_cuu_no__huong_dan__tai_quay": "tra_cuu_no__huong_dan",
"tra_cuu_no__khieu_nai__online": "tra_cuu_no__khieu_nai",
"tra_cuu_no__khieu_nai__tai_quay": "tra_cuu_no__khieu_nai",
"dang_ky_tu_dong__huong_dan__online": "dang_ky_tu_dong__huong_dan",
"dang_ky_tu_dong__huong_dan__tai_quay": "dang_ky_tu_dong__huong_dan",
"dang_ky_tu_dong__khieu_nai__online": "dang_ky_tu_dong__khieu_nai",
"dang_ky_tu_dong__khieu_nai__tai_quay": "dang_ky_tu_dong__khieu_nai"
}
}
\ No newline at end of file
"""Khối preprocess của ensemble: câu -> input_ids. Dùng chung text_preprocess.py với lúc train."""
import os
import sys
import numpy as np
import triton_python_backend_utils as pb_utils
class TritonPythonModel:
def initialize(self, args):
sys.path.insert(0, os.path.join(args["model_repository"], args["model_version"]))
import text_preprocess
self.tp = text_preprocess
def execute(self, requests):
responses = []
for req in requests:
text = pb_utils.get_input_tensor_by_name(req, "INPUT_TEXT").as_numpy().reshape(-1)[0]
if isinstance(text, bytes):
text = text.decode("utf-8")
ids = np.array([self.tp.encode(text)], dtype=np.int64)
responses.append(pb_utils.InferenceResponse(output_tensors=[pb_utils.Tensor("INPUT_IDS", ids)]))
return responses
"""Tiền xử lý dùng chung cho train (máy 106) và serving (khối preprocess_PhoBERT trên Triton).
Viết tương thích Python 3.8 vì Python backend của Triton 23.03 chạy 3.8.
Bản mô phỏng: chuẩn hoá bỏ dấu, tách từ theo khoảng trắng, băm unigram + bigram ra id.
"""
import re
import unicodedata
import zlib
MAX_SEQ_LENGTH = 32
VOCAB_SIZE = 50000
_TOKEN_RE = re.compile(r"[a-z0-9]+")
def normalize(text):
"""Chữ thường, bỏ dấu tiếng Việt, chỉ giữ chữ và số."""
text = str(text).lower().replace("đ", "d")
text = unicodedata.normalize("NFD", text)
text = "".join(c for c in text if unicodedata.category(c) != "Mn")
return " ".join(_TOKEN_RE.findall(text))
def tokens(text):
words = normalize(text).split()
return words + [a + "_" + b for a, b in zip(words, words[1:])]
def _hash(tok):
return zlib.crc32(tok.encode("utf-8")) % (VOCAB_SIZE - 1) + 1
def encode(text, max_len=MAX_SEQ_LENGTH):
"""Trả list id độ dài max_len; 0 là đệm."""
ids = [_hash(t) for t in tokens(text)][:max_len]
return ids + [0] * (max_len - len(ids))
name: "preprocess_PhoBERT"
backend: "python"
max_batch_size: 0
input [
{ name: "INPUT_TEXT" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "BOT_ID" data_type: TYPE_STRING dims: [ 1 ] }
]
output [
{ name: "INPUT_IDS" data_type: TYPE_INT64 dims: [ 1, 32 ] }
]
instance_group [ { count: 1 kind: KIND_CPU } ]
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment