Vu Anh commited on
Commit
6b2c2e0
·
1 Parent(s): 25aa0d2
Files changed (2) hide show
  1. inference.py +123 -45
  2. use_this_model.py +123 -61
inference.py CHANGED
@@ -1,7 +1,8 @@
1
  #!/usr/bin/env python3
2
  """
3
- Inference script for Pulse Core 1 - Vietnamese Aspect Sentiment Analysis.
4
  Loads trained sentiment models from local files and performs predictions.
 
5
  """
6
 
7
  import argparse
@@ -20,22 +21,34 @@ def find_local_models():
20
  # Find exported sentiment models in project root
21
  for filename in os.listdir('.'):
22
  if filename.endswith('.joblib'):
23
- if filename.startswith('uts2017_sentiment_'):
 
 
24
  models['exported']['uts2017_sentiment'] = filename
25
 
26
  # Find models in runs directory - prioritize SVC models
27
- sentiment_runs = glob.glob('runs/*/models/UTS2017_Bank_AspectSentiment_*.joblib')
 
28
 
29
- if sentiment_runs:
30
  # Sort by modification time (most recent first)
31
- sentiment_runs.sort(key=lambda x: os.path.getmtime(x), reverse=True)
 
 
 
 
 
 
32
 
 
 
 
33
  # Prefer SVC models over other types
34
- svc_models = [m for m in sentiment_runs if 'SVC' in m]
35
  if svc_models:
36
  models['runs']['uts2017_sentiment'] = svc_models[0] # Most recent SVC
37
  else:
38
- models['runs']['uts2017_sentiment'] = sentiment_runs[0] # Most recent any model
39
 
40
  return models
41
 
@@ -78,9 +91,14 @@ def predict_text(model, text):
78
  def interactive_mode(model, dataset_name):
79
  """Interactive prediction mode"""
80
  print(f"\n{'='*60}")
