Spaces:
Sleeping
Sleeping
Download src/models/intent_classifier.py from Darshankumarr03/nonllm-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/spaces/Darshankumarr03/nonllm-chatbot/resolve/main/src/models/intent_classifier.py
- Command line
-
hf download hf://spaces/Darshankumarr03/nonllm-chatbot/src/models/intent_classifier.py
-
curl -L -o intent_classifier.py https://huggingface.co/spaces/Darshankumarr03/nonllm-chatbot/resolve/main/src/models/intent_classifier.py
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 | |