File size: 9,754 Bytes
05cf955 |
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
#!/usr/bin/env python3
"""
Inference script for Sonar Core 1 - Vietnamese Text Classification.
Loads trained models from local files and performs predictions.
"""
import argparse
import joblib
import os
import glob
def find_local_models():
"""Find all available local model files"""
models = {
'exported': {},
'runs': {}
}
# Find exported models in project root
for filename in os.listdir('.'):
if filename.endswith('.joblib'):
if filename.startswith('vntc_classifier_'):
models['exported']['vntc'] = filename
elif filename.startswith('uts2017_bank_classifier_'):
models['exported']['uts2017_bank'] = filename
# Find models in runs directory
vntc_runs = glob.glob('runs/*/models/VNTC_*.joblib')
bank_runs = glob.glob('runs/*/models/UTS2017_Bank_*.joblib')
if vntc_runs:
models['runs']['vntc'] = sorted(vntc_runs)[-1] # Most recent
if bank_runs:
models['runs']['uts2017_bank'] = sorted(bank_runs)[-1] # Most recent
return models
def load_model(model_path):
"""Load a model from file path"""
try:
print(f"Loading model from: {model_path}")
model = joblib.load(model_path)
print(f"Model loaded successfully. Classes: {len(model.classes_)}")
return model
except Exception as e:
print(f"Error loading model: {e}")
return None
def predict_text(model, text):
"""Make prediction on a single text"""
try:
probabilities = model.predict_proba([text])[0]
# Get top 3 predictions sorted by probability
top_indices = probabilities.argsort()[-3:][::-1]
top_predictions = []
for idx in top_indices:
category = model.classes_[idx]
prob = probabilities[idx]
top_predictions.append((category, prob))
# The prediction should be the top category
prediction = top_predictions[0][0]
confidence = top_predictions[0][1]
return prediction, confidence, top_predictions
except Exception as e:
print(f"Error making prediction: {e}")
return None, 0, []
def interactive_mode(model, dataset_name):
"""Interactive prediction mode"""
print(f"\n{'='*60}")
print(f"INTERACTIVE MODE - {dataset_name.upper()} CLASSIFICATION")
print(f"{'='*60}")
print("Enter Vietnamese text to classify (type 'quit' to exit):")
while True:
try:
user_input = input("\nText: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
break
if not user_input:
continue
prediction, confidence, top_predictions = predict_text(model, user_input)
if prediction:
print(f"Predicted category: {prediction}")
print(f"Confidence: {confidence:.3f}")
print("Top 3 predictions:")
for i, (category, prob) in enumerate(top_predictions, 1):
print(f" {i}. {category}: {prob:.3f}")
except KeyboardInterrupt:
print("\nExiting...")
break
except Exception as e:
print(f"Error: {e}")
def test_examples(model, dataset_name):
"""Test model with predefined examples"""
if dataset_name == 'vntc':
examples = [
"Đội tuyển bóng đá Việt Nam giành chiến thắng 2-0",
"Chính phủ thông qua nghị định mới về chính sách xã hội",
"Các nhà khoa học phát hiện loại vi khuẩn mới",
"Thị trường chứng khoán biến động mạnh",
"Tiêm vaccine COVID-19 đạt tỷ lệ cao",
"Công nghệ trí tuệ nhân tạo phát triển mạnh"
]
else: # uts2017_bank
examples = [
"Tôi muốn mở tài khoản tiết kiệm mới",
"Lãi suất vay mua nhà hiện tại là bao nhiều?",
"Làm thế nào để đăng ký internet banking?",
"Chi phí chuyển tiền ra nước ngoài",
"Ngân hàng ACB có uy tín không?",
"Tôi cần hỗ trợ về dịch vụ ngân hàng"
]
print(f"\n{'='*60}")
print(f"TESTING {dataset_name.upper()} MODEL WITH EXAMPLES")
print(f"{'='*60}")
for text in examples:
prediction, confidence, top_predictions = predict_text(model, text)
if prediction:
print(f"\nText: {text}")
print(f"Prediction: {prediction}")
print(f"Confidence: {confidence:.3f}")
# Show top 3 if confidence is low
if confidence < 0.7:
print("Alternative predictions:")
for i, (category, prob) in enumerate(top_predictions[:3], 1):
print(f" {i}. {category}: {prob:.3f}")
print("-" * 60)
def list_available_models():
"""List all available models"""
models = find_local_models()
print("Available Models:")
print("=" * 50)
if models['exported']:
print("\nExported Models (Project Root):")
for dataset, filename in models['exported'].items():
file_size = os.path.getsize(filename) / (1024 * 1024) # MB
print(f" {dataset}: {filename} ({file_size:.1f}MB)")
if models['runs']:
print("\nRuns Models (Training Directory):")
for dataset, filepath in models['runs'].items():
file_size = os.path.getsize(filepath) / (1024 * 1024) # MB
print(f" {dataset}: {filepath} ({file_size:.1f}MB)")
if not models['exported'] and not models['runs']:
print("No local models found!")
print("Train a model first using: python train.py --export-model")
print("Or download from HuggingFace using: python use_this_model.py")
def main():
"""Main function"""
parser = argparse.ArgumentParser(
description="Inference with local Sonar Core 1 models"
)
parser.add_argument(
"--model-path",
type=str,
help="Path to specific model file"
)
parser.add_argument(
"--dataset",
type=str,
choices=["vntc", "uts2017_bank"],
help="Dataset type (auto-detects if not specified)"
)
parser.add_argument(
"--text",
type=str,
help="Text to classify (if not provided, enters interactive mode)"
)
parser.add_argument(
"--test-examples",
action="store_true",
help="Test with predefined examples"
)
parser.add_argument(
"--list-models",
action="store_true",
help="List all available local models"
)
parser.add_argument(
"--source",
type=str,
choices=["exported", "runs"],
default="exported",
help="Model source: exported files or runs directory (default: exported)"
)
args = parser.parse_args()
# List models and exit
if args.list_models:
list_available_models()
return
# Find available models
models = find_local_models()
# Determine model path
model_path = None
dataset_name = args.dataset
if args.model_path:
# Use specified model path
model_path = args.model_path
# Try to infer dataset from filename
if not dataset_name:
if 'vntc' in args.model_path.lower():
dataset_name = 'vntc'
elif 'uts2017' in args.model_path.lower() or 'bank' in args.model_path.lower():
dataset_name = 'uts2017_bank'
else:
# Auto-select model
if args.dataset:
# Use specified dataset
if args.dataset in models[args.source]:
model_path = models[args.source][args.dataset]
dataset_name = args.dataset
else:
print(f"No {args.dataset} model found in {args.source} models")
list_available_models()
return
else:
# Use first available model
if models[args.source]:
dataset_name = list(models[args.source].keys())[0]
model_path = models[args.source][dataset_name]
print(f"Auto-selected {dataset_name} model")
else:
print("No models found!")
list_available_models()
return
if not model_path or not os.path.exists(model_path):
print(f"Model file not found: {model_path}")
list_available_models()
return
# Load model
model = load_model(model_path)
if not model:
return
# Process based on arguments
if args.text:
# Single prediction
prediction, confidence, top_predictions = predict_text(model, args.text)
if prediction:
print(f"\nText: {args.text}")
print(f"Prediction: {prediction}")
print(f"Confidence: {confidence:.3f}")
print("Top 3 predictions:")
for i, (category, prob) in enumerate(top_predictions, 1):
print(f" {i}. {category}: {prob:.3f}")
elif args.test_examples:
# Test with examples
test_examples(model, dataset_name)
else:
# Interactive mode
print(f"Loaded {dataset_name} model: {os.path.basename(model_path)}")
test_examples(model, dataset_name)
# Ask if user wants interactive mode
try:
response = input("\nEnter interactive mode? (y/n): ").strip().lower()
if response in ['y', 'yes']:
interactive_mode(model, dataset_name)
except KeyboardInterrupt:
print("\nExiting...")
if __name__ == "__main__":
main() |