nonllm-chatbot / src /models /intent_classifier.py
Hreeem's picture
Deploy to Hugging Face (clean history)
2750f74
Raw History Blame Contribute Delete
1.83 kB
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import torch
torch.set_default_device('cpu')
from sentence_transformers import SentenceTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, f1_score, classification_report
def train_intent_model(X_train, y_train, X_val, y_val, cfg):
print("\n--- Training Intent Model on CPU ---")
embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', device='cpu')
print("Encoding training data...")
X_train_emb = embedder.encode(
list(X_train),
show_progress_bar=True,
batch_size=16,
convert_to_numpy=True,
device='cpu'
)
print("Encoding validation data...")
X_val_emb = embedder.encode(
list(X_val),
show_progress_bar=True,
batch_size=16,
convert_to_numpy=True,
device='cpu'
)
clf = make_pipeline(
StandardScaler(),
LogisticRegression(
max_iter=2000,
class_weight='balanced',
C=2.0,
solver='lbfgs',
random_state=cfg.random_state,
n_jobs=-1
)
)
print("Fitting classifier...")
clf.fit(X_train_emb, y_train)
print("Evaluating classifier...")
pred = clf.predict(X_val_emb)
metrics = {
'val_accuracy': float(accuracy_score(y_val, pred)),
'val_f1_macro': float(f1_score(y_val, pred, average='macro')),
'val_f1_weighted': float(f1_score(y_val, pred, average='weighted')),
'classification_report': classification_report(y_val, pred, zero_division=0, output_dict=True)
}
return embedder, clf, metrics