File size: 10,690 Bytes
3715499
 
 
1a2922b
3715499
 
 
 
742fa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3715499
 
1a2922b
 
 
 
 
 
 
9cf063d
1a2922b
 
9cf063d
1a2922b
 
 
 
 
10d299f
3715499
10d299f
 
 
3715499
 
 
 
 
1a2922b
3715499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
742fa4d
 
 
 
 
 
 
 
 
3715499
742fa4d
3715499
 
 
 
 
 
 
 
 
1a2922b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
742fa4d
 
 
 
 
 
 
 
 
1a2922b
742fa4d
1a2922b
 
 
 
 
 
 
 
 
 
3715499
1a2922b
3715499
1a2922b
3715499
 
 
 
 
 
 
 
 
 
 
 
 
742fa4d
3715499
742fa4d
 
 
3715499
742fa4d
3715499
742fa4d
3715499
 
 
 
 
 
 
 
 
1a2922b
742fa4d
3715499
742fa4d
3715499
 
1a2922b
3715499
1a2922b
3715499
 
 
742fa4d
1a2922b
9cf063d
3715499
 
1a2922b
 
 
 
 
 
742fa4d
1a2922b
9cf063d
1a2922b
3715499
1a2922b
 
 
 
742fa4d
 
3715499
 
 
 
 
1a2922b
3715499
 
 
1a2922b
 
3715499
1a2922b
 
 
 
3715499
1a2922b
 
 
 
 
 
 
 
 
 
3715499
 
 
 
 
1a2922b
 
 
 
 
 
 
 
 
 
 
3715499
 
 
 
 
1a2922b
9cf063d
 
3715499
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Demonstration script for using Sonar Core 1 models from Hugging Face Hub.
Shows how to download and use both VNTC and UTS2017_Bank pre-trained models.
"""

from huggingface_hub import hf_hub_download
import joblib


def predict_text(model, text):
    """Make prediction on a single text (consistent with inference.py)"""
    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 load_model_from_hub(model_type="vntc"):
    """Load the pre-trained model from Hugging Face Hub

    Args:
        model_type: "vntc" for news classification or "uts2017_bank" for banking text
    """
    if model_type == "vntc":
        filename = "vntc_classifier_20250927_161550.joblib"
        print("Downloading VNTC (Vietnamese News) model from Hugging Face Hub...")
    elif model_type == "uts2017_bank":
        filename = "uts2017_bank_classifier_20250927_161733.joblib"
        print("Downloading UTS2017_Bank (Vietnamese Banking) model from Hugging Face Hub...")
    else:
        raise ValueError("model_type must be 'vntc' or 'uts2017_bank'")

    model_path = hf_hub_download("undertheseanlp/sonar_core_1", filename)
    print(f"Model downloaded to: {model_path}")

    print("Loading model...")
    model = joblib.load(model_path)
    return model


def predict_vntc_examples(model):
    """Demonstrate predictions on VNTC (news) examples"""
    print("\n" + "="*60)
    print("VIETNAMESE NEWS CLASSIFICATION EXAMPLES (VNTC)")
    print("="*60)

    # Vietnamese news examples for different categories
    examples = [
        ("Chính trị & Xã hội", "Chính phủ đã thông qua nghị định mới về chính sách xã hội"),
        ("Đời sống", "Xu hướng ăn uống lành mạnh đang được nhiều người quan tâm"),
        ("Khoa học", "Các nhà khoa học đã phát hiện ra loại vi khuẩn mới"),
        ("Kinh doanh", "Thị trường chứng khoán có nhiều biến động trong tuần qua"),
        ("Pháp luật", "Luật an toàn giao thông sẽ có hiệu lực từ tháng sau"),
        ("Sức khỏe", "Tiêm vaccine phòng chống COVID-19 đã đạt tỷ lệ cao"),
        ("Thế giới", "Hội nghị thượng đỉnh quốc tế sẽ diễn ra tại Geneva"),
        ("Thể thao", "Đội tuyển bóng đá Việt Nam giành chiến thắng 2-0"),
        ("Văn hóa", "Lễ hội truyền thống sẽ được tổ chức vào cuối tuần"),
        ("Vi tính", "Công nghệ trí tuệ nhân tạo đang phát triển mạnh mẽ")
    ]

    print("Testing Vietnamese news classification:")
    print("-" * 60)

    for expected_category, text in examples:
        try:
            prediction, confidence, top_predictions = predict_text(model, text)

            if prediction:
                print(f"Text: {text}")
                print(f"Expected: {expected_category}")
                print(f"Predicted: {prediction}")
                print(f"Confidence: {confidence:.3f}")

                # Show top 3 predictions
                print("Top 3 predictions:")
                for i, (category, prob) in enumerate(top_predictions, 1):
                    print(f"  {i}. {category}: {prob:.3f}")

            print("-" * 60)

        except Exception as e:
            print(f"Error predicting '{text[:30]}...': {e}")
            print("-" * 60)


def predict_uts2017_examples(model):
    """Demonstrate predictions on UTS2017_Bank examples"""
    print("\n" + "="*60)
    print("VIETNAMESE BANKING TEXT CLASSIFICATION EXAMPLES (UTS2017_Bank)")
    print("="*60)

    # Vietnamese banking examples for different categories
    examples = [
        ("ACCOUNT", "Tôi muốn mở tài khoản tiết kiệm mới"),
        ("CARD", "Thẻ tín dụng của tôi bị khóa, làm sao để mở lại?"),
        ("CUSTOMER_SUPPORT", "Tôi cần hỗ trợ về dịch vụ ngân hàng"),
        ("DISCOUNT", "Có chương trình giảm giá nào cho khách hàng không?"),
        ("INTEREST_RATE", "Lãi suất tiết kiệm hiện tại là bao nhiều?"),
        ("INTERNET_BANKING", "Làm thế nào để đăng ký internet banking?"),
        ("LOAN", "Tôi muốn vay mua nhà với lãi suất ưu đãi"),
        ("MONEY_TRANSFER", "Chi phí chuyển tiền ra nước ngoài là bao nhiều?"),
        ("OTHER", "Tôi có câu hỏi về dịch vụ khác"),
        ("PAYMENT", "Thanh toán hóa đơn điện nước qua ngân hàng"),
        ("PROMOTION", "Khuyến mãi tháng này có gì hấp dẫn?"),
        ("SAVING", "Gói tiết kiệm nào có lãi suất cao nhất?"),
        ("SECURITY", "Bảo mật tài khoản ngân hàng như thế nào?"),
        ("TRADEMARK", "Ngân hàng ACB có uy tín không?")
    ]

    print("Testing Vietnamese banking text classification:")
    print("-" * 60)

    for expected_category, text in examples:
        try:
            prediction, confidence, top_predictions = predict_text(model, text)

            if prediction:
                print(f"Text: {text}")
                print(f"Expected: {expected_category}")
                print(f"Predicted: {prediction}")
                print(f"Confidence: {confidence:.3f}")

                # Show top 3 predictions
                print("Top 3 predictions:")
                for i, (category, prob) in enumerate(top_predictions, 1):
                    print(f"  {i}. {category}: {prob:.3f}")

            print("-" * 60)

        except Exception as e:
            print(f"Error predicting '{text}': {e}")
            print("-" * 60)


def interactive_mode(model, model_type):
    """Interactive mode for testing custom text"""
    dataset_name = "VNTC (News)" if model_type == "vntc" else "UTS2017_Bank (Banking)"
    print("\n" + "="*60)
    print(f"INTERACTIVE MODE - {dataset_name.upper()} CLASSIFICATION")
    print("="*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}")

                # Show top 3 predictions
                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 simple_usage_examples():
    """Show simple usage examples for HuggingFace Hub models"""
    print("\n" + "="*60)
    print("HUGGINGFACE HUB USAGE EXAMPLES")
    print("="*60)

    print("Code examples:")
    print("""
