Instructions to use halderavik/contextual-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use halderavik/contextual-classifier with PEFT:
from peft import PeftModel from transformers import AutoModelForSequenceClassification base_model = AutoModelForSequenceClassification.from_pretrained("google/gemma-7b") model = PeftModel.from_pretrained(base_model, "halderavik/contextual-classifier") - Notebooks
- Google Colab
- Kaggle
Gemma-7B Survey Response Classifier - Inference
This document provides instructions for using the inference script to make predictions with the trained Gemma-7B model for survey response classification.
Requirements
- Python 3.8+
- PyTorch
- Transformers
- PEFT (Parameter-Efficient Fine-Tuning)
- Pandas
- BitsAndBytes (for 4-bit quantization)
Usage
The inference script (inference_gemma.py) supports two modes of operation:
- Single Prediction: Classify a single question-response pair
- Batch Prediction: Process multiple question-response pairs from a CSV file
Single Prediction
To classify a single question-response pair:
python inference_gemma.py --model_path /path/to/model --question "What is your favorite color?" --response "My favorite color is blue because it reminds me of the ocean."
Batch Prediction
To process multiple question-response pairs from a CSV file:
python inference_gemma.py --model_path /path/to/model --input_file data.csv --output_file predictions.csv --batch_size 4
The input CSV file should have at least the following columns:
question: The survey questionresponse: The response to classify
If the file also contains a label column, the script will calculate and report the accuracy of the predictions.
Command Line Arguments
--model_path: Path to the trained model directory (required)--input_file: Path to CSV file with question and response columns (for batch prediction)--output_file: Path to save predictions (default: 'predictions.csv')--question: Question for single prediction--response: Response for single prediction--batch_size: Batch size for inference (default: 4)
Output Format
Single Prediction
The script will print the question, response, prediction (Positive/Negative), and the confidence probability.
Batch Prediction
The script will generate a CSV file with the following columns:
question: The original questionresponse: The original responsetrue_label: The true label (if available in the input file)prediction: The predicted class (0 or 1)probability: The confidence probabilitylabel: The human-readable label (Positive/Negative)
Model Loading
The inference script is designed to handle different model formats:
PEFT Adapter Models: If the model directory contains an
adapter_config.jsonfile, the script will load the model using the PEFT configuration.Direct Model Weights: The script will look for any
.binfiles ormodel.safetensorsin the model directory and attempt to load them.Base Model: If no model weights are found, the script will use the base Gemma-7B model without fine-tuning.
Common Model File Formats
The script can load models saved in the following formats:
pytorch_model.bin: Standard PyTorch model filemodel.safetensors: SafeTensors format (more secure)- Any other
.binfile containing model weights
Troubleshooting Model Loading
If you see the warning "No model weights found at [path]. Using base model without fine-tuning", check the following:
Correct Model Path: Make sure you're pointing to the correct directory where your trained model is saved.
Model File Format: Ensure your model was saved with one of the supported formats. The script will log the files it finds in the model directory.
Training Output: Check the output directory from your training script. The model should be saved in one of these locations:
best_model/: Directory containing the best model based on validation metricsfinal_model/: Directory containing the final model after trainingcheckpoint_epoch_X/: Checkpoints saved after each epoch
Model Saving: If you're using a custom training script, ensure it's saving the model weights correctly.
Notes
- The model uses 4-bit quantization for memory efficiency
- The script automatically handles loading the model and tokenizer
- Logs are saved to a timestamped file for debugging purposes
- The model path should point to the directory where the trained model is saved (e.g., 'best_model' or 'final_model' from the training script)
- Downloads last month
- -
Model tree for halderavik/contextual-classifier
Base model
google/gemma-7b