Spaces:
Runtime error
Runtime error
File size: 1,763 Bytes
c9f668a 3d2c1a8 c9f668a | 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 | import os
import torch
import torch.nn.functional as F
from transformers import AutoModelForSequenceClassification, AutoTokenizer
MAX_LENGTH = 128
DEFAULT_MODEL_ID = "FallacyHunter/Fallacy-Hunter-Roberta"
class FallacyClassifier:
def __init__(self, model_path=None):
self.model_path = model_path or os.environ.get("ROBERTA_MODEL_ID", DEFAULT_MODEL_ID)
self.model = None
self.tokenizer = None
self._load_model()
def _load_model(self):
print(f"Loading RoBERTa tokenizer from {self.model_path}...")
self.tokenizer = AutoTokenizer.from_pretrained(self.model_path)
print("Loading RoBERTa model...")
self.model = AutoModelForSequenceClassification.from_pretrained(
self.model_path,
torch_dtype=torch.bfloat16,
device_map="cpu",
)
self.model.eval()
print("RoBERTa loaded successfully")
def classify(self, text: str) -> dict:
inputs = self.tokenizer(
text,
return_tensors="pt",
truncation=True,
padding="max_length",
max_length=MAX_LENGTH
)
inputs = {k: v.to(self.model.device) for k, v in inputs.items()}
with torch.no_grad():
logits = self.model(**inputs).logits
probs = F.softmax(logits.float(), dim=-1)[0]
id2label = self.model.config.id2label
probabilities = {id2label[i]: float(probs[i]) for i in range(len(probs))}
top_idx = int(probs.argmax())
final_label = id2label[top_idx]
confidence = float(probs[top_idx])
return {
"final_label": final_label,
"confidence": confidence,
"probabilities": probabilities
}
|