File size: 12,989 Bytes
31e3523
 
6b2c2e0
31e3523
6b2c2e0
31e3523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2c2e0
 
 
31e3523
 
0b1c1cf
6b2c2e0
 
31e3523
6b2c2e0
0b1c1cf
6b2c2e0
 
 
 
 
 
 
0b1c1cf
6b2c2e0
 
 
0b1c1cf
6b2c2e0
0b1c1cf
 
 
6b2c2e0
31e3523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2c2e0
 
 
 
 
 
 
 
31e3523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2c2e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31e3523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2c2e0
31e3523
 
 
 
 
 
6b2c2e0
 
31e3523
 
 
 
 
6b2c2e0
 
31e3523
 
 
6b2c2e0
 
 
31e3523
 
 
 
 
6b2c2e0
 
 
 
 
 
 
 
31e3523
 
 
 
 
 
 
 
 
6b2c2e0
31e3523
 
 
 
6b2c2e0
31e3523
 
 
 
 
 
 
 
 
 
0b1c1cf
 
31e3523
 
 
 
 
 
 
 
 
 
 
 
6b2c2e0
31e3523
6b2c2e0
31e3523
 
 
 
6b2c2e0
 
 
 
 
31e3523
6b2c2e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31e3523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2c2e0
 
31e3523
 
 
 
 
 
 
 
 
 
 
 
 
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
"""
Inference script for Pulse Core 1 - Vietnamese Sentiment Analysis System.
Loads trained sentiment models from local files and performs predictions.
Supports both VLSP2016 general sentiment and UTS2017_Bank aspect sentiment models.
"""

import argparse
import joblib
import os
import glob


