Vu Anh Claude commited on
Commit
1a2922b
·
1 Parent(s): 10d299f

Add UTS2017_Bank model and dual-model usage demonstration

Browse files

- Upload sklearn_model_uts2017_bank.joblib (UTS2017_Bank model, 70.96% accuracy)
- Update use_this_model.py to support both VNTC and UTS2017_Bank models
- Add separate examples for news and banking text classification
- Include interactive mode selection for both models
- Provide clear usage examples for both model types

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

sklearn_model_uts2017_bank.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:729e6e6d7b34dc1057275d15ce0d8475ffc1b614a13fbc0f174155e9dec4795d
3
+ size 3029656
use_this_model.py CHANGED
@@ -1,7 +1,7 @@
1
  #!/usr/bin/env python3
2
  """
3
  Demonstration script for using Sonar Core 1 models from Hugging Face Hub.
4
- Shows how to download and use the pre-trained Vietnamese text classification model.
5
  """
6
 
7
  from huggingface_hub import hf_hub_download
@@ -9,10 +9,22 @@ import joblib
9
  import numpy as np
10
 
11
 
12
- def load_model_from_hub():
13
- """Load the pre-trained model from Hugging Face Hub"""
14
- print("Downloading model from Hugging Face Hub...")
15
- model_path = hf_hub_download("undertheseanlp/sonar_core_1", "sklearn_model.joblib")
 
 
 
 
 
 
 
 
 
 
 
 
16
  print(f"Model downloaded to: {model_path}")
17
 
18
  print("Loading model...")
@@ -23,7 +35,7 @@ def load_model_from_hub():
23
  def predict_vntc_examples(model):
24
  """Demonstrate predictions on VNTC (news) examples"""
25
  print("\n" + "="*60)
26
- print("VIETNAMESE NEWS CLASSIFICATION EXAMPLES")
27
  print("="*60)
28
 
29
  # Vietnamese news examples for different categories
@@ -70,10 +82,65 @@ def predict_vntc_examples(model):
70
  print("-" * 60)
71
 
72
 
73
- def interactive_mode(model):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  """Interactive mode for testing custom text"""
 
75
  print("\n" + "="*60)
76
- print("INTERACTIVE MODE - VIETNAMESE TEXT CLASSIFICATION")
77
  print("="*60)
78
  print("Enter Vietnamese text to classify (type 'quit' to exit):")
79
 
@@ -110,60 +177,89 @@ def interactive_mode(model):
110
  print(f"Error: {e}")
111
 
112
 
113
- def simple_usage_example():
114
- """Show simple usage example"""
115
  print("\n" + "="*60)
116
- print("SIMPLE USAGE EXAMPLE")
117
  print("="*60)
118
 
119
- print("Code example:")
120
  print("""
 
121
  from huggingface_hub import hf_hub_download
122
  import joblib
123
 
124
- # Download and load model
125
- model = joblib.load(
126
  hf_hub_download("undertheseanlp/sonar_core_1", "sklearn_model.joblib")
127
  )
128
 
129
- # Make prediction
130
- text = "Việt Nam giành chiến thắng trong trận bán kết"
131
- prediction = model.predict([text])[0]
132
- probabilities = model.predict_proba([text])[0]
 
 
 
 
 
 
133
 
134
- print(f"Predicted category: {prediction}")
135
- print(f"Confidence: {max(probabilities):.3f}")
 
 
136
  """)
137
 
138
 
139
  def main():
140
  """Main demonstration function"""
141
- print("Sonar Core 1 - Hugging Face Hub Model Usage")
142
  print("=" * 60)
143
 
144
  try:
145
- # Load model from Hugging Face Hub
146
- model = load_model_from_hub()
147
 
148
- # Show simple usage example
149
- simple_usage_example()
 
 
150
 
151
- # Run prediction examples
152
- predict_vntc_examples(model)
 
 
 
 
 
 
 
 
153
 
154
  # Check if we're in an interactive environment
155
  try:
156
- # Try to get input to see if we can run interactive mode
157
  import sys
158
  if hasattr(sys, 'ps1') or sys.stdin.isatty():
159
- response = input("\nWould you like to try interactive mode? (y/n): ")
160
- if response.lower().startswith('y'):
161
- interactive_mode(model)
 
 
 
 
 
 
 
 
162
  except (EOFError, OSError):
163
  print("\nInteractive mode not available in this environment.")
164
  print("Run this script in a regular terminal to use interactive mode.")
165
 
