Spaces:
Sleeping
Sleeping
Update src/inference.py
Browse files- src/inference.py +22 -66
src/inference.py
CHANGED
|
@@ -1,74 +1,30 @@
|
|
| 1 |
import joblib
|
| 2 |
import torch
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
-
from sklearn.preprocessing import LabelEncoder
|
| 6 |
-
from sklearn.model_selection import train_test_split
|
| 7 |
-
from torch.utils.data import Dataset, DataLoader
|
| 8 |
-
from src.data_processing import load_and_clean_data
|
| 9 |
from src.model_def import EmotionTransformer
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
EPOCHS = 5
|
| 15 |
-
LR = 1e-3
|
| 16 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
self.y = torch.tensor(y, dtype=torch.long)
|
| 23 |
-
def __len__(self): return len(self.X)
|
| 24 |
-
def __getitem__(self, idx): return self.X[idx], self.y[idx]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def train():
|
| 28 |
-
df = load_and_clean_data()
|
| 29 |
-
toks = df['clean'].str.split()
|
| 30 |
-
|
| 31 |
-
# Build vocab
|
| 32 |
-
ctr = Counter(tok for sent in toks for tok in sent)
|
| 33 |
-
vocab = {w:i+2 for i,(w,_) in enumerate(ctr.most_common())}
|
| 34 |
-
vocab['<PAD>'], vocab['<UNK>'] = 0, 1
|
| 35 |
-
joblib.dump(vocab, 'vocab.pkl')
|
| 36 |
-
|
| 37 |
-
# Encode + pad
|
| 38 |
-
X = [
|
| 39 |
-
([vocab.get(tok,1) for tok in sent] + [0]*(MAX_LEN-len(sent)))[:MAX_LEN]
|
| 40 |
-
for sent in toks
|
| 41 |
-
]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
le = LabelEncoder()
|
| 45 |
-
y = le.fit_transform(df['label'])
|
| 46 |
-
joblib.dump(le, 'label_encoder.pkl')
|
| 47 |
-
|
| 48 |
-
# Split & loader
|
| 49 |
-
X_tr, X_va, y_tr, y_va = train_test_split(X, y, test_size=0.2,
|
| 50 |
-
stratify=y, random_state=42)
|
| 51 |
-
tr_loader = DataLoader(EmotionDataset(X_tr, y_tr), batch_size=BATCH_SIZE, shuffle=True)
|
| 52 |
-
va_loader = DataLoader(EmotionDataset(X_va, y_va), batch_size=BATCH_SIZE)
|
| 53 |
-
|
| 54 |
-
# Model, optimizer, loss
|
| 55 |
-
model = EmotionTransformer(len(vocab), num_classes=len(le.classes_)).to(DEVICE)
|
| 56 |
-
opt = torch.optim.Adam(model.parameters(), lr=LR)
|
| 57 |
-
crit = torch.nn.CrossEntropyLoss()
|
| 58 |
-
|
| 59 |
-
# Training loop
|
| 60 |
-
for epoch in range(EPOCHS):
|
| 61 |
-
model.train(); total_loss = 0
|
| 62 |
-
for xb, yb in tr_loader:
|
| 63 |
-
xb, yb = xb.to(DEVICE), yb.to(DEVICE)
|
| 64 |
-
opt.zero_grad()
|
| 65 |
-
loss = crit(model(xb), yb)
|
| 66 |
-
loss.backward(); opt.step()
|
| 67 |
-
total_loss += loss.item()
|
| 68 |
-
print(f"Epoch {epoch+1}/{EPOCHS} Loss: {total_loss/len(tr_loader):.4f}")
|
| 69 |
-
|
| 70 |
-
# Save weights
|
| 71 |
-
torch.save(model.state_dict(), 'emotion_transformer_model.pth')
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import joblib
|
| 2 |
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from src.data_processing import clean_text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from src.model_def import EmotionTransformer
|
| 6 |
|
| 7 |
+
# Load artifacts
|
| 8 |
+
vocab = joblib.load('vocab.pkl')
|
| 9 |
+
le = joblib.load('label_encoder.pkl')
|
|
|
|
|
|
|
| 10 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 11 |
|
| 12 |
+
# Recreate model
|
| 13 |
+
model = EmotionTransformer(len(vocab), num_classes=len(le.classes_)).to(DEVICE)
|
| 14 |
+
model.load_state_dict(torch.load('emotion_transformer_model.pth', map_location=DEVICE))
|
| 15 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
MAX_LEN = 32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
def predict(text):
|
| 20 |
+
toks = clean_text(text).split()
|
| 21 |
+
idxs = [vocab.get(tok,1) for tok in toks]
|
| 22 |
+
pad = (idxs + [0]*MAX_LEN)[:MAX_LEN]
|
| 23 |
+
x = torch.tensor([pad], dtype=torch.long).to(DEVICE)
|
| 24 |
+
|
| 25 |
+
# MC-dropout inference
|
| 26 |
+
model.train()
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
probs = torch.stack([F.softmax(model(x), dim=1) for _ in range(5)])
|
| 29 |
+
avg = probs.mean(dim=0)
|
| 30 |
+
return le.inverse_transform([avg.argmax().item()])[0]
|