Adya662/bert-tiny-amd
112
1#!/usr/bin/env python32"""3Simple script to upload model files to Hugging Face Hub4"""5 6import os7import torch8from transformers import AutoTokenizer9from huggingface_hub import HfApi10import 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 upload_files():85 """Upload files to Hugging Face Hub"""86 87 print("๐ Starting file 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 and save tokenizer from base model107 print("๐ฅ Loading tokenizer...")108 tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)109 tokenizer.save_pretrained(".")110 111 # Copy model weights112 if os.path.exists(MODEL_PATH):113 print("๐ฅ Copying model weights...")114 import shutil115 shutil.copy2(MODEL_PATH, "pytorch_model.bin")116 print("โ
Model weights copied successfully")117 else:118 print(f"โ Model file {MODEL_PATH} not found!")119 return False120 121 # Create README.md122 readme_content = """---123license: mit124tags:125- text-classification126- answering-machine-detection127- bert-tiny128- binary-classification129- call-center130- voice-processing131pipeline_tag: text-classification132---133 134# BERT-Tiny AMD Classifier135 136A lightweight BERT-Tiny model fine-tuned for Answering Machine Detection (AMD) in call center environments.137 138## Model Description139 140This 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.141 142## Model Architecture143 144- **Base Model**: `prajjwal1/bert-tiny` (2 layers, 128 hidden size, 2 attention heads)145- **Total Parameters**: ~4.4M (lightweight and efficient)146- **Input**: User transcript text (max 128 tokens)147- **Output**: Single logit with sigmoid activation for binary classification148- **Loss Function**: BCEWithLogitsLoss with positive weight for class imbalance149 150## Performance151 152- **Validation Accuracy**: 93.94%153- **Precision**: 92.75%154- **Recall**: 87.27%155- **F1-Score**: 89.93%156- **Training Device**: MPS (Apple Silicon GPU)157- **Best Epoch**: 15 (with early stopping)158 159## Training Data160 161- **Total Samples**: 3,548 phone call transcripts162- **Training Set**: 2,838 samples163- **Validation Set**: 710 samples164- **Class Distribution**: 30.8% machine calls, 69.2% human calls165- **Source**: ElevateNow call center data166 167## Usage168 169### Basic Inference170 171```python172from transformers import AutoTokenizer, AutoModelForSequenceClassification173import torch174 175# Load model and tokenizer176model = AutoModelForSequenceClassification.from_pretrained("Adya662/bert-tiny-amd")177tokenizer = AutoTokenizer.from_pretrained("Adya662/bert-tiny-amd")178 179# Prepare input180text = "Hello, this is John speaking"181inputs = tokenizer(text, return_tensors="pt", max_length=128, truncation=True, padding=True)182 183# Make prediction184with torch.no_grad():185 outputs = model(**inputs)186 logits = outputs.logits.squeeze(-1)187 probability = torch.sigmoid(logits).item()188 is_machine = probability >= 0.5189 190print(f"Prediction: {'Machine' if is_machine else 'Human'}")191print(f"Confidence: {probability:.4f}")192```193 194## Training Details195 196- **Optimizer**: AdamW with weight decay (0.01)197- **Learning Rate**: 3e-5 with linear scheduling198- **Batch Size**: 32199- **Epochs**: 15 (with early stopping)200- **Early Stopping**: Patience of 3 epochs201- **Class Imbalance**: Handled with positive weight202 203## Limitations204 205- Trained on English phone call transcripts206- May not generalize well to other languages or domains207- Performance may vary with different transcription quality208- Designed for short utterances (max 128 tokens)209 210## License211 212MIT License - see LICENSE file for details.213"""214 215 with open("README.md", "w") as f:216 f.write(readme_content)217 218 # Upload to Hub219 print("โฌ๏ธ Uploading to Hugging Face Hub...")220 try:221 api.upload_folder(222 folder_path=".",223 repo_id=REPO_ID,224 repo_type="model",225 commit_message="Upload trained BERT-Tiny AMD model"226 )227 print("โ
Model uploaded successfully!")228 print(f"๐ Model available at: https://huggingface.co/{REPO_ID}")229 return True230 except Exception as e:231 print(f"โ Upload failed: {e}")232 return False233 234if __name__ == "__main__":235 success = upload_files()236 if success:237 print("\n๐ Model deployment completed successfully!")238 else:239 print("\n๐ฅ Model deployment failed!")