166
  print("\nDemonstration complete!")
 
 
 
167
 
168
  except ImportError:
169
  print("Error: huggingface_hub is required. Install with:")
 
1
  #!/usr/bin/env python3
2
  """
3
  Demonstration script for using Sonar Core 1 models from Hugging Face Hub.
4
+ Shows how to download and use both VNTC and UTS2017_Bank pre-trained models.
5
  """
6
 
7
  from huggingface_hub import hf_hub_download
 
9
  import numpy as np
10
 
11
 
12
+ def load_model_from_hub(model_type="vntc"):
13
+ """Load the pre-trained model from Hugging Face Hub
14
+
15
+ Args:
16
+ model_type: "vntc" for news classification or "uts2017_bank" for banking text
17
+ """
18
+ if model_type == "vntc":
19
+ filename = "sklearn_model.joblib"
20
+ print("Downloading VNTC (Vietnamese News) model from Hugging Face Hub...")
21
+ elif model_type == "uts2017_bank":
22
+ filename = "sklearn_model_uts2017_bank.joblib"
23
+ print("Downloading UTS2017_Bank (Vietnamese Banking) model from Hugging Face Hub...")
24
+ else:
25
+ raise ValueError("model_type must be 'vntc' or 'uts2017_bank'")
26
+
27
+ model_path = hf_hub_download("undertheseanlp/sonar_core_1", filename)
28
  print(f"Model downloaded to: {model_path}")
29
 
30
  print("Loading model...")
 
35
  def predict_vntc_examples(model):
36
  """Demonstrate predictions on VNTC (news) examples"""
37
  print("\n" + "="*60)
38
+ print("VIETNAMESE NEWS CLASSIFICATION EXAMPLES (VNTC)")
39
  print("="*60)
40
 
41
  # Vietnamese news examples for different categories
 
82
  print("-" * 60)
83
 
84
 
85
+ def predict_uts2017_examples(model):
86
+ """Demonstrate predictions on UTS2017_Bank examples"""
87
+ print("\n" + "="*60)
88
+ print("VIETNAMESE BANKING TEXT CLASSIFICATION EXAMPLES (UTS2017_Bank)")
89
+ print("="*60)
90
+
91
+ # Vietnamese banking examples for different categories
92
+ examples = [
93
+ ("ACCOUNT", "Tôi muốn mở tài khoản tiết kiệm mới"),
94
+ ("CARD", "Thẻ tín dụng của tôi bị khóa, làm sao để mở lại?"),
95
+ ("CUSTOMER_SUPPORT", "Tôi cần hỗ trợ về dịch vụ ngân hàng"),
96
+ ("DISCOUNT", "Có chương trình giảm giá nào cho khách hàng không?"),
97
+ ("INTEREST_RATE", "Lãi suất tiết kiệm hiện tại là bao nhiều?"),
98
+ ("INTERNET_BANKING", "Làm thế nào để đăng ký internet banking?"),
99
+ ("LOAN", "Tôi muốn vay mua nhà với lãi suất ưu đãi"),
100
+ ("MONEY_TRANSFER", "Chi phí chuyển tiền ra nước ngoài là bao nhiều?"),
101
+ ("OTHER", "Tôi có câu hỏi về dịch vụ khác"),
102
+ ("PAYMENT", "Thanh toán hóa đơn điện nước qua ngân hàng"),
103
+ ("PROMOTION", "Khuyến mãi tháng này có gì hấp dẫn?"),
104
+ ("SAVING", "Gói tiết kiệm nào có lãi suất cao nhất?"),
105
+ ("SECURITY", "Bảo mật tài khoản ngân hàng như thế nào?"),
106
+ ("TRADEMARK", "Ngân hàng ACB có uy tín không?")
107
+ ]
108
+
109
+ print("Testing Vietnamese banking text classification:")
110
+ print("-" * 60)
111
+
112
+ for expected_category, text in examples:
113
+ try:
114
+ prediction = model.predict([text])[0]
115
+ probabilities = model.predict_proba([text])[0]
116
+ confidence = np.max(probabilities)
117
+
118
+ print(f"Text: {text}")
119
+ print(f"Expected: {expected_category}")
120
+ print(f"Predicted: {prediction}")
121
+ print(f"Confidence: {confidence:.3f}")
122
+
123
+ # Show top 3 predictions
124
+ if hasattr(model, 'classes_'):
125
+ top_indices = np.argsort(probabilities)[-3:][::-1]
126
+ print("Top 3 predictions:")
127
+ for i, idx in enumerate(top_indices, 1):
128
+ category = model.classes_[idx]
129
+ prob = probabilities[idx]
130
+ print(f" {i}. {category}: {prob:.3f}")
131
+
132
+ print("-" * 60)
133
+
134
+ except Exception as e:
135
+ print(f"Error predicting '{text}': {e}")
136
+ print("-" * 60)
137
+
138
+
139
+ def interactive_mode(model, model_type):
140
  """Interactive mode for testing custom text"""
