Create inference.py
Browse files- inference.py +39 -0
inference.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
|
| 4 |
+
class CritiqueCoreInference:
|
| 5 |
+
def __init__(self, model_path):
|
| 6 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 8 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model_path).to(self.device)
|
| 9 |
+
self.model.eval()
|
| 10 |
+
|
| 11 |
+
def analyze(self, text):
|
| 12 |
+
inputs = self.tokenizer(
|
| 13 |
+
text,
|
| 14 |
+
return_tensors="pt",
|
| 15 |
+
padding=True,
|
| 16 |
+
truncation=True,
|
| 17 |
+
max_length=128
|
| 18 |
+
).to(self.device)
|
| 19 |
+
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = self.model(**inputs)
|
| 22 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 23 |
+
conf, pred = torch.max(probs, dim=-1)
|
| 24 |
+
|
| 25 |
+
result = "POSITIVE" if pred.item() == 1 else "NEGATIVE"
|
| 26 |
+
return {
|
| 27 |
+
"text": text,
|
| 28 |
+
"label": result,
|
| 29 |
+
"confidence": f"{conf.item() * 100:.2f}%"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Usage
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
# Point this to your unzipped folder
|
| 35 |
+
engine = CritiqueCoreInference("./CritiqueCore_v1_HF")
|
| 36 |
+
|
| 37 |
+
sample = "The plot was a bit slow, but overall a great experience."
|
| 38 |
+
prediction = engine.analyze(sample)
|
| 39 |
+
print(f"Result: {prediction['label']} | Confidence: {prediction['confidence']}")
|