def find_local_models():
    """Find all available local sentiment model files"""
    models = {
        'exported': {},
        'runs': {}
    }

    # Find exported sentiment models in project root
    for filename in os.listdir('.'):
        if filename.endswith('.joblib'):
            if filename.startswith('vlsp2016_sentiment_'):
                models['exported']['vlsp2016_sentiment'] = filename
            elif filename.startswith('uts2017_sentiment_'):
                models['exported']['uts2017_sentiment'] = filename

    # Find models in runs directory - prioritize SVC models
    vlsp_runs = glob.glob('runs/*/models/VLSP2016_Sentiment_*.joblib')
    uts_runs = glob.glob('runs/*/models/UTS2017_Bank_AspectSentiment_*.joblib')

    if vlsp_runs:
        # Sort by modification time (most recent first)
        vlsp_runs.sort(key=lambda x: os.path.getmtime(x), reverse=True)
        # Prefer SVC models over other types
        svc_models = [m for m in vlsp_runs if 'SVC' in m]
        if svc_models:
            models['runs']['vlsp2016_sentiment'] = svc_models[0]  # Most recent SVC
        else:
            models['runs']['vlsp2016_sentiment'] = vlsp_runs[0]  # Most recent any model

    if uts_runs:
        # Sort by modification time (most recent first)
        uts_runs.sort(key=lambda x: os.path.getmtime(x), reverse=True)
        # Prefer SVC models over other types
        svc_models = [m for m in uts_runs if 'SVC' in m]
        if svc_models:
            models['runs']['uts2017_sentiment'] = svc_models[0]  # Most recent SVC
        else:
            models['runs']['uts2017_sentiment'] = uts_runs[0]  # Most recent any model

    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}")
    if dataset_name == 'vlsp2016_sentiment':
        print("INTERACTIVE MODE - VIETNAMESE GENERAL SENTIMENT ANALYSIS")
        print(f"{'='*60}")
        print("Enter Vietnamese text to analyze sentiment (type 'quit' to exit):")
    else:
        print("INTERACTIVE MODE - VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
        print(f"{'='*60}")
        print("Enter Vietnamese banking text to analyze aspect and sentiment (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 based on dataset type"""
    if dataset_name == 'vlsp2016_sentiment':
        examples = [
            "Sản phẩm này rất tốt, tôi rất hài lòng",
            "Chất lượng dịch vụ tệ quá",
            "Giá cả hợp lý, có thể chấp nhận được",
            "Nhân viên phục vụ rất nhiệt tình",
            "Đồ ăn không ngon, sẽ không quay lại",
            "Giao hàng nhanh chóng, đóng gói cẩn thận",
            "Sản phẩm bình thường, không có gì đặc biệt",
            "Rất đáng tiền, chất lượng tuyệt vời",
            "Không như mong đợi, khá thất vọng",
            "Dịch vụ khách hàng tốt, giải quyết nhanh chóng"
        ]
        print("\n" + "="*60)
        print("TESTING VIETNAMESE GENERAL SENTIMENT ANALYSIS")
        print("="*60)
    else:
        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 quá cao",
            "Làm thế nào để đăng ký internet banking?",
            "Chi phí chuyển tiền ra nước ngoài rất đắt",
            "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",
            "Thẻ tín dụng bị khóa không rõ lý do",
            "Dịch vụ chăm sóc khách hàng rất tệ",
            "Khuyến mãi tháng này rất hấp dẫn",
            "Bảo mật tài khoản có được đảm bảo không?"
        ]
        print("\n" + "="*60)
        print("TESTING VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
        print("="*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 sentiment models"""
    models = find_local_models()

    print("Available Vietnamese Sentiment Models:")
    print("=" * 50)

    if models['exported']:
        print("\nExported Models (Project Root):")
        for model_type, filename in models['exported'].items():
            file_size = os.path.getsize(filename) / (1024 * 1024)  # MB
            dataset_type = "General Sentiment" if "vlsp2016" in model_type else "Banking Aspect Sentiment"
            print(f"  {model_type}: {filename} ({file_size:.1f}MB) - {dataset_type}")

    if models['runs']:
        print("\nRuns Models (Training Directory):")
        for model_type, filepath in models['runs'].items():
            file_size = os.path.getsize(filepath) / (1024 * 1024)  # MB
            dataset_type = "General Sentiment" if "vlsp2016" in model_type else "Banking Aspect Sentiment"
            print(f"  {model_type}: {filepath} ({file_size:.1f}MB) - {dataset_type}")

    if not models['exported'] and not models['runs']:
        print("No local sentiment models found!")
        print("Train a model first using:")
        print("  VLSP2016: python train.py --dataset vlsp2016 --export-model")
        print("  UTS2017:  python train.py --dataset uts2017 --export-model")


def main():
    """Main function"""
    parser = argparse.ArgumentParser(
        description="Inference with local Pulse Core 1 Vietnamese sentiment models"
    )
    parser.add_argument(
        "--dataset",
        type=str,
        choices=["vlsp2016", "uts2017", "auto"],
        default="auto",
        help="Dataset type to use (default: auto-detect)"
    )
    parser.add_argument(
        "--model-path",
        type=str,
        help="Path to specific sentiment model file"
    )
    parser.add_argument(
        "--text",
        type=str,
        help="Vietnamese text to analyze (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 sentiment models"
    )
    parser.add_argument(
        "--source",
        type=str,
        choices=["exported", "runs"],
        default="runs",
        help="Model source: exported files or runs directory (default: runs)"
    )

    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 and dataset
    model_path = None
    dataset_name = None

    if args.model_path:
        # Use specified model path
        model_path = args.model_path
        # Try to detect dataset from filename
        if 'vlsp2016' in args.model_path:
            dataset_name = 'vlsp2016_sentiment'
        elif 'uts2017' in args.model_path:
            dataset_name = 'uts2017_sentiment'
        else:
            dataset_name = 'unknown'
    else:
        # Auto-select or use specified dataset
        if args.dataset == 'vlsp2016':
            if models[args.source] and 'vlsp2016_sentiment' in models[args.source]:
                model_path = models[args.source]['vlsp2016_sentiment']
                dataset_name = 'vlsp2016_sentiment'
                print("Selected VLSP2016 general sentiment model")
            else:
                print("No VLSP2016 models found!")
                list_available_models()
                return
        elif args.dataset == 'uts2017':
            if models[args.source] and 'uts2017_sentiment' in models[args.source]:
                model_path = models[args.source]['uts2017_sentiment']
                dataset_name = 'uts2017_sentiment'
                print("Selected UTS2017 banking aspect sentiment model")
            else:
                print("No UTS2017 models found!")
                list_available_models()
                return
        else:  # auto
            # Prefer VLSP2016 if available, otherwise UTS2017
            if models[args.source] and 'vlsp2016_sentiment' in models[args.source]:
                model_path = models[args.source]['vlsp2016_sentiment']
                dataset_name = 'vlsp2016_sentiment'
                print("Auto-selected VLSP2016 general sentiment model")
            elif models[args.source] and 'uts2017_sentiment' in models[args.source]:
                model_path = models[args.source]['uts2017_sentiment']
                dataset_name = 'uts2017_sentiment'
                print("Auto-selected UTS2017 banking aspect sentiment model")
            else:
                print("No sentiment 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
        model_type = "General Sentiment" if dataset_name == 'vlsp2016_sentiment' else "Banking Aspect Sentiment"
        print(f"Loaded {model_type} 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()