Adya662/bert-tiny-amd
112
1#!/usr/bin/env python32"""3Script to push the trained BERT-Tiny AMD model to Hugging Face Hub4"""5 6import os7import torch8from transformers import AutoTokenizer, AutoModelForSequenceClassification9from huggingface_hub import HfApi, Repository10import json11from pathlib import Path12 13# Configuration14REPO_ID = "Adya662/bert-tiny-amd"15MODEL_PATH = "best_enhanced_progressive_amd.pth"16BASE_MODEL = "prajjwal1/bert-tiny"17 18def create_model_config():19 """Create model configuration"""20 config = {21 "model_type": "bert",22 "architectures": ["BertForSequenceClassification"],23 "attention_proxy_dtype": "float32",24 "attention_dropout": 0.1,25 "classifier_dropout": None,26 "hidden_act": "gelu",27 "hidden_dropout_prob": 0.1,28 "hidden_size": 128,29 "initializer_range": 0.02,30 "intermediate_size": 512,31 "layer_norm_eps": 1e-12,32 "max_position_embeddings": 512,33 "model_type": "bert",34 "num_attention_heads": 2,35 "num_hidden_layers": 2,36 "num_labels": 1,37 "pad_token_id": 0,38 "position_embedding_type": "absolute",39 "problem_type": "single_label_classification",40 "torch_dtype": "float32",41 "transformers_version": "4.21.0",42 "type_vocab_size": 2,43 "use_cache": True,44 "vocab_size": 3052245 }46 return config47 48def create_training_metadata():49 """Create training metadata"""50 metadata = {51 "model_name": "bert-tiny-amd",52 "base_model": "prajjwal1/bert-tiny",53 "task": "text-classification",54 "dataset": "ElevateNow call center transcripts",55 "language": "en",56 "license": "mit",57 "pipeline_tag": "text-classification",58 "tags": [59 "text-classification",60 "answering-machine-detection",61 "bert-tiny",62 "binary-classification",63 "call-center",64 "voice-processing"65 ],66 "performance": {67 "validation_accuracy": 0.9394,68 "precision": 0.9275,69 "recall": 0.8727,70 "f1_score": 0.899371 },72 "training_details": {73 "total_samples": 3548,74 "training_samples": 2838,75 "validation_samples": 710,76 "epochs": 15,77 "batch_size": 32,78 "learning_rate": 3e-5,79 "device": "mps"80 }81 }82 return metadata83 84def push_model_to_hub():85 """Push the trained model to Hugging Face Hub"""86 87 print("๐ Starting model upload to Hugging Face Hub...")88 89 # Initialize HF API90 api = HfApi()91 92 # Create model configuration93 config = create_model_config()94 95 # Save config96 with open("config.json", "w") as f:97 json.dump(config, f, indent=2)98 99 # Create training metadata100 metadata = create_training_metadata()101 102 # Save training metadata103 with open("training_metadata.json", "w") as f:104 json.dump(metadata, f, indent=2)105 106 # Load tokenizer from base model107 print("๐ฅ Loading tokenizer...")108 tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)109 tokenizer.save_pretrained(".")110 111 # Load base model and update with trained weights112 print("๐ฅ Loading base model...")113 model = AutoModelForSequenceClassification.from_pretrained(114 BASE_MODEL, 115 num_labels=1,116 config=config117 )118 119 # Load trained weights120 print("๐ฅ Loading trained weights...")121 if os.path.exists(MODEL_PATH):122 state_dict = torch.load(MODEL_PATH, map_location='cpu')123 model.load_state_dict(state_dict)124 print("โ
Trained weights loaded successfully")125 else:126 print(f"โ Model file {MODEL_PATH} not found!")127 return False128 129 # Save model130 print("๐พ Saving model...")131 model.save_pretrained(".", safe_serialization=True)132 133 # Create README.md134 readme_content = """---135license: mit136tags:137- text-classification138- answering-machine-detection139- bert-tiny140- binary-classification141- call-center142- voice-processing143pipeline_tag: text-classification144---145 146# BERT-Tiny AMD Classifier147 148A lightweight BERT-Tiny model fine-tuned for Answering Machine Detection (AMD) in call center environments.149 150## Model Description151 152This 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.153 154## Model Architecture155 156- **Base Model**: `prajjwal1/bert-tiny` (2 layers, 128 hidden size, 2 attention heads)157- **Total Parameters**: ~4.4M (lightweight and efficient)158- **Input**: User transcript text (max 128 tokens)159- **Output**: Single logit with sigmoid activation for binary classification160- **Loss Function**: BCEWithLogitsLoss with positive weight for class imbalance161 162## Performance163 164- **Validation Accuracy**: 93.94%165- **Precision**: 92.75%166- **Recall**: 87.27%167- **F1-Score**: 89.93%168- **Training Device**: MPS (Apple Silicon GPU)169- **Best Epoch**: 15 (with early stopping)170 171## Training Data172 173- **Total Samples**: 3,548 phone call transcripts174- **Training Set**: 2,838 samples175- **Validation Set**: 710 samples176- **Class Distribution**: 30.8% machine calls, 69.2% human calls177- **Source**: ElevateNow call center data178 179## Usage180 181### Basic Inference182 183```python184from transformers import AutoTokenizer, AutoModelForSequenceClassification185import torch186 187# Load model and tokenizer188model = AutoModelForSequenceClassification.from_pretrained("Adya662/bert-tiny-amd")189tokenizer = AutoTokenizer.from_pretrained("Adya662/bert-tiny-amd")190 191# Prepare input192text = "Hello, this is John speaking"193inputs = tokenizer(text, return_tensors="pt", max_length=128, truncation=True, padding=True)194 195# Make prediction196with torch.no_grad():197 outputs = model(**inputs)198 logits = outputs.logits.squeeze(-1)199 probability = torch.sigmoid(logits).item()200 is_machine = probability >= 0.5201 202print(f"Prediction: {'Machine' if is_machine else 'Human'}")203print(f"Confidence: {probability:.4f}")204```205 206### Production Usage207 208```python209from transformers import AutoTokenizer, AutoModelForSequenceClassification210import torch211 212class AMDClassifier:213 def __init__(self, model_name="Adya662/bert-tiny-amd"):214 self.model = AutoModelForSequenceClassification.from_pretrained(model_name)215 self.tokenizer = AutoTokenizer.from_pretrained(model_name)216 self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')217 self.model.to(self.device)218 self.model.eval()219 220 def predict(self, transcript_text, threshold=0.5):221 # Predict if transcript is from answering machine222 inputs = self.tokenizer(223 transcript_text,224 return_tensors="pt",225 max_length=128,226 truncation=True,227 padding=True228 ).to(self.device)229 230 with torch.no_grad():231 outputs = self.model(**inputs)232 logits = outputs.logits.squeeze(-1)233 probability = torch.sigmoid(logits).item()234 is_machine = probability >= threshold235 236 return is_machine, probability237 238# Usage239classifier = AMDClassifier()240is_machine, confidence = classifier.predict("Hello, this is John speaking")241```242 243## Training Details244 245- **Optimizer**: AdamW with weight decay (0.01)246- **Learning Rate**: 3e-5 with linear scheduling247- **Batch Size**: 32248- **Epochs**: 15 (with early stopping)249- **Early Stopping**: Patience of 3 epochs250- **Class Imbalance**: Handled with positive weight251 252## Limitations253 254- Trained on English phone call transcripts255- May not generalize well to other languages or domains256- Performance may vary with different transcription quality257- Designed for short utterances (max 128 tokens)258 259## License260 261MIT License - see LICENSE file for details.262"""263 264 with open("README.md", "w") as f:265 f.write(readme_content)266 267 # Upload to Hub268 print("โฌ๏ธ Uploading to Hugging Face Hub...")269 try:270 api.upload_folder(271 folder_path=".",272 repo_id=REPO_ID,273 repo_type="model",274 commit_message="Upload trained BERT-Tiny AMD model with enhanced progressive features"275 )276 print("โ
Model uploaded successfully!")277 print(f"๐ Model available at: https://huggingface.co/{REPO_ID}")278 return True279 except Exception as e:280 print(f"โ Upload failed: {e}")281 return False282 283if __name__ == "__main__":284 success = push_model_to_hub()285 if success:286 print("\n๐ Model deployment completed successfully!")287 else:288 print("\n๐ฅ Model deployment failed!")