Upload inference.py with huggingface_hub
Browse files- inference.py +112 -0
inference.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Inference script for Linear Regression Text Classification Model
|
| 3 |
+
This script demonstrates how to load and use the trained model for predictions.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import joblib
|
| 7 |
+
import numpy as np
|
| 8 |
+
from typing import Union, List
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TextClassifier:
|
| 12 |
+
"""Wrapper class for the Linear Regression text classification model."""
|
| 13 |
+
|
| 14 |
+
def __init__(self, model_path: str):
|
| 15 |
+
"""
|
| 16 |
+
Initialize the classifier by loading the model.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
model_path: Path to the saved joblib model file
|
| 20 |
+
"""
|
| 21 |
+
self.model = joblib.load(model_path)
|
| 22 |
+
self.class_names = {
|
| 23 |
+
0: "Strictly Necessary",
|
| 24 |
+
1: "Functionality",
|
| 25 |
+
2: "Analytics",
|
| 26 |
+
3: "Advertising/Tracking"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
def predict(self, features: np.ndarray) -> np.ndarray:
|
| 30 |
+
"""
|
| 31 |
+
Make predictions on input features.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
features: Preprocessed TF-IDF features (numpy array)
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
Array of predicted class labels
|
| 38 |
+
"""
|
| 39 |
+
predictions = self.model.predict(features)
|
| 40 |
+
return predictions.astype(int)
|
| 41 |
+
|
| 42 |
+
def predict_single(self, features: np.ndarray) -> int:
|
| 43 |
+
"""
|
| 44 |
+
Make a prediction for a single sample.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
features: Preprocessed TF-IDF features for one sample
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
Predicted class label
|
| 51 |
+
"""
|
| 52 |
+
if len(features.shape) == 1:
|
| 53 |
+
features = features.reshape(1, -1)
|
| 54 |
+
prediction = self.model.predict(features)[0]
|
| 55 |
+
return int(prediction)
|
| 56 |
+
|
| 57 |
+
def get_class_name(self, class_id: int) -> str:
|
| 58 |
+
"""
|
| 59 |
+
Get the name of a class given its ID.
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
class_id: The numeric class identifier
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
The name/description of the class
|
| 66 |
+
"""
|
| 67 |
+
return self.class_names.get(class_id, f"Unknown Class {class_id}")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def load_model(model_path: str = "Linear Regression/LR_TFIDF+NAME.joblib") -> TextClassifier:
|
| 71 |
+
"""
|
| 72 |
+
Load the trained model from disk.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
model_path: Path to the model file
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
TextClassifier instance
|
| 79 |
+
"""
|
| 80 |
+
return TextClassifier(model_path)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def main():
|
| 84 |
+
"""Example usage of the model."""
|
| 85 |
+
# Load the model
|
| 86 |
+
print("Loading model...")
|
| 87 |
+
classifier = load_model()
|
| 88 |
+
print("Model loaded successfully!")
|
| 89 |
+
|
| 90 |
+
# Example: Create dummy features for demonstration
|
| 91 |
+
# In practice, you would use your TF-IDF vectorizer to transform text
|
| 92 |
+
print("\nExample prediction (using random features for demonstration):")
|
| 93 |
+
dummy_features = np.random.randn(1, 100) # Replace with actual TF-IDF features
|
| 94 |
+
|
| 95 |
+
prediction = classifier.predict_single(dummy_features)
|
| 96 |
+
class_name = classifier.get_class_name(prediction)
|
| 97 |
+
|
| 98 |
+
print(f"Predicted class: {prediction}")
|
| 99 |
+
print(f"Class name: {class_name}")
|
| 100 |
+
|
| 101 |
+
# Batch prediction example
|
| 102 |
+
print("\nBatch prediction example:")
|
| 103 |
+
batch_features = np.random.randn(5, 100) # 5 samples
|
| 104 |
+
batch_predictions = classifier.predict(batch_features)
|
| 105 |
+
|
| 106 |
+
print(f"Predictions for {len(batch_predictions)} samples:")
|
| 107 |
+
for i, pred in enumerate(batch_predictions):
|
| 108 |
+
print(f" Sample {i+1}: Class {pred} ({classifier.get_class_name(pred)})")
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
main()
|