File size: 8,757 Bytes
4523f56 |
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 |
#!/usr/bin/env python3
"""
Script to push the trained BERT-Tiny AMD model to Hugging Face Hub
"""
import os
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from huggingface_hub import HfApi, Repository
import json
from pathlib import Path
# Configuration
REPO_ID = "Adya662/bert-tiny-amd"
MODEL_PATH = "best_enhanced_progressive_amd.pth"
BASE_MODEL = "prajjwal1/bert-tiny"
def create_model_config():
"""Create model configuration"""
config = {
"model_type": "bert",
"architectures": ["BertForSequenceClassification"],
"attention_proxy_dtype": "float32",
"attention_dropout": 0.1,
"classifier_dropout": None,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 128,
"initializer_range": 0.02,
"intermediate_size": 512,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "bert",
"num_attention_heads": 2,
"num_hidden_layers": 2,
"num_labels": 1,
"pad_token_id": 0,
"position_embedding_type": "absolute",
"problem_type": "single_label_classification",
"torch_dtype": "float32",
"transformers_version": "4.21.0",
"type_vocab_size": 2,
"use_cache": True,
"vocab_size": 30522
}
return config
def create_training_metadata():
"""Create training metadata"""
metadata = {
"model_name": "bert-tiny-amd",
"base_model": "prajjwal1/bert-tiny",
"task": "text-classification",
"dataset": "ElevateNow call center transcripts",
"language": "en",
"license": "mit",
"pipeline_tag": "text-classification",
"tags": [
"text-classification",
"answering-machine-detection",
"bert-tiny",
"binary-classification",
"call-center",
"voice-processing"
],
"performance": {
"validation_accuracy": 0.9394,
"precision": 0.9275,
"recall": 0.8727,
"f1_score": 0.8993
},
"training_details": {
"total_samples": 3548,
"training_samples": 2838,
"validation_samples": 710,
"epochs": 15,
"batch_size": 32,
"learning_rate": 3e-5,
"device": "mps"
}
}
return metadata
def push_model_to_hub():
"""Push the trained model to Hugging Face Hub"""
print("π Starting model upload to Hugging Face Hub...")
# Initialize HF API
api = HfApi()
# Create model configuration
config = create_model_config()
# Save config
with open("config.json", "w") as f:
json.dump(config, f, indent=2)
# Create training metadata
metadata = create_training_metadata()
# Save training metadata
with open("training_metadata.json", "w") as f:
json.dump(metadata, f, indent=2)
# Load tokenizer from base model
print("π₯ Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
tokenizer.save_pretrained(".")
# Load base model and update with trained weights
print("π₯ Loading base model...")
model = AutoModelForSequenceClassification.from_pretrained(
BASE_MODEL,
num_labels=1,
config=config
)
# Load trained weights
print("π₯ Loading trained weights...")
if os.path.exists(MODEL_PATH):
state_dict = torch.load(MODEL_PATH, map_location='cpu')
model.load_state_dict(state_dict)
print("β
Trained weights loaded successfully")
else:
print(f"β Model file {MODEL_PATH} not found!")
return False
# Save model
print("πΎ Saving model...")
model.save_pretrained(".", safe_serialization=True)
# Create README.md
readme_content = """---
license: mit
tags:
- text-classification
- answering-machine-detection
- bert-tiny
- binary-classification
- call-center
- voice-processing
pipeline_tag: text-classification
---
# BERT-Tiny AMD Classifier
A lightweight BERT-Tiny model fine-tuned for Answering Machine Detection (AMD) in call center environments.
## Model Description
This model is based on `prajjwal1/bert-tiny` and fine-tuned to classify phone call transcripts as either human or machine (answering machine/voicemail) responses. It's designed for real-time call center applications where quick and accurate detection of answering machines is crucial.
## Model Architecture
- **Base Model**: `prajjwal1/bert-tiny` (2 layers, 128 hidden size, 2 attention heads)
- **Total Parameters**: ~4.4M (lightweight and efficient)
- **Input**: User transcript text (max 128 tokens)
- **Output**: Single logit with sigmoid activation for binary classification
- **Loss Function**: BCEWithLogitsLoss with positive weight for class imbalance
## Performance
- **Validation Accuracy**: 93.94%
- **Precision**: 92.75%
- **Recall**: 87.27%
- **F1-Score**: 89.93%
- **Training Device**: MPS (Apple Silicon GPU)
- **Best Epoch**: 15 (with early stopping)
## Training Data
- **Total Samples**: 3,548 phone call transcripts
- **Training Set**: 2,838 samples
- **Validation Set**: 710 samples
- **Class Distribution**: 30.8% machine calls, 69.2% human calls
- **Source**: ElevateNow call center data
## Usage
### Basic Inference
```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Load model and tokenizer
model = AutoModelForSequenceClassification.from_pretrained("Adya662/bert-tiny-amd")
tokenizer = AutoTokenizer.from_pretrained("Adya662/bert-tiny-amd")
# Prepare input
text = "Hello, this is John speaking"
inputs = tokenizer(text, return_tensors="pt", max_length=128, truncation=True, padding=True)
# Make prediction
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits.squeeze(-1)
probability = torch.sigmoid(logits).item()
is_machine = probability >= 0.5
print(f"Prediction: {'Machine' if is_machine else 'Human'}")
print(f"Confidence: {probability:.4f}")
```
### Production Usage
```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
class AMDClassifier:
def __init__(self, model_name="Adya662/bert-tiny-amd"):
self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model.to(self.device)
self.model.eval()
def predict(self, transcript_text, threshold=0.5):
# Predict if transcript is from answering machine
inputs = self.tokenizer(
transcript_text,
return_tensors="pt",
max_length=128,
truncation=True,
padding=True
).to(self.device)
with torch.no_grad():
outputs = self.model(**inputs)
logits = outputs.logits.squeeze(-1)
probability = torch.sigmoid(logits).item()
is_machine = probability >= threshold
return is_machine, probability
# Usage
classifier = AMDClassifier()
is_machine, confidence = classifier.predict("Hello, this is John speaking")
```
## Training Details
- **Optimizer**: AdamW with weight decay (0.01)
- **Learning Rate**: 3e-5 with linear scheduling
- **Batch Size**: 32
- **Epochs**: 15 (with early stopping)
- **Early Stopping**: Patience of 3 epochs
- **Class Imbalance**: Handled with positive weight
## Limitations
- Trained on English phone call transcripts
- May not generalize well to other languages or domains
- Performance may vary with different transcription quality
- Designed for short utterances (max 128 tokens)
## License
MIT License - see LICENSE file for details.
"""
with open("README.md", "w") as f:
f.write(readme_content)
# Upload to Hub
print("β¬οΈ Uploading to Hugging Face Hub...")
try:
api.upload_folder(
folder_path=".",
repo_id=REPO_ID,
repo_type="model",
commit_message="Upload trained BERT-Tiny AMD model with enhanced progressive features"
)
print("β
Model uploaded successfully!")
print(f"π Model available at: https://huggingface.co/{REPO_ID}")
return True
except Exception as e:
print(f"β Upload failed: {e}")
return False
if __name__ == "__main__":
success = push_model_to_hub()
if success:
print("\nπ Model deployment completed successfully!")
else:
print("\nπ₯ Model deployment failed!") |