141
+ dataset_name = "VNTC (News)" if model_type == "vntc" else "UTS2017_Bank (Banking)"
142
  print("\n" + "="*60)
143
+ print(f"INTERACTIVE MODE - {dataset_name.upper()} CLASSIFICATION")
144
  print("="*60)
145
  print("Enter Vietnamese text to classify (type 'quit' to exit):")
146
 
 
177
  print(f"Error: {e}")
178
 
179
 
180
+ def simple_usage_examples():
181
+ """Show simple usage examples for both models"""
182
  print("\n" + "="*60)
183
+ print("SIMPLE USAGE EXAMPLES")
184
  print("="*60)
185
 
186
+ print("Code examples:")
187
  print("""
188
+ # VNTC Model (Vietnamese News Classification)
189
  from huggingface_hub import hf_hub_download
190
  import joblib
191
 
192
+ # Download and load VNTC model
193
+ vntc_model = joblib.load(
194
  hf_hub_download("undertheseanlp/sonar_core_1", "sklearn_model.joblib")
195
  )
196
 
197
+ # Make prediction on news text
198
+ news_text = "Đội tuyển bóng đá Việt Nam giành chiến thắng"
199
+ prediction = vntc_model.predict([news_text])[0]
200
+ print(f"News category: {prediction}")
201
+
202
+ # UTS2017_Bank Model (Vietnamese Banking Text Classification)
203
+ # Download and load UTS2017_Bank model
204
+ bank_model = joblib.load(
205
+ hf_hub_download("undertheseanlp/sonar_core_1", "sklearn_model_uts2017_bank.joblib")
206
+ )
207
 
208
+ # Make prediction on banking text
209
+ bank_text = "Tôi muốn mở tài khoản tiết kiệm"
210
+ prediction = bank_model.predict([bank_text])[0]
211
+ print(f"Banking category: {prediction}")
212
  """)
213
 
214
 
215
  def main():
216
  """Main demonstration function"""
217
+ print("Sonar Core 1 - Dual Model Hugging Face Hub Usage")
218
  print("=" * 60)
219
 
220
  try:
221
+ # Show simple usage examples
222
+ simple_usage_examples()
223
 
224
+ # Test VNTC model
225
+ print("\n" + "="*60)
226
+ print("TESTING VNTC MODEL (Vietnamese News Classification)")
227
+ print("="*60)
228
 
229
+ vntc_model = load_model_from_hub("vntc")
230
+ predict_vntc_examples(vntc_model)
231
+
232
+ # Test UTS2017_Bank model
233
+ print("\n" + "="*60)
234
+ print("TESTING UTS2017_BANK MODEL (Vietnamese Banking Text Classification)")
235
+ print("="*60)
236
+
237
+ bank_model = load_model_from_hub("uts2017_bank")
238
+ predict_uts2017_examples(bank_model)
239
 
240
  # Check if we're in an interactive environment
241
  try:
 
242
  import sys
243
  if hasattr(sys, 'ps1') or sys.stdin.isatty():
244
+ print("\nAvailable interactive modes:")
245
+ print("1. VNTC (News) classification")
246
+ print("2. UTS2017_Bank (Banking) classification")
247
+
248
+ choice = input("\nSelect model for interactive mode (1/2) or 'n' to skip: ").strip()
249
+
250
+ if choice == "1":
251
+ interactive_mode(vntc_model, "vntc")
252
+ elif choice == "2":
253
+ interactive_mode(bank_model, "uts2017_bank")
254
+
255
  except (EOFError, OSError):
256
  print("\nInteractive mode not available in this environment.")
257
  print("Run this script in a regular terminal to use interactive mode.")
258
 
259
  print("\nDemonstration complete!")
260
+ print("\nBoth models are now available on Hugging Face Hub:")
261
+ print("- VNTC (News): sklearn_model.joblib")
262
+ print("- UTS2017_Bank (Banking): sklearn_model_uts2017_bank.joblib")
263
 
264
  except ImportError:
265
  print("Error: huggingface_hub is required. Install with:")