| import torch |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
| class VibeCheckInference: |
| def __init__(self, model_path): |
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| self.tokenizer = AutoTokenizer.from_pretrained(model_path) |
| self.model = AutoModelForSequenceClassification.from_pretrained(model_path).to(self.device) |
| self.model.eval() |
|
|
| def analyze(self, text): |
| inputs = self.tokenizer( |
| text, |
| return_tensors="pt", |
| padding=True, |
| truncation=True, |
| max_length=128 |
| ).to(self.device) |
|
|
| with torch.no_grad(): |
| outputs = self.model(**inputs) |
| probs = torch.nn.functional.softmax(outputs.logits, dim=-1) |
| conf, pred = torch.max(probs, dim=-1) |
|
|
| result = "POSITIVE" if pred.item() == 1 else "NEGATIVE" |
| return { |
| "text": text, |
| "label": result, |
| "confidence": f"{conf.item() * 100:.2f}%" |
| } |
|
|
| |
| if __name__ == "__main__": |
| |
| engine = VibeCheckInference("./VibeCheck_v1_HF") |
| |
| sample = "Did you see the new movie?' B: 'Yeah, it was okay, but the ending felt a bit rushed.' A: 'I totally agree, it could have been better.'" |
| prediction = engine.analyze(sample) |
| print(f"Result: {prediction['label']} | Confidence: {prediction['confidence']}") |