81
- print("INTERACTIVE MODE - VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
82
- print(f"{'='*60}")
83
- print("Enter Vietnamese banking text to analyze aspect and sentiment (type 'quit' to exit):")
 
 
 
 
 
84
 
85
  while True:
86
  try:
@@ -109,23 +127,39 @@ def interactive_mode(model, dataset_name):
109
 
110
 
111
  def test_examples(model, dataset_name):
112
- """Test model with predefined banking examples"""
113
- examples = [
114
- "Tôi muốn mở tài khoản tiết kiệm mới",
115
- "Lãi suất vay mua nhà hiện tại quá cao",
116
- "Làm thế nào để đăng ký internet banking?",
117
- "Chi phí chuyển tiền ra nước ngoài rất đắt",
118
- "Ngân hàng ACB uy tín không?",
119
- "Tôi cần hỗ trợ về dịch vụ ngân hàng",
120
- "Thẻ tín dụng bị khóa không lý do",
121
- "Dịch vụ chăm sóc khách hàng rất tệ",
122
- "Khuyến mãi tháng này rất hấp dẫn",
123
- "Bảo mật tài khoản được đảm bảo không?"
124
- ]
125
-
126
- print("\n" + "="*60)
127
- print("TESTING VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
128
- print("="*60)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  for text in examples:
131
  prediction, confidence, top_predictions = predict_text(model, text)
@@ -147,30 +181,41 @@ def list_available_models():
147
  """List all available sentiment models"""
148
  models = find_local_models()
149
 
150
- print("Available Vietnamese Aspect Sentiment Models:")
151
  print("=" * 50)
152
 
153
  if models['exported']:
154
  print("\nExported Models (Project Root):")
155
  for model_type, filename in models['exported'].items():
156
  file_size = os.path.getsize(filename) / (1024 * 1024) # MB
157
- print(f" {model_type}: {filename} ({file_size:.1f}MB)")
 
158
 
159
  if models['runs']:
160
  print("\nRuns Models (Training Directory):")
161
  for model_type, filepath in models['runs'].items():
162
  file_size = os.path.getsize(filepath) / (1024 * 1024) # MB
163
- print(f" {model_type}: {filepath} ({file_size:.1f}MB)")
 
164
 
165
  if not models['exported'] and not models['runs']:
166
  print("No local sentiment models found!")
167
- print("Train a model first using: python train.py --export-model")
 
 
168
 
169
 
170
  def main():
171
  """Main function"""
172
  parser = argparse.ArgumentParser(
173
- description="Inference with local Pulse Core 1 Vietnamese aspect sentiment models"
 
 
 
 
 
 
 
174
  )
175
  parser.add_argument(
176
  "--model-path",
@@ -180,12 +225,12 @@ def main():
180
  parser.add_argument(
181
  "--text",
182
  type=str,
183
- help="Vietnamese banking text to analyze (if not provided, enters interactive mode)"
184
  )
185
  parser.add_argument(
186
  "--test-examples",
187
  action="store_true",
188
- help="Test with predefined banking examples"
189
  )
190
  parser.add_argument(
191
  "--list-models",
@@ -210,22 +255,54 @@ def main():
210
  # Find available models
211
  models = find_local_models()
212
 
213
- # Determine model path
214
  model_path = None
215
- dataset_name = "uts2017_sentiment"
216
 
217
  if args.model_path:
218
  # Use specified model path
219
  model_path = args.model_path
220
- else:
221
- # Auto-select sentiment model
222
- if models[args.source] and 'uts2017_sentiment' in models[args.source]:
223
- model_path = models[args.source]['uts2017_sentiment']
224
- print("Auto-selected UTS2017 sentiment model")
225
  else:
226
- print("No sentiment models found!")
227
- list_available_models()
228
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
  if not model_path or not os.path.exists(model_path):
231
  print(f"Model file not found: {model_path}")
@@ -255,7 +332,8 @@ def main():
255
 
256
  else:
257
  # Interactive mode
258
- print(f"Loaded sentiment model: {os.path.basename(model_path)}")
 
259
  test_examples(model, dataset_name)
260
 
261
  # Ask if user wants interactive mode
 
1
  #!/usr/bin/env python3
2
  """
3
+ Inference script for Pulse Core 1 - Vietnamese Sentiment Analysis System.
4
  Loads trained sentiment models from local files and performs predictions.
5
+ Supports both VLSP2016 general sentiment and UTS2017_Bank aspect sentiment models.
6
  """
7
 
8
  import argparse
 
21
  # Find exported sentiment models in project root
22
  for filename in os.listdir('.'):
23
  if filename.endswith('.joblib'):
24
+ if filename.startswith('vlsp2016_sentiment_'):
25
+ models['exported']['vlsp2016_sentiment'] = filename
26
+ elif filename.startswith('uts2017_sentiment_'):
27
  models['exported']['uts2017_sentiment'] = filename
28
 
29
  # Find models in runs directory - prioritize SVC models
30
+ vlsp_runs = glob.glob('runs/*/models/VLSP2016_Sentiment_*.joblib')
31
+ uts_runs = glob.glob('runs/*/models/UTS2017_Bank_AspectSentiment_*.joblib')
32
 
33
+ if vlsp_runs:
34
  # Sort by modification time (most recent first)
35
+ vlsp_runs.sort(key=lambda x: os.path.getmtime(x), reverse=True)
36
+ # Prefer SVC models over other types
37
+ svc_models = [m for m in vlsp_runs if 'SVC' in m]
38
+ if svc_models:
39
+ models['runs']['vlsp2016_sentiment'] = svc_models[0] # Most recent SVC
40
+ else:
41
+ models['runs']['vlsp2016_sentiment'] = vlsp_runs[0] # Most recent any model
42
 
43
+ if uts_runs:
44
+ # Sort by modification time (most recent first)
45
+ uts_runs.sort(key=lambda x: os.path.getmtime(x), reverse=True)
46
  # Prefer SVC models over other types
47
+ svc_models = [m for m in uts_runs if 'SVC' in m]
48
  if svc_models:
49
  models['runs']['uts2017_sentiment'] = svc_models[0] # Most recent SVC
50
  else:
51
+ models['runs']['uts2017_sentiment'] = uts_runs[0] # Most recent any model
52
 
53
  return models
54
 
 
91
  def interactive_mode(model, dataset_name):
92
  """Interactive prediction mode"""
93
  print(f"\n{'='*60}")
94
+ if dataset_name == 'vlsp2016_sentiment':
95
+ print("INTERACTIVE MODE - VIETNAMESE GENERAL SENTIMENT ANALYSIS")
96
+ print(f"{'='*60}")
97
+ print("Enter Vietnamese text to analyze sentiment (type 'quit' to exit):")
98
+ else:
99
+ print("INTERACTIVE MODE - VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
100
+ print(f"{'='*60}")
101
+ print("Enter Vietnamese banking text to analyze aspect and sentiment (type 'quit' to exit):")
102
 
103
  while True:
104
  try:
 
127
 
128
 
129
  def test_examples(model, dataset_name):
130
+ """Test model with predefined examples based on dataset type"""
131
+ if dataset_name == 'vlsp2016_sentiment':
132
+ examples = [
133
+ "Sản phẩm này rất tốt, tôi rất hài lòng",
134
+ "Chất lượng dịch vụ tệ quá",
135
+ "Giá cả hợp lý, thể chấp nhận được",
136
+ "Nhân viên phục vụ rất nhiệt tình",
137
+ "Đồ ăn không ngon, sẽ không quay lại",
138
+ "Giao hàng nhanh chóng, đóng gói cẩn thận",
139
+ "Sản phẩm bình thường, không đặc biệt",
140
+ "Rất đáng tiền, chất lượng tuyệt vời",
141
+ "Không như mong đợi, khá thất vọng",
142
+ "Dịch vụ khách hàng tốt, giải quyết nhanh chóng"
143
+ ]
144
+ print("\n" + "="*60)
145
+ print("TESTING VIETNAMESE GENERAL SENTIMENT ANALYSIS")
146
+ print("="*60)
147
+ else:
148
+ examples = [
149
+ "Tôi muốn mở tài khoản tiết kiệm mới",
150
+ "Lãi suất vay mua nhà hiện tại quá cao",
151
+ "Làm thế nào để đăng ký internet banking?",
152
+ "Chi phí chuyển tiền ra nước ngoài rất đắt",
153
+ "Ngân hàng ACB có uy tín không?",
154
+ "Tôi cần hỗ trợ về dịch vụ ngân hàng",
155
+ "Thẻ tín dụng bị khóa không rõ lý do",
156
+ "Dịch vụ chăm sóc khách hàng rất tệ",
157
+ "Khuyến mãi tháng này rất hấp dẫn",
158
+ "Bảo mật tài khoản có được đảm bảo không?"
159
+ ]
160
+ print("\n" + "="*60)
161
+ print("TESTING VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
162
+ print("="*60)
163
 
164
  for text in examples:
165
  prediction, confidence, top_predictions = predict_text(model, text)
 
181
  """List all available sentiment models"""
182
  models = find_local_models()
183
 
184
+ print("Available Vietnamese Sentiment Models:")
185
  print("=" * 50)
186
 
187
  if models['exported']:
188
  print("\nExported Models (Project Root):")
189
  for model_type, filename in models['exported'].items():
190
  file_size = os.path.getsize(filename) / (1024 * 1024) # MB
191
+ dataset_type = "General Sentiment" if "vlsp2016" in model_type else "Banking Aspect Sentiment"
192
+ print(f" {model_type}: {filename} ({file_size:.1f}MB) - {dataset_type}")
193
 
194
  if models['runs']:
195
  print("\nRuns Models (Training Directory):")
196
  for model_type, filepath in models['runs'].items():
197
  file_size = os.path.getsize(filepath) / (1024 * 1024) # MB
198
+ dataset_type = "General Sentiment" if "vlsp2016" in model_type else "Banking Aspect Sentiment"
199
+ print(f" {model_type}: {filepath} ({file_size:.1f}MB) - {dataset_type}")
200
 
201
  if not models['exported'] and not models['runs']:
202
  print("No local sentiment models found!")
203
+ print("Train a model first using:")
204
+ print(" VLSP2016: python train.py --dataset vlsp2016 --export-model")
205
+ print(" UTS2017: python train.py --dataset uts2017 --export-model")
206
 
207
 
208
  def main():
209
  """Main function"""
210
  parser = argparse.ArgumentParser(
211
+ description="Inference with local Pulse Core 1 Vietnamese sentiment models"
212
+ )
213
+ parser.add_argument(
214
+ "--dataset",
215
+ type=str,
216
+ choices=["vlsp2016", "uts2017", "auto"],
217
+ default="auto",
218
+ help="Dataset type to use (default: auto-detect)"
219
  )
220
  parser.add_argument(
221
  "--model-path",
 
225
  parser.add_argument(
226
  "--text",
227
  type=str,
228
+ help="Vietnamese text to analyze (if not provided, enters interactive mode)"
229
  )
230
  parser.add_argument(
231
  "--test-examples",
232
  action="store_true",
233
+ help="Test with predefined examples"
234
  )
235
  parser.add_argument(
236
  "--list-models",
 
255
  # Find available models
256
  models = find_local_models()
257
 
258
+ # Determine model path and dataset
259
  model_path = None
260
+ dataset_name = None
261
 
262
  if args.model_path:
263
  # Use specified model path
264
  model_path = args.model_path
265
+ # Try to detect dataset from filename
266
+ if 'vlsp2016' in args.model_path:
267
+ dataset_name = 'vlsp2016_sentiment'
268
+ elif 'uts2017' in args.model_path:
269
+ dataset_name = 'uts2017_sentiment'
270
  else:
271
+ dataset_name = 'unknown'
272
+ else:
273
+ # Auto-select or use specified dataset
274
+ if args.dataset == 'vlsp2016':
275
+ if models[args.source] and 'vlsp2016_sentiment' in models[args.source]:
276
+ model_path = models[args.source]['vlsp2016_sentiment']
277
+ dataset_name = 'vlsp2016_sentiment'
278
+ print("Selected VLSP2016 general sentiment model")
279
+ else:
280
+ print("No VLSP2016 models found!")
281
+ list_available_models()
282
+ return
283
+ elif args.dataset == 'uts2017':
284
+ if models[args.source] and 'uts2017_sentiment' in models[args.source]:
285
+ model_path = models[args.source]['uts2017_sentiment']
286
+ dataset_name = 'uts2017_sentiment'
287
+ print("Selected UTS2017 banking aspect sentiment model")
288
+ else:
289
+ print("No UTS2017 models found!")
290
+ list_available_models()
291
+ return
292
+ else: # auto
293
+ # Prefer VLSP2016 if available, otherwise UTS2017
294
+ if models[args.source] and 'vlsp2016_sentiment' in models[args.source]:
295
+ model_path = models[args.source]['vlsp2016_sentiment']
296
+ dataset_name = 'vlsp2016_sentiment'
297
+ print("Auto-selected VLSP2016 general sentiment model")
298
+ elif models[args.source] and 'uts2017_sentiment' in models[args.source]:
299
+ model_path = models[args.source]['uts2017_sentiment']
300
+ dataset_name = 'uts2017_sentiment'
301
+ print("Auto-selected UTS2017 banking aspect sentiment model")
302
+ else:
303
+ print("No sentiment models found!")
304
+ list_available_models()
305
+ return
306
 
307
  if not model_path or not os.path.exists(model_path):
308
  print(f"Model file not found: {model_path}")
 
332
 
333
  else:
334
  # Interactive mode
335
+ model_type = "General Sentiment" if dataset_name == 'vlsp2016_sentiment' else "Banking Aspect Sentiment"
336
+ print(f"Loaded {model_type} model: {os.path.basename(model_path)}")
337
  test_examples(model, dataset_name)
338
 
339
  # Ask if user wants interactive mode
use_this_model.py CHANGED
@@ -1,7 +1,7 @@
1
  #!/usr/bin/env python3
2
  """
3
- Demonstration script for using Pulse Core 1 - Vietnamese Banking Aspect Sentiment Analysis from Hugging Face Hub.
4
- Shows how to download and use the pre-trained aspect sentiment model.
5
  """
6
 
7
  from huggingface_hub import hf_hub_download
@@ -31,10 +31,19 @@ def predict_text(model, text):
31
  return None, 0, []
32
 
33
 
34
- def load_model_from_hub():
35
- """Load the pre-trained Pulse Core 1 banking aspect sentiment model from Hugging Face Hub"""
36
- filename = "uts2017_sentiment_20250928_131716.joblib"
37
- print("Downloading Pulse Core 1 (Vietnamese Banking Aspect Sentiment) model from Hugging Face Hub...")
 
 
 
 
 
 
 
 
 
38
 
39
  try:
40
  model_path = hf_hub_download("undertheseanlp/pulse_core_1", filename)
@@ -42,9 +51,9 @@ def load_model_from_hub():
42
 
43
  print("Loading model...")
44
  model = joblib.load(model_path)
45
- print(f"Model loaded successfully. Classes: {len(model.classes_)} aspect-sentiment combinations")
46
  print(f"Model type: {type(model.named_steps['clf']).__name__}")
47
- return model
48
  except Exception as e:
49
  print(f"Error downloading model: {e}")
50
  print("This might mean the model file hasn't been uploaded to Hugging Face Hub yet.")
@@ -52,41 +61,61 @@ def load_model_from_hub():
52
  raise
53
 
54
 
55
- def predict_banking_examples(model):
56
- """Demonstrate predictions on Vietnamese banking aspect sentiment examples"""
57
- print("\n" + "="*60)
58
- print("VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS EXAMPLES")
59
- print("="*60)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- # Vietnamese banking examples with expected aspect-sentiment combinations
62
- examples = [
63
- ("CUSTOMER_SUPPORT#negative", "Dịch vụ chăm sóc khách hàng rất tệ"),
64
- ("CUSTOMER_SUPPORT#positive", "Nhân viên hỗ trợ rất nhiệt tình"),
65
- ("TRADEMARK#positive", "Ngân hàng ACB có uy tín tốt"),
66
- ("TRADEMARK#negative", "Thương hiệu ngân hàng này không đáng tin cậy"),
67
- ("LOAN#positive", "Lãi suất vay mua nhà rất ưu đãi"),
68
- ("LOAN#negative", "Lãi suất vay quá cao, không chấp nhận được"),
69
- ("INTEREST_RATE#negative", "Lãi suất tiết kiệm thấp quá"),
70
- ("INTEREST_RATE#positive", "Lãi suất gửi tiết kiệm khá hấp dẫn"),
71
- ("CARD#negative", "Thẻ tín dụng bị khóa không rõ lý do"),
72
- ("CARD#positive", "Thẻ ATM rất tiện lợi khi sử dụng"),
73
- ("INTERNET_BANKING#negative", "Internet banking hay bị lỗi"),
74
- ("INTERNET_BANKING#positive", "Ứng dụng ngân hàng điện tử dễ sử dụng"),
75
- ("MONEY_TRANSFER#negative", "Phí chuyển tiền quá đắt"),
76
- ("PROMOTION#positive", "Chương trình khuyến mãi rất hấp dẫn"),
77
- ("SECURITY#positive", "Bảo mật tài khoản rất tốt")
78
- ]
79
-
80
- print("Testing Vietnamese banking aspect sentiment analysis:")
81
  print("-" * 60)
82
 
83
- for expected_aspect_sentiment, text in examples:
84
  try:
85
  prediction, confidence, top_predictions = predict_text(model, text)
86
 
87
  if prediction:
88
  print(f"Text: {text}")
89
- print(f"Expected: {expected_aspect_sentiment}")
90
  print(f"Predicted: {prediction}")
91
  print(f"Confidence: {confidence:.3f}")
92
 
@@ -102,12 +131,17 @@ def predict_banking_examples(model):
102
  print("-" * 60)
103
 
104
 
105
- def interactive_mode(model):
106
- """Interactive mode for testing custom banking text"""
107
  print("\n" + "="*60)
108
- print("INTERACTIVE MODE - VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
109
- print("="*60)
110
- print("Enter Vietnamese banking text to analyze aspect and sentiment (type 'quit' to exit):")
 
 
 
 
 
111
 
112
  while True:
113
  try:
@@ -122,7 +156,10 @@ def interactive_mode(model):
122
  prediction, confidence, top_predictions = predict_text(model, user_input)
123
 
124
  if prediction:
125
- print(f"Predicted aspect-sentiment: {prediction}")
 
 
 
126
  print(f"Confidence: {confidence:.3f}")
127
 
128
  # Show top 3 predictions
@@ -145,25 +182,35 @@ def simple_usage_examples():
145
 
146
  print("Code examples:")
147
  print("""
148
- # Pulse Core 1 Model (Vietnamese Banking Aspect Sentiment Analysis)
149
  from huggingface_hub import hf_hub_download
150
  import joblib
151
 
152
- # Download and load Pulse Core 1 model from HuggingFace Hub
153
- model = joblib.load(
 
 
 
 
 
 
 
 
 
 
154
  hf_hub_download("undertheseanlp/pulse_core_1", "uts2017_sentiment_20250928_131716.joblib")
155
  )
156
 
157
  # Make prediction on banking text
158
  bank_text = "Tôi muốn mở tài khoản tiết kiệm"
159
- prediction = model.predict([bank_text])[0]
160
  print(f"Aspect-Sentiment: {prediction}")
161
 
162
  # For detailed predictions with confidence scores
163
- probabilities = model.predict_proba([bank_text])[0]
164
  top_indices = probabilities.argsort()[-3:][::-1]
165
  for idx in top_indices:
166
- category = model.classes_[idx]
167
  prob = probabilities[idx]
168
  print(f"{category}: {prob:.3f}")
169
 
@@ -173,48 +220,63 @@ for idx in top_indices:
173
 
174
  def main():
175
  """Main demonstration function"""
176
- print("Pulse Core 1 - Vietnamese Banking Aspect Sentiment Analysis")
177
  print("=" * 60)
178
 
179
  try:
180
  # Show simple usage examples
181
  simple_usage_examples()
182
 
183
- # Test banking aspect sentiment model
184
  print("\n" + "="*60)
185
- print("TESTING PULSE CORE 1 MODEL (Vietnamese Banking Aspect Sentiment Analysis)")
186
  print("="*60)
187
 
188
- model = load_model_from_hub()
189
- predict_banking_examples(model)
 
 
 
 
 
 
 
 
 
190
 
191
  # Check if we're in an interactive environment
192
  try:
193
  import sys
194
  if hasattr(sys, 'ps1') or sys.stdin.isatty():
195
- choice = input("\nEnter interactive mode for banking aspect sentiment analysis? (y/n): ").strip().lower()
196
 
197
- if choice in ['y', 'yes']:
198
- interactive_mode(model)
 
 
199
 
200
  except (EOFError, OSError):
201
  print("\nInteractive mode not available in this environment.")
202
  print("Run this script in a regular terminal to use interactive mode.")
203
 
204
  print("\nDemonstration complete!")
205
- print("\nPulse Core 1 model is available on Hugging Face Hub:")
206
  print("- Repository: undertheseanlp/pulse_core_1")
207
- print("- Model file: uts2017_sentiment_20250928_131716.joblib")
208
- print("- Task: Vietnamese Banking Aspect Sentiment Analysis")
209
- print("- Classes: 35 aspect-sentiment combinations")
 
 
 
 
 
210
  print("- Model type: Support Vector Classification (SVC)")
211
- print("- Test accuracy: 71.72%")
212
 
213
  except ImportError:
214
  print("Error: huggingface_hub is required. Install with:")
215
  print(" pip install huggingface_hub")
216
  except Exception as e:
217
- print(f"Error loading model: {e}")
218
  print("\nMake sure you have internet connection and try again.")
219
 
220
 
 
1
  #!/usr/bin/env python3
2
  """
3
+ Demonstration script for using Pulse Core 1 - Vietnamese Sentiment Analysis System from Hugging Face Hub.
4
+ Shows how to download and use the pre-trained sentiment models for both general sentiment and banking aspect sentiment.
5
  """
6
 
7
  from huggingface_hub import hf_hub_download
 
31
  return None, 0, []
32
 
33
 
34
+ def load_model_from_hub(model_type="vlsp2016"):
35
+ """Load the pre-trained Pulse Core 1 sentiment model from Hugging Face Hub
36
+ Args:
37
+ model_type: 'vlsp2016' for general sentiment or 'uts2017' for banking aspect sentiment
38
+ """
39
+ if model_type == "vlsp2016":
40
+ filename = "vlsp2016_sentiment_20250929_075529.joblib"
41
+ print("Downloading Pulse Core 1 (Vietnamese General Sentiment) model from Hugging Face Hub...")
42
+ classes_desc = "sentiment classes (positive, negative, neutral)"
43
+ else:
44
+ filename = "uts2017_sentiment_20250928_131716.joblib"
45
+ print("Downloading Pulse Core 1 (Vietnamese Banking Aspect Sentiment) model from Hugging Face Hub...")
46
+ classes_desc = "aspect-sentiment combinations"
47
 
48
  try:
49
  model_path = hf_hub_download("undertheseanlp/pulse_core_1", filename)
 
51
 
52
  print("Loading model...")
53
  model = joblib.load(model_path)
54
+ print(f"Model loaded successfully. Classes: {len(model.classes_)} {classes_desc}")
55
  print(f"Model type: {type(model.named_steps['clf']).__name__}")
56
+ return model, model_type
57
  except Exception as e:
58
  print(f"Error downloading model: {e}")
59
  print("This might mean the model file hasn't been uploaded to Hugging Face Hub yet.")
 
61
  raise
62
 
63
 
64
+ def predict_sentiment_examples(model, model_type):
65
+ """Demonstrate predictions on Vietnamese sentiment examples"""
66
+ if model_type == "vlsp2016":
67
+ print("\n" + "="*60)
68
+ print("VIETNAMESE GENERAL SENTIMENT ANALYSIS EXAMPLES")
69
+ print("="*60)
70
+
71
+ # Vietnamese general sentiment examples
72
+ examples = [
73
+ ("positive", "Sản phẩm này rất tốt, tôi rất hài lòng"),
74
+ ("negative", "Chất lượng dịch vụ tệ quá"),
75
+ ("neutral", "Giá cả hợp lý, có thể chấp nhận được"),
76
+ ("positive", "Nhân viên phục vụ rất nhiệt tình"),
77
+ ("negative", "Đồ ăn không ngon, sẽ không quay lại"),
78
+ ("positive", "Giao hàng nhanh chóng, đóng gói cẩn thận"),
79
+ ("neutral", "Sản phẩm bình thường, không có gì đặc biệt"),
80
+ ("positive", "Rất đáng tiền, chất lượng tuyệt vời"),
81
+ ("negative", "Không như mong đợi, khá thất vọng"),
82
+ ("positive", "Dịch vụ khách hàng tốt, giải quyết nhanh chóng")
83
+ ]
84
+ print("Testing Vietnamese general sentiment analysis:")
85
+ else:
86
+ print("\n" + "="*60)
87
+ print("VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS EXAMPLES")
88
+ print("="*60)
89
+
90
+ # Vietnamese banking examples with expected aspect-sentiment combinations
91
+ examples = [
92
+ ("CUSTOMER_SUPPORT#negative", "Dịch vụ chăm sóc khách hàng rất tệ"),
93
+ ("CUSTOMER_SUPPORT#positive", "Nhân viên hỗ trợ rất nhiệt tình"),
94
+ ("TRADEMARK#positive", "Ngân hàng ACB có uy tín tốt"),
95
+ ("TRADEMARK#negative", "Thương hiệu ngân hàng này không đáng tin cậy"),
96
+ ("LOAN#positive", "Lãi suất vay mua nhà rất ưu đãi"),
97
+ ("LOAN#negative", "Lãi suất vay quá cao, không chấp nhận được"),
98
+ ("INTEREST_RATE#negative", "Lãi suất tiết kiệm thấp quá"),
99
+ ("INTEREST_RATE#positive", "Lãi suất gửi tiết kiệm khá hấp dẫn"),
100
+ ("CARD#negative", "Thẻ tín dụng bị khóa không rõ lý do"),
101
+ ("CARD#positive", "Thẻ ATM rất tiện lợi khi sử dụng"),
102
+ ("INTERNET_BANKING#negative", "Internet banking hay bị lỗi"),
103
+ ("INTERNET_BANKING#positive", "Ứng dụng ngân hàng điện tử dễ sử dụng"),
104
+ ("MONEY_TRANSFER#negative", "Phí chuyển tiền quá đắt"),
105
+ ("PROMOTION#positive", "Chương trình khuyến mãi rất hấp dẫn"),
106
+ ("SECURITY#positive", "Bảo mật tài khoản rất tốt")
107
+ ]
108
+ print("Testing Vietnamese banking aspect sentiment analysis:")
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  print("-" * 60)
111
 
112
+ for expected_label, text in examples:
113
  try:
114
  prediction, confidence, top_predictions = predict_text(model, text)
115
 
116
  if prediction:
117
  print(f"Text: {text}")
118
+ print(f"Expected: {expected_label}")
119
  print(f"Predicted: {prediction}")
120
  print(f"Confidence: {confidence:.3f}")
121
 
 
131
  print("-" * 60)
132
 
133
 
134
+ def interactive_mode(model, model_type):
135
+ """Interactive mode for testing custom text"""
136
  print("\n" + "="*60)
137
+ if model_type == "vlsp2016":
138
+ print("INTERACTIVE MODE - VIETNAMESE GENERAL SENTIMENT ANALYSIS")
139
+ print("="*60)
140
+ print("Enter Vietnamese text to analyze sentiment (type 'quit' to exit):")
141
+ else:
142
+ print("INTERACTIVE MODE - VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
143
+ print("="*60)
144
+ print("Enter Vietnamese banking text to analyze aspect and sentiment (type 'quit' to exit):")
145
 
146
  while True:
147
  try:
 
156
  prediction, confidence, top_predictions = predict_text(model, user_input)
157
 
158
  if prediction:
159
+ if model_type == "vlsp2016":
160
+ print(f"Predicted sentiment: {prediction}")
161
+ else:
162
+ print(f"Predicted aspect-sentiment: {prediction}")
163
  print(f"Confidence: {confidence:.3f}")
164
 
165
  # Show top 3 predictions
 
182
 
183
  print("Code examples:")
184
  print("""
185
+ # Pulse Core 1 Models (Vietnamese Sentiment Analysis)
186
  from huggingface_hub import hf_hub_download
187
  import joblib
188
 
189
+ # Option 1: General Sentiment Analysis (VLSP2016)
190
+ general_model = joblib.load(
191
+ hf_hub_download("undertheseanlp/pulse_core_1", "vlsp2016_sentiment_20250929_075529.joblib")
192
+ )
193
+
194
+ # Make prediction on general text
195
+ general_text = "Sản phẩm này rất tốt"
196
+ prediction = general_model.predict([general_text])[0]
197
+ print(f"Sentiment: {prediction}")
198
+
199
+ # Option 2: Banking Aspect Sentiment Analysis (UTS2017_Bank)
200
+ banking_model = joblib.load(
201
  hf_hub_download("undertheseanlp/pulse_core_1", "uts2017_sentiment_20250928_131716.joblib")
202
  )
203
 
204
  # Make prediction on banking text
205
  bank_text = "Tôi muốn mở tài khoản tiết kiệm"
206
+ prediction = banking_model.predict([bank_text])[0]
207
  print(f"Aspect-Sentiment: {prediction}")
208
 
209
  # For detailed predictions with confidence scores
210
+ probabilities = banking_model.predict_proba([bank_text])[0]
211
  top_indices = probabilities.argsort()[-3:][::-1]
212
  for idx in top_indices:
213
+ category = banking_model.classes_[idx]
214
  prob = probabilities[idx]
215
  print(f"{category}: {prob:.3f}")
216
 
 
220
 
221
  def main():
222
  """Main demonstration function"""
223
+ print("Pulse Core 1 - Vietnamese Sentiment Analysis System")
224
  print("=" * 60)
225
 
226
  try:
227
  # Show simple usage examples
228
  simple_usage_examples()
229
 
230
+ # Test both models
231
  print("\n" + "="*60)
232
+ print("TESTING PULSE CORE 1 MODELS")
233
  print("="*60)
234
 
235
+ # Test VLSP2016 general sentiment model
236
+ print("\n1. Testing VLSP2016 General Sentiment Model")
237
+ print("-" * 40)
238
+ vlsp_model, vlsp_type = load_model_from_hub("vlsp2016")
239
+ predict_sentiment_examples(vlsp_model, vlsp_type)
240
+
241
+ # Test UTS2017 banking aspect sentiment model
242
+ print("\n2. Testing UTS2017 Banking Aspect Sentiment Model")
243
+ print("-" * 40)
244
+ uts_model, uts_type = load_model_from_hub("uts2017")
245
+ predict_sentiment_examples(uts_model, uts_type)
246
 
247
  # Check if we're in an interactive environment
248
  try:
249
  import sys
250
  if hasattr(sys, 'ps1') or sys.stdin.isatty():
251
+ choice = input("\nEnter interactive mode? Choose model type (vlsp2016/uts2017/n): ").strip().lower()
252
 
253
+ if choice == 'vlsp2016':
254
+ interactive_mode(vlsp_model, "vlsp2016")
255
+ elif choice == 'uts2017':
256
+ interactive_mode(uts_model, "uts2017")
257
 
258
  except (EOFError, OSError):
259
  print("\nInteractive mode not available in this environment.")
260
  print("Run this script in a regular terminal to use interactive mode.")
261
 
262
  print("\nDemonstration complete!")
263
+ print("\nPulse Core 1 models are available on Hugging Face Hub:")
264
  print("- Repository: undertheseanlp/pulse_core_1")
265
+ print("- VLSP2016 Model: vlsp2016_sentiment_20250929_075529.joblib")
266
+ print(" * Task: Vietnamese General Sentiment Analysis")
267
+ print(" * Classes: 3 sentiment polarities")
268
+ print(" * Test accuracy: 71.14%")
269
+ print("- UTS2017 Model: uts2017_sentiment_20250928_131716.joblib")
270
+ print(" * Task: Vietnamese Banking Aspect Sentiment Analysis")
271
+ print(" * Classes: 35 aspect-sentiment combinations")
272
+ print(" * Test accuracy: 71.72%")
273
  print("- Model type: Support Vector Classification (SVC)")
 
274
 
275
  except ImportError:
276
  print("Error: huggingface_hub is required. Install with:")
277
  print(" pip install huggingface_hub")
278
  except Exception as e:
279
+ print(f"Error loading models: {e}")
280
  print("\nMake sure you have internet connection and try again.")
281
 
282