Views
No views yet
pip install scikit-learn>=1.6 joblib1# Default training with VNTC dataset
2python train.py --dataset vntc --model logistic
3
4# With specific parameters
5python train.py --dataset vntc --model logistic --max-features 20000 --ngram-min 1 --ngram-max 21# Train with UTS2017_Bank dataset (SVC recommended)
2python train.py --dataset uts2017 --model svc_linear
3
4# Train with Logistic Regression
5python train.py --dataset uts2017 --model logistic
6
7# With specific parameters (SVC)
8python train.py --dataset uts2017 --model svc_linear --max-features 20000 --ngram-min 1 --ngram-max 2
9
10# Compare multiple configurations
11python train.py --dataset uts2017 --compare1from train import train_notebook
2
3# Train VNTC model
4vntc_results = train_notebook(
5 dataset="vntc",
6 model_name="logistic",
7 max_features=20000,
8 ngram_min=1,
9 ngram_max=2
10)
11
12# Train UTS2017_Bank model
13bank_results = train_notebook(
14 dataset="uts2017",
15 model_name="logistic",
16 max_features=20000,
17 ngram_min=1,
18 ngram_max=2
19)1from huggingface_hub import hf_hub_download
2import joblib
3
4# Download and load VNTC model
5vntc_model = joblib.load(
6 hf_hub_download("undertheseanlp/sonar_core_1", "vntc_classifier_20250927_161550.joblib")
7)
8
9# Enhanced prediction function
10def predict_text(model, text):
11 probabilities = model.predict_proba([text])[0]
12
13 # Get top 3 predictions sorted by probability
14 top_indices = probabilities.argsort()[-3:][::-1]
15 top_predictions = []
16 for idx in top_indices:
17 category = model.classes_[idx]
18 prob = probabilities[idx]
19 top_predictions.append((category, prob))
20
21 # The prediction should be the top category
22 prediction = top_predictions[0][0]
23 confidence = top_predictions[0][1]
24
25 return prediction, confidence, top_predictions
26
27# Make prediction on news text
28news_text = "Đội tuyển bóng đá Việt Nam giành chiến thắng"
29prediction, confidence, top_predictions = predict_text(vntc_model, news_text)
30
31print(f"News category: {prediction}")
32print(f"Confidence: {confidence:.3f}")
33print("Top 3 predictions:")
34for i, (category, prob) in enumerate(top_predictions, 1):
35 print(f" {i}. {category}: {prob:.3f}")1from huggingface_hub import hf_hub_download
2import joblib
3
4# Download and load UTS2017_Bank model (latest SVC model)
5bank_model = joblib.load(
6 hf_hub_download("undertheseanlp/sonar_core_1", "uts2017_bank_classifier_20250928_060819.joblib")
7)
8
9# Enhanced prediction function (same as above)
10def predict_text(model, text):
11 probabilities = model.predict_proba([text])[0]
12
13 # Get top 3 predictions sorted by probability
14 top_indices = probabilities.argsort()[-3:][::-1]
15 top_predictions = []
16 for idx in top_indices:
17 category = model.classes_[idx]
18 prob = probabilities[idx]
19 top_predictions.append((category, prob))
20
21 # The prediction should be the top category
22 prediction = top_predictions[0][0]
23 confidence = top_predictions[0][1]
24
25 return prediction, confidence, top_predictions
26
27# Make prediction on banking text
28bank_text = "Tôi muốn mở tài khoản tiết kiệm"
29prediction, confidence, top_predictions = predict_text(bank_model, bank_text)
30
31print(f"Banking category: {prediction}")
32print(f"Confidence: {confidence:.3f}")
33print("Top 3 predictions:")
34for i, (category, prob) in enumerate(top_predictions, 1):
35 print(f" {i}. {category}: {prob:.3f}")1from huggingface_hub import hf_hub_download
2import joblib
3
4# Load both models
5vntc_model = joblib.load(
6 hf_hub_download("undertheseanlp/sonar_core_1", "vntc_classifier_20250927_161550.joblib")
7)
8bank_model = joblib.load(
9 hf_hub_download("undertheseanlp/sonar_core_1", "uts2017_bank_classifier_20250928_060819.joblib")
10)
11
12# Enhanced prediction function for both models
13def predict_text(model, text):
14 probabilities = model.predict_proba([text])[0]
15
16 # Get top 3 predictions sorted by probability
17 top_indices = probabilities.argsort()[-3:][::-1]
18 top_predictions = []
19 for idx in top_indices:
20 category = model.classes_[idx]
21 prob = probabilities[idx]
22 top_predictions.append((category, prob))
23
24 # The prediction should be the top category
25 prediction = top_predictions[0][0]
26 confidence = top_predictions[0][1]
27
28 return prediction, confidence, top_predictions
29
30# Function to classify any Vietnamese text
31def classify_vietnamese_text(text, domain="auto"):
32 """
33 Classify Vietnamese text using appropriate model with detailed predictions
34
35 Args:
36 text: Vietnamese text to classify
37 domain: "news", "banking", or "auto" to detect domain
38
39 Returns:
40 tuple: (prediction, confidence, top_predictions, domain_used)
41 """
42 if domain == "news":
43 prediction, confidence, top_predictions = predict_text(vntc_model, text)
44 return prediction, confidence, top_predictions, "news"
45 elif domain == "banking":
46 prediction, confidence, top_predictions = predict_text(bank_model, text)
47 return prediction, confidence, top_predictions, "banking"
48 else:
49 # Try both models and return higher confidence
50 news_pred, news_conf, news_top = predict_text(vntc_model, text)
51 bank_pred, bank_conf, bank_top = predict_text(bank_model, text)
52
53 if news_conf > bank_conf:
54 return f"NEWS: {news_pred}", news_conf, news_top, "news"
55 else:
56 return f"BANKING: {bank_pred}", bank_conf, bank_top, "banking"
57
58# Examples
59examples = [
60 "Đội tuyển bóng đá Việt Nam thắng 2-0",
61 "Tôi muốn vay tiền mua nhà",
62 "Chính phủ thông qua luật mới"
63]
64
65for text in examples:
66 category, confidence, top_predictions, domain = classify_vietnamese_text(text)
67 print(f"Text: {text}")
68 print(f"Category: {category}")
69 print(f"Confidence: {confidence:.3f}")
70 print(f"Domain: {domain}")
71 print("Top 3 predictions:")
72 for i, (cat, prob) in enumerate(top_predictions, 1):
73 print(f" {i}. {cat}: {prob:.3f}")
74 print()dataset: Dataset to use ("vntc" or "uts2017")model: Model type ("logistic" or "svc" - SVC recommended for best performance)max_features: Maximum number of TF-IDF features (default: 20000)ngram_min/max: N-gram range (default: 1-2)split_ratio: Train/test split ratio for UTS2017 (default: 0.2)n_samples: Optional sample limit for quick testing1@misc{undertheseanlp_2025,
2 author = { undertheseanlp },
3 title = { Sonar Core 1 - Vietnamese Text Classification Model },
4 year = 2025,
5 url = { https://huggingface.co/undertheseanlp/sonar_core_1 },
6 doi = { 10.57967/hf/6599 },
7 publisher = { Hugging Face }
8}