File size: 6,401 Bytes
980fef7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import argparse
import json
import os
from pathlib import Path
import sys
import time
from datetime import datetime

import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline


ROOT_DIR = Path(__file__).resolve().parents[2]
if str(ROOT_DIR) not in sys.path:
    sys.path.insert(0, str(ROOT_DIR))


BASE_DIR = Path(__file__).resolve().parent
DEFAULT_DATASET = BASE_DIR / "intent_dataset.json"
GENERATED_QA_DIR = BASE_DIR / "generated_qa"
ARTIFACT_DIR = BASE_DIR / "artifacts"
LOG_DIR = ROOT_DIR / "logs" / "intent"
ARTIFACT_DIR.mkdir(parents=True, exist_ok=True)
LOG_DIR.mkdir(parents=True, exist_ok=True)


def load_dataset(path: Path):
    payload = json.loads(path.read_text(encoding="utf-8"))
    texts = []
    labels = []
    for intent in payload.get("intents", []):
        name = intent["name"]
        for example in intent.get("examples", []):
            texts.append(example)
            labels.append(name)
    return texts, labels, payload


def load_generated_qa(directory: Path):
    """
    Load generated QA questions as additional intent training samples.

    Each JSON file is expected to contain a list of objects compatible
    with `QAItem` from `generated_qa`, at minimum having:
      - question: str
      - intent: str
    """
    texts: list[str] = []
    labels: list[str] = []

    if not directory.exists():
        return texts, labels

    for path in sorted(directory.glob("*.json")):
        try:
            payload = json.loads(path.read_text(encoding="utf-8"))
        except Exception:
            # Skip malformed files but continue loading others
            continue
        if not isinstance(payload, list):
            continue
        for item in payload:
            if not isinstance(item, dict):
                continue
            question = str(item.get("question") or "").strip()
            intent = str(item.get("intent") or "").strip() or "search_legal"
            if not question:
                continue
            texts.append(question)
            labels.append(intent)
    return texts, labels


def load_combined_dataset(path: Path, generated_dir: Path):
    """
    Load seed intent dataset and merge with generated QA questions.
    """
    texts, labels, meta = load_dataset(path)
    gen_texts, gen_labels = load_generated_qa(generated_dir)

    texts.extend(gen_texts)
    labels.extend(gen_labels)
    return texts, labels, meta


def build_pipelines():
    vectorizer = TfidfVectorizer(
        analyzer="word",
        ngram_range=(1, 2),
        lowercase=True,
        token_pattern=r"\b\w+\b",
    )

    nb_pipeline = Pipeline([
        ("tfidf", vectorizer),
        ("clf", MultinomialNB()),
    ])

    logreg_pipeline = Pipeline([
        ("tfidf", vectorizer),
        ("clf", LogisticRegression(max_iter=1000, solver="lbfgs")),
    ])

    return {
        "multinomial_nb": nb_pipeline,
        "logistic_regression": logreg_pipeline,
    }


def train(dataset_path: Path, test_size: float = 0.2, random_state: int = 42):
    texts, labels, meta = load_combined_dataset(dataset_path, GENERATED_QA_DIR)
    if not texts:
        raise ValueError("Dataset rỗng, không thể huấn luyện")

    X_train, X_test, y_train, y_test = train_test_split(
        texts, labels, test_size=test_size, random_state=random_state, stratify=labels
    )

    pipelines = build_pipelines()
    best_model = None
    best_metrics = None

    for name, pipeline in pipelines.items():
        start = time.perf_counter()
        pipeline.fit(X_train, y_train)
        train_duration = time.perf_counter() - start

        y_pred = pipeline.predict(X_test)
        acc = accuracy_score(y_test, y_pred)
        report = classification_report(y_test, y_pred, output_dict=True)
        cm = confusion_matrix(y_test, y_pred, labels=sorted(set(labels)))

        metrics = {
            "model": name,
            "accuracy": acc,
            "train_duration_sec": train_duration,
            "classification_report": report,
            "confusion_matrix": cm.tolist(),
            "labels": sorted(set(labels)),
            "dataset_version": meta.get("version"),
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "test_size": test_size,
            "samples": len(texts),
        }

        if best_model is None or acc > best_metrics["accuracy"]:
            best_model = pipeline
            best_metrics = metrics

    assert best_model is not None

    model_path = ARTIFACT_DIR / "intent_model.joblib"
    metrics_path = ARTIFACT_DIR / "metrics.json"
    joblib.dump(best_model, model_path)
    metrics_path.write_text(json.dumps(best_metrics, ensure_ascii=False, indent=2), encoding="utf-8")

    log_entry = {
        "event": "train_intent",
        "model": best_metrics["model"],
        "accuracy": best_metrics["accuracy"],
        "timestamp": best_metrics["timestamp"],
        "samples": best_metrics["samples"],
        "dataset_version": best_metrics["dataset_version"],
        "artifact": str(model_path.relative_to(ROOT_DIR)),
    }

    log_file = LOG_DIR / "train.log"
    with log_file.open("a", encoding="utf-8") as fh:
        fh.write(json.dumps(log_entry, ensure_ascii=False) + "\n")

    return model_path, metrics_path, best_metrics


def parse_args():
    parser = argparse.ArgumentParser(description="Huấn luyện model intent cho chatbot")
    parser.add_argument("--dataset", type=Path, default=DEFAULT_DATASET, help="Đường dẫn tới intent_dataset.json")
    parser.add_argument("--test-size", type=float, default=0.2, help="Tỉ lệ dữ liệu test")
    parser.add_argument("--seed", type=int, default=42, help="Giá trị random seed")
    return parser.parse_args()


def main():
    args = parse_args()
    model_path, metrics_path, metrics = train(args.dataset, test_size=args.test_size, random_state=args.seed)
    print("Huấn luyện hoàn tất:")
    print(f"  Model: {metrics['model']}")
    print(f"  Accuracy: {metrics['accuracy']:.4f}")
    print(f"  Model artifact: {model_path}")
    print(f"  Metrics: {metrics_path}")


if __name__ == "__main__":
    main()