Vu Anh
Claude
commited on
Commit
·
43b1fa0
1
Parent(s):
91cc050
Update use_this_model.py for Pulse Core 1 banking aspect sentiment analysis
Browse files- Updated script to focus on Vietnamese banking aspect sentiment analysis
- Changed repository from undertheseanlp/sonar_core_1 to undertheseanlp/pulse_core_1
- Updated model filename to uts2017_sentiment_20250928_122636.joblib
- Removed VNTC news classification examples
- Added comprehensive banking aspect sentiment examples with expected outcomes
- Updated interactive mode for aspect-sentiment analysis
- Added proper aspect#sentiment format demonstrations
- Tested successfully with Hugging Face Hub model download
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- use_this_model.py +213 -0
use_this_model.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 8 |
+
import joblib
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def predict_text(model, text):
|
| 12 |
+
"""Make prediction on a single text (consistent with inference.py)"""
|
| 13 |
+
try:
|
| 14 |
+
probabilities = model.predict_proba([text])[0]
|
| 15 |
+
|
| 16 |
+
# Get top 3 predictions sorted by probability
|
| 17 |
+
top_indices = probabilities.argsort()[-3:][::-1]
|
| 18 |
+
top_predictions = []
|
| 19 |
+
for idx in top_indices:
|
| 20 |
+
category = model.classes_[idx]
|
| 21 |
+
prob = probabilities[idx]
|
| 22 |
+
top_predictions.append((category, prob))
|
| 23 |
+
|
| 24 |
+
# The prediction should be the top category
|
| 25 |
+
prediction = top_predictions[0][0]
|
| 26 |
+
confidence = top_predictions[0][1]
|
| 27 |
+
|
| 28 |
+
return prediction, confidence, top_predictions
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error making prediction: {e}")
|
| 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_122636.joblib"
|
| 37 |
+
print("Downloading Pulse Core 1 (Vietnamese Banking Aspect Sentiment) model from Hugging Face Hub...")
|
| 38 |
+
|
| 39 |
+
model_path = hf_hub_download("undertheseanlp/pulse_core_1", filename)
|
| 40 |
+
print(f"Model downloaded to: {model_path}")
|
| 41 |
+
|
| 42 |
+
print("Loading model...")
|
| 43 |
+
model = joblib.load(model_path)
|
| 44 |
+
print(f"Model loaded successfully. Classes: {len(model.classes_)} aspect-sentiment combinations")
|
| 45 |
+
return model
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def predict_banking_examples(model):
|
| 49 |
+
"""Demonstrate predictions on Vietnamese banking aspect sentiment examples"""
|
| 50 |
+
print("\n" + "="*60)
|
| 51 |
+
print("VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS EXAMPLES")
|
| 52 |
+
print("="*60)
|
| 53 |
+
|
| 54 |
+
# Vietnamese banking examples with expected aspect-sentiment combinations
|
| 55 |
+
examples = [
|
| 56 |
+
("CUSTOMER_SUPPORT#negative", "Dịch vụ chăm sóc khách hàng rất tệ"),
|
| 57 |
+
("CUSTOMER_SUPPORT#positive", "Nhân viên hỗ trợ rất nhiệt tình"),
|
| 58 |
+
("TRADEMARK#positive", "Ngân hàng ACB có uy tín tốt"),
|
| 59 |
+
("TRADEMARK#negative", "Thương hiệu ngân hàng này không đáng tin cậy"),
|
| 60 |
+
("LOAN#positive", "Lãi suất vay mua nhà rất ưu đãi"),
|
| 61 |
+
("LOAN#negative", "Lãi suất vay quá cao, không chấp nhận được"),
|
| 62 |
+
("INTEREST_RATE#negative", "Lãi suất tiết kiệm thấp quá"),
|
| 63 |
+
("INTEREST_RATE#positive", "Lãi suất gửi tiết kiệm khá hấp dẫn"),
|
| 64 |
+
("CARD#negative", "Thẻ tín dụng bị khóa không rõ lý do"),
|
| 65 |
+
("CARD#positive", "Thẻ ATM rất tiện lợi khi sử dụng"),
|
| 66 |
+
("INTERNET_BANKING#negative", "Internet banking hay bị lỗi"),
|
| 67 |
+
("INTERNET_BANKING#positive", "Ứng dụng ngân hàng điện tử dễ sử dụng"),
|
| 68 |
+
("MONEY_TRANSFER#negative", "Phí chuyển tiền quá đắt"),
|
| 69 |
+
("PROMOTION#positive", "Chương trình khuyến mãi rất hấp dẫn"),
|
| 70 |
+
("SECURITY#positive", "Bảo mật tài khoản rất tốt")
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
print("Testing Vietnamese banking aspect sentiment analysis:")
|
| 74 |
+
print("-" * 60)
|
| 75 |
+
|
| 76 |
+
for expected_aspect_sentiment, text in examples:
|
| 77 |
+
try:
|
| 78 |
+
prediction, confidence, top_predictions = predict_text(model, text)
|
| 79 |
+
|
| 80 |
+
if prediction:
|
| 81 |
+
print(f"Text: {text}")
|
| 82 |
+
print(f"Expected: {expected_aspect_sentiment}")
|
| 83 |
+
print(f"Predicted: {prediction}")
|
| 84 |
+
print(f"Confidence: {confidence:.3f}")
|
| 85 |
+
|
| 86 |
+
# Show top 3 predictions
|
| 87 |
+
print("Top 3 predictions:")
|
| 88 |
+
for i, (category, prob) in enumerate(top_predictions, 1):
|
| 89 |
+
print(f" {i}. {category}: {prob:.3f}")
|
| 90 |
+
|
| 91 |
+
print("-" * 60)
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f"Error predicting '{text}': {e}")
|
| 95 |
+
print("-" * 60)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def interactive_mode(model):
|
| 99 |
+
"""Interactive mode for testing custom banking text"""
|
| 100 |
+
print("\n" + "="*60)
|
| 101 |
+
print("INTERACTIVE MODE - VIETNAMESE BANKING ASPECT SENTIMENT ANALYSIS")
|
| 102 |
+
print("="*60)
|
| 103 |
+
print("Enter Vietnamese banking text to analyze aspect and sentiment (type 'quit' to exit):")
|
| 104 |
+
|
| 105 |
+
while True:
|
| 106 |
+
try:
|
| 107 |
+
user_input = input("\nText: ").strip()
|
| 108 |
+
|
| 109 |
+
if user_input.lower() in ['quit', 'exit', 'q']:
|
| 110 |
+
break
|
| 111 |
+
|
| 112 |
+
if not user_input:
|
| 113 |
+
continue
|
| 114 |
+
|
| 115 |
+
prediction, confidence, top_predictions = predict_text(model, user_input)
|
| 116 |
+
|
| 117 |
+
if prediction:
|
| 118 |
+
print(f"Predicted aspect-sentiment: {prediction}")
|
| 119 |
+
print(f"Confidence: {confidence:.3f}")
|
| 120 |
+
|
| 121 |
+
# Show top 3 predictions
|
| 122 |
+
print("Top 3 predictions:")
|
| 123 |
+
for i, (category, prob) in enumerate(top_predictions, 1):
|
| 124 |
+
print(f" {i}. {category}: {prob:.3f}")
|
| 125 |
+
|
| 126 |
+
except KeyboardInterrupt:
|
| 127 |
+
print("\nExiting...")
|
| 128 |
+
break
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"Error: {e}")
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def simple_usage_examples():
|
| 134 |
+
"""Show simple usage examples for HuggingFace Hub models"""
|
| 135 |
+
print("\n" + "="*60)
|
| 136 |
+
print("HUGGINGFACE HUB USAGE EXAMPLES")
|
| 137 |
+
print("="*60)
|
| 138 |
+
|
| 139 |
+
print("Code examples:")
|
| 140 |
+
print("""
|
| 141 |
+
# Pulse Core 1 Model (Vietnamese Banking Aspect Sentiment Analysis)
|
| 142 |
+
from huggingface_hub import hf_hub_download
|
| 143 |
+
import joblib
|
| 144 |
+
|
| 145 |
+
# Download and load Pulse Core 1 model from HuggingFace Hub
|
| 146 |
+
model = joblib.load(
|
| 147 |
+
hf_hub_download("undertheseanlp/pulse_core_1", "uts2017_sentiment_20250928_122636.joblib")
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Make prediction on banking text
|
| 151 |
+
bank_text = "Tôi muốn mở tài khoản tiết kiệm"
|
| 152 |
+
prediction = model.predict([bank_text])[0]
|
| 153 |
+
print(f"Aspect-Sentiment: {prediction}")
|
| 154 |
+
|
| 155 |
+
# For detailed predictions with confidence scores
|
| 156 |
+
probabilities = model.predict_proba([bank_text])[0]
|
| 157 |
+
top_indices = probabilities.argsort()[-3:][::-1]
|
| 158 |
+
for idx in top_indices:
|
| 159 |
+
category = model.classes_[idx]
|
| 160 |
+
prob = probabilities[idx]
|
| 161 |
+
print(f"{category}: {prob:.3f}")
|
| 162 |
+
|
| 163 |
+
# For local file inference, use inference.py instead
|
| 164 |
+
""")
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def main():
|
| 168 |
+
"""Main demonstration function"""
|
| 169 |
+
print("Pulse Core 1 - Vietnamese Banking Aspect Sentiment Analysis")
|
| 170 |
+
print("=" * 60)
|
| 171 |
+
|
| 172 |
+
try:
|
| 173 |
+
# Show simple usage examples
|
| 174 |
+
simple_usage_examples()
|
| 175 |
+
|
| 176 |
+
# Test banking aspect sentiment model
|
| 177 |
+
print("\n" + "="*60)
|
| 178 |
+
print("TESTING PULSE CORE 1 MODEL (Vietnamese Banking Aspect Sentiment Analysis)")
|
| 179 |
+
print("="*60)
|
| 180 |
+
|
| 181 |
+
model = load_model_from_hub()
|
| 182 |
+
predict_banking_examples(model)
|
| 183 |
+
|
| 184 |
+
# Check if we're in an interactive environment
|
| 185 |
+
try:
|
| 186 |
+
import sys
|
| 187 |
+
if hasattr(sys, 'ps1') or sys.stdin.isatty():
|
| 188 |
+
choice = input("\nEnter interactive mode for banking aspect sentiment analysis? (y/n): ").strip().lower()
|
| 189 |
+
|
| 190 |
+
if choice in ['y', 'yes']:
|
| 191 |
+
interactive_mode(model)
|
| 192 |
+
|
| 193 |
+
except (EOFError, OSError):
|
| 194 |
+
print("\nInteractive mode not available in this environment.")
|
| 195 |
+
print("Run this script in a regular terminal to use interactive mode.")
|
| 196 |
+
|
| 197 |
+
print("\nDemonstration complete!")
|
| 198 |
+
print("\nPulse Core 1 model is available on Hugging Face Hub:")
|
| 199 |
+
print("- Repository: undertheseanlp/pulse_core_1")
|
| 200 |
+
print("- Model file: uts2017_sentiment_20250928_122636.joblib")
|
| 201 |
+
print("- Task: Vietnamese Banking Aspect Sentiment Analysis")
|
| 202 |
+
print("- Classes: 35+ aspect-sentiment combinations")
|
| 203 |
+
|
| 204 |
+
except ImportError:
|
| 205 |
+
print("Error: huggingface_hub is required. Install with:")
|
| 206 |
+
print(" pip install huggingface_hub")
|
| 207 |
+
except Exception as e:
|
| 208 |
+
print(f"Error loading model: {e}")
|
| 209 |
+
print("\nMake sure you have internet connection and try again.")
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
if __name__ == "__main__":
|
| 213 |
+
main()
|