Text Classification
Transformers
Safetensors
English
bert
multi-text-classification
classification
intent-classification
intent-detection
nlp
natural-language-processing
edge-ai
iot
smart-home
location-intelligence
voice-assistant
conversational-ai
real-time
bert-local
bert-mini
local-search
business-category-classification
fast-inference
lightweight-model
on-device-nlp
offline-nlp
mobile-ai
multilingual-nlp
intent-routing
category-detection
query-understanding
artificial-intelligence
assistant-ai
smart-cities
customer-support
productivity-tools
contextual-ai
semantic-search
user-intent
microservices
smart-query-routing
industry-application
aiops
domain-specific-nlp
location-aware-ai
intelligent-routing
edge-nlp
smart-query-classifier
zero-shot-classification
smart-search
location-awareness
contextual-intelligence
geolocation
query-classification
multilingual-intent
chatbot-nlp
enterprise-ai
sdk-integration
api-ready
developer-tools
real-world-ai
geo-intelligence
embedded-ai
smart-routing
voice-interface
smart-devices
contextual-routing
fast-nlp
data-driven-ai
inference-optimization
digital-assistants
neural-nlp
ai-automation
lightweight-transformers
Run this model with HF Jobs
Run boltuix/bert-local on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/boltuix/bert-local. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/boltuix/bert-local on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use boltuix/bert-local with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use boltuix/bert-local with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="boltuix/bert-local")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("boltuix/bert-local") model = AutoModelForSequenceClassification.from_pretrained("boltuix/bert-local", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files350 unmodified lines351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567350 unmodified lines```### Training Code```pythonimport pandas as pdfrom transformers import BertTokenizer, BertForSequenceClassification, Trainer, TrainingArguments, TrainerCallbackfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score, f1_scoreimport torchfrom torch.utils.data import Datasetimport shutilfrom tqdm import tqdmimport numpy as np# === 0. Define model and output paths ===MODEL_NAME = "bert-mini"OUTPUT_DIR = "./bert-local"# === 1. Custom callback for tqdm progress bar ===class TQDMProgressBarCallback(TrainerCallback):def __init__(self):super().__init__()self.progress_bar = Nonedef on_train_begin(self, args, state, control, **kwargs):self.total_steps = state.max_stepsself.progress_bar = tqdm(total=self.total_steps, desc="Training", unit="step")def on_step_end(self, args, state, control, **kwargs):self.progress_bar.update(1)self.progress_bar.set_postfix({"epoch": f"{state.epoch:.2f}","step": state.global_step})def on_train_end(self, args, state, control, **kwargs):if self.progress_bar is not None:self.progress_bar.close()self.progress_bar = None# === 2. Load and preprocess data ===dataset_path = 'dataset.csv'df = pd.read_csv(dataset_path)df = df.dropna(subset=['category'])df.columns = ['label', 'text'] # Rename columns# === 3. Encode labels ===labels = sorted(df["label"].unique())label_to_id = {label: idx for idx, label in enumerate(labels)}id_to_label = {idx: label for label, idx in label_to_id.items()}df['label'] = df['label'].map(label_to_id)# === 4. Train-val split ===train_texts, val_texts, train_labels, val_labels = train_test_split(df['text'].tolist(), df['label'].tolist(), test_size=0.2, random_state=42, stratify=df['label'])# === 5. Tokenizer ===tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)# === 6. Dataset class ===class CategoryDataset(Dataset):def __init__(self, texts, labels, tokenizer, max_length=128):self.texts = textsself.labels = labelsself.tokenizer = tokenizerself.max_length = max_lengthdef __len__(self):return len(self.texts)def __getitem__(self, idx):encoding = self.tokenizer(self.texts[idx],padding='max_length',truncation=True,max_length=self.max_length,return_tensors='pt')return {'input_ids': encoding['input_ids'].squeeze(0),'attention_mask': encoding['attention_mask'].squeeze(0),'labels': torch.tensor(self.labels[idx], dtype=torch.long)}# === 7. Load datasets ===train_dataset = CategoryDataset(train_texts, train_labels, tokenizer)val_dataset = CategoryDataset(val_texts, val_labels, tokenizer)# === 8. Load model with num_labels ===model = BertForSequenceClassification.from_pretrained(MODEL_NAME,num_labels=len(label_to_id))# === 9. Define metrics for evaluation ===def compute_metrics(eval_pred):logits, labels = eval_predpredictions = np.argmax(logits, axis=-1)acc = accuracy_score(labels, predictions)f1 = f1_score(labels, predictions, average='weighted')return {'accuracy': acc,'f1_weighted': f1,}# === 10. Training arguments ===training_args = TrainingArguments(output_dir='./results',run_name="bert-local",num_train_epochs=5,per_device_train_batch_size=16,per_device_eval_batch_size=16,warmup_steps=500,weight_decay=0.01,logging_dir='./logs',logging_steps=10,eval_strategy="epoch",report_to="none")# === 11. Trainer setup ===trainer = Trainer(model=model,args=training_args,train_dataset=train_dataset,eval_dataset=val_dataset,compute_metrics=compute_metrics,callbacks=[TQDMProgressBarCallback()])# === 12. Train and evaluate ===trainer.train()trainer.evaluate()# === 13. Save model and tokenizer ===model.config.label2id = label_to_idmodel.config.id2label = id_to_labelmodel.config.num_labels = len(label_to_id)model.save_pretrained(OUTPUT_DIR)tokenizer.save_pretrained(OUTPUT_DIR)# === 14. Zip model directory ===shutil.make_archive("bert-local", 'zip', OUTPUT_DIR)print("✅ Training complete. Model and tokenizer saved to ./bert-local")print("✅ Model directory zipped to bert-local.zip")# === 15. Test function with confidence threshold ===def run_test_cases(model, tokenizer, test_sentences, label_to_id, id_to_label, confidence_threshold=0.5):model.eval()device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)correct = 0total = len(test_sentences)results = []for text, expected_label in test_sentences:encoding = tokenizer(text,padding='max_length',truncation=True,max_length=128,return_tensors='pt')input_ids = encoding['input_ids'].to(device)attention_mask = encoding['attention_mask'].to(device)with torch.no_grad():outputs = model(input_ids, attention_mask=attention_mask)probs = torch.nn.functional.softmax(outputs.logits, dim=-1)max_prob, predicted_id = torch.max(probs, dim=1)predicted_label = id_to_label[predicted_id.item()]if max_prob.item() < confidence_threshold:predicted_label = "unknown"is_correct = (predicted_label == expected_label)if is_correct:correct += 1results.append({"sentence": text,"expected": expected_label,"predicted": predicted_label,"confidence": max_prob.item(),"correct": is_correct})accuracy = correct / total * 100print(f"\nTest Cases Accuracy: {accuracy:.2f}% ({correct}/{total} correct)")for r in results:status = "✓" if r["correct"] else "✗"print(f"{status} '{r['sentence']}'")print(f" Expected: {r['expected']}, Predicted: {r['predicted']}, Confidence: {r['confidence']:.3f}")assert accuracy >= 70, f"Test failed: Accuracy {accuracy:.2f}% < 70%"return results# === 16. Sample test sentences for testing ===test_sentences = [("Where is the nearest airport to this location?", "airport"),("Can I bring a laptop through airport security?", "airport"),("How do I get to the closest airport terminal?", "airport"),("Need help finding an accounting firm for tax planning.", "accounting firm"),("Can an accounting firm help with financial audits?", "accounting firm"),("Looking for an accounting firm to manage payroll.", "accounting firm"),]print("\nRunning test cases...")test_results = run_test_cases(model, tokenizer, test_sentences, label_to_id, id_to_label)print("✅ Test cases completed.")```---## Evaluation 📈350 unmodified lines351352353354355356357358350 unmodified lines```### Training Code- 📍 Get training [Source Code](https://huggingface.co/boltuix/bert-local/blob/main/colab_training_code.ipynb) 🌟- 📍 Dataset (comming soon..)---## Evaluation 📈
README.md+2-211