File size: 10,198 Bytes
5b6f681 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | """Advanced inference pipeline with batch processing and model switching."""
import json
import os
from typing import List, Dict, Any, Optional, Union
import torch
import numpy as np
from transformers import (
AutoTokenizer,
AutoModelForSequenceClassification,
pipeline
)
from src.data_utils import load_config
class SentimentInference:
"""Advanced sentiment analysis inference pipeline."""
def __init__(
self,
model_path: str,
device: Optional[str] = None,
batch_size: int = 32
):
"""
Initialize inference pipeline.
Args:
model_path: Path to trained model or model name
device: Device to run inference on (auto-detect if None)
batch_size: Batch size for batch inference
"""
self.model_path = model_path
self.batch_size = batch_size
# Auto-detect device
if device is None:
self.device = "cuda" if torch.cuda.is_available() else "cpu"
else:
self.device = device
print(f"🚀 Loading model from: {model_path}")
print(f"🔧 Using device: {self.device}")
# Load model and tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
self.model = AutoModelForSequenceClassification.from_pretrained(model_path)
self.model.to(self.device)
self.model.eval()
# Load model info if available
self.model_info = self._load_model_info()
# Create pipeline for easy inference
self.pipeline = pipeline(
"sentiment-analysis",
model=self.model,
tokenizer=self.tokenizer,
device=0 if self.device == "cuda" else -1,
batch_size=self.batch_size
)
print("✅ Model loaded successfully!")
def _load_model_info(self) -> Optional[Dict[str, Any]]:
"""Load model information if available."""
info_path = os.path.join(self.model_path, "model_info.json")
if os.path.exists(info_path):
with open(info_path, "r") as f:
return json.load(f)
return None
def predict_single(self, text: str) -> Dict[str, Any]:
"""
Predict sentiment for a single text.
Args:
text: Input text
Returns:
Dictionary with prediction results
"""
result = self.pipeline(text)[0]
return {
"text": text,
"predicted_label": result["label"],
"confidence": result["score"],
"model_path": self.model_path
}
def predict_batch(self, texts: List[str]) -> List[Dict[str, Any]]:
"""
Predict sentiment for a batch of texts.
Args:
texts: List of input texts
Returns:
List of prediction results
"""
results = self.pipeline(texts)
predictions = []
for text, result in zip(texts, results):
predictions.append({
"text": text,
"predicted_label": result["label"],
"confidence": result["score"],
"model_path": self.model_path
})
return predictions
def predict_with_probabilities(self, text: str) -> Dict[str, Any]:
"""
Predict with full probability distribution.
Args:
text: Input text
Returns:
Dictionary with full probability distribution
"""
# Tokenize input
inputs = self.tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512
)
inputs = {k: v.to(self.device) for k, v in inputs.items()}
# Get predictions
with torch.no_grad():
outputs = self.model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=-1)
probabilities = probabilities.cpu().numpy()[0]
# Get label mapping
id2label = self.model.config.id2label
# Create probability distribution
prob_dist = {}
for label_id, prob in enumerate(probabilities):
label = id2label.get(label_id, f"LABEL_{label_id}")
prob_dist[label] = float(prob)
# Get predicted label
predicted_id = np.argmax(probabilities)
predicted_label = id2label.get(predicted_id, f"LABEL_{predicted_id}")
return {
"text": text,
"predicted_label": predicted_label,
"confidence": float(probabilities[predicted_id]),
"probability_distribution": prob_dist,
"model_path": self.model_path
}
def get_attention_weights(self, text: str) -> Dict[str, Any]:
"""
Get attention weights for interpretability.
Args:
text: Input text
Returns:
Dictionary with attention weights and tokens
"""
# Tokenize input
inputs = self.tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512
)
inputs = {k: v.to(self.device) for k, v in inputs.items()}
# Get attention weights
with torch.no_grad():
outputs = self.model(**inputs, output_attentions=True)
attentions = outputs.attentions
# Convert to numpy and get tokens
attention_weights = [att.cpu().numpy() for att in attentions]
tokens = self.tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
return {
"text": text,
"tokens": tokens,
"attention_weights": attention_weights,
"num_layers": len(attention_weights),
"num_heads": attention_weights[0].shape[1]
}
def benchmark_inference(self, texts: List[str], num_runs: int = 5) -> Dict[str, Any]:
"""
Benchmark inference performance.
Args:
texts: List of texts to benchmark
num_runs: Number of runs for averaging
Returns:
Dictionary with benchmark results
"""
import time
times = []
# Warm up
self.predict_batch(texts[:min(5, len(texts))])
# Benchmark
for _ in range(num_runs):
start_time = time.time()
self.predict_batch(texts)
end_time = time.time()
times.append(end_time - start_time)
avg_time = np.mean(times)
std_time = np.std(times)
throughput = len(texts) / avg_time
return {
"num_texts": len(texts),
"num_runs": num_runs,
"avg_time_seconds": avg_time,
"std_time_seconds": std_time,
"throughput_texts_per_second": throughput,
"device": self.device,
"batch_size": self.batch_size
}
def get_model_summary(self) -> Dict[str, Any]:
"""Get model summary information."""
param_count = sum(p.numel() for p in self.model.parameters())
trainable_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
summary = {
"model_path": self.model_path,
"device": self.device,
"total_parameters": param_count,
"trainable_parameters": trainable_params,
"model_config": self.model.config.to_dict() if hasattr(self.model.config, 'to_dict') else str(self.model.config)
}
if self.model_info:
summary["training_info"] = self.model_info
return summary
def create_inference_pipeline(model_path: str, **kwargs) -> SentimentInference:
"""Factory function to create inference pipeline."""
return SentimentInference(model_path, **kwargs)
def main():
"""CLI entry point for inference."""
import argparse
parser = argparse.ArgumentParser(description="Run sentiment analysis inference")
parser.add_argument("--model", type=str, required=True, help="Path to model or model name")
parser.add_argument("--text", type=str, help="Single text to analyze")
parser.add_argument("--texts", type=str, nargs="+", help="Multiple texts to analyze")
parser.add_argument("--batch_size", type=int, default=32, help="Batch size for inference")
parser.add_argument("--device", type=str, help="Device to use (cuda/cpu)")
parser.add_argument("--probabilities", action="store_true", help="Show full probability distribution")
parser.add_argument("--attention", action="store_true", help="Show attention weights")
parser.add_argument("--benchmark", action="store_true", help="Run benchmark")
args = parser.parse_args()
# Create inference pipeline
pipeline = SentimentInference(
model_path=args.model,
device=args.device,
batch_size=args.batch_size
)
# Single text prediction
if args.text:
if args.probabilities:
result = pipeline.predict_with_probabilities(args.text)
elif args.attention:
result = pipeline.get_attention_weights(args.text)
else:
result = pipeline.predict_single(args.text)
print(json.dumps(result, indent=2))
# Batch prediction
elif args.texts:
if args.benchmark:
benchmark_result = pipeline.benchmark_inference(args.texts)
print("Benchmark Results:")
print(json.dumps(benchmark_result, indent=2))
results = pipeline.predict_batch(args.texts)
print(json.dumps(results, indent=2))
# Model summary
else:
summary = pipeline.get_model_summary()
print("Model Summary:")
print(json.dumps(summary, indent=2))
if __name__ == "__main__":
main() |