# VNTC Model (Vietnamese News Classification)
from huggingface_hub import hf_hub_download
import joblib

# Download and load VNTC model from HuggingFace Hub
vntc_model = joblib.load(
    hf_hub_download("undertheseanlp/sonar_core_1", "vntc_classifier_20250927_161550.joblib")
)

# Make prediction on news text
news_text = "Đội tuyển bóng đá Việt Nam giành chiến thắng"
prediction = vntc_model.predict([news_text])[0]
print(f"News category: {prediction}")

# UTS2017_Bank Model (Vietnamese Banking Text Classification)
# Download and load UTS2017_Bank model from HuggingFace Hub
bank_model = joblib.load(
    hf_hub_download("undertheseanlp/sonar_core_1", "uts2017_bank_classifier_20250927_161733.joblib")
)

# Make prediction on banking text
bank_text = "Tôi muốn mở tài khoản tiết kiệm"
prediction = bank_model.predict([bank_text])[0]
print(f"Banking category: {prediction}")

# For local file inference, use inference.py instead
""")


def main():
    """Main demonstration function"""
    print("Sonar Core 1 - Dual Model Hugging Face Hub Usage")
    print("=" * 60)

    try:
        # Show simple usage examples
        simple_usage_examples()

        # Test VNTC model
        print("\n" + "="*60)
        print("TESTING VNTC MODEL (Vietnamese News Classification)")
        print("="*60)

        vntc_model = load_model_from_hub("vntc")
        predict_vntc_examples(vntc_model)

        # Test UTS2017_Bank model
        print("\n" + "="*60)
        print("TESTING UTS2017_BANK MODEL (Vietnamese Banking Text Classification)")
        print("="*60)

        bank_model = load_model_from_hub("uts2017_bank")
        predict_uts2017_examples(bank_model)

        # Check if we're in an interactive environment
        try:
            import sys
            if hasattr(sys, 'ps1') or sys.stdin.isatty():
                print("\nAvailable interactive modes:")
                print("1. VNTC (News) classification")
                print("2. UTS2017_Bank (Banking) classification")

                choice = input("\nSelect model for interactive mode (1/2) or 'n' to skip: ").strip()

                if choice == "1":
                    interactive_mode(vntc_model, "vntc")
                elif choice == "2":
                    interactive_mode(bank_model, "uts2017_bank")

        except (EOFError, OSError):
            print("\nInteractive mode not available in this environment.")
            print("Run this script in a regular terminal to use interactive mode.")

        print("\nDemonstration complete!")
        print("\nBoth models are now available on Hugging Face Hub:")
        print("- VNTC (News): vntc_classifier_20250927_161550.joblib")
        print("- UTS2017_Bank (Banking): uts2017_bank_classifier_20250927_161733.joblib")

    except ImportError:
        print("Error: huggingface_hub is required. Install with:")
        print("  pip install huggingface_hub")
    except Exception as e:
        print(f"Error loading model: {e}")
        print("\nMake sure you have internet connection and try again.")


if __name__ == "__main__":
    main()