ngamulin commited on
Commit
5d5427a
·
verified ·
1 Parent(s): bdb5d9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -17
app.py CHANGED
@@ -1,41 +1,74 @@
1
  import torch
2
  import torch.nn as nn
 
3
  import gradio as gr
4
  from huggingface_hub import hf_hub_download
5
 
6
  REPO_ID = "SentilyticsOPJ/lstm_model"
7
  FILENAME = "lstm_model.pt"
8
 
 
 
 
 
 
9
 
10
- class TextBiLSTM(nn.Module):
11
- def __init__(self, vocab_size, embed_dim, hidden_dim, num_classes):
 
 
 
 
12
  super().__init__()
13
- self.embedding = nn.Embedding(vocab_size, embed_dim, padding_idx=0)
14
- self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True, bidirectional=True)
15
- self.fc = nn.Linear(hidden_dim * 2, num_classes)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def forward(self, x):
18
- x = self.embedding(x)
19
- _, (hidden, _) = self.lstm(x)
20
- x = torch.cat([hidden[0], hidden[1]], dim=1)
21
- return self.fc(x)
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
24
  obj = torch.load(path, map_location="cpu", weights_only=False)
25
  sd = obj["state_dict"]
26
 
27
- vocab_size, embed_dim = sd["embedding.weight"].shape
28
- hidden_dim = sd["lstm.weight_hh_l0"].shape[1]
29
- num_classes = sd["fc.weight"].shape[0]
30
-
31
- model = TextBiLSTM(vocab_size, embed_dim, hidden_dim, num_classes)
32
- model.load_state_dict(sd)
33
- model.eval()
34
-
35
  word2id = obj["word2id"]
36
  labels = obj["label_classes"]
37
  max_len = obj["max_len"]
38
 
 
 
 
 
39
 
40
  def predict(text):
41
  if not text.strip():
@@ -46,4 +79,5 @@ def predict(text):
46
  probs = torch.softmax(model(torch.tensor(ids).unsqueeze(0)), dim=1)[0]
47
  return {labels[i]: float(probs[i]) for i in range(len(labels))}
48
 
 
49
  gr.Interface(fn=predict, inputs="text", outputs="text").launch()
 
1
  import torch
2
  import torch.nn as nn
3
+ import torch.nn.functional as F
4
  import gradio as gr
5
  from huggingface_hub import hf_hub_download
6
 
7
  REPO_ID = "SentilyticsOPJ/lstm_model"
8
  FILENAME = "lstm_model.pt"
9
 
10
+ MAX_LEN = 80
11
+ EMBEDDING_DIM = 300
12
+ LSTM_UNITS = 96
13
+ SPATIAL_DROPOUT = 0.3
14
+ DROPOUT = 0.5
15
 
16
+
17
+ class BiLSTM(nn.Module):
18
+ def __init__(self, vocab_size, num_classes, embedding_matrix=None,
19
+ embedding_dim=EMBEDDING_DIM, lstm_units=LSTM_UNITS,
20
+ spatial_dropout=SPATIAL_DROPOUT, dropout=DROPOUT,
21
+ padding_idx=0):
22
  super().__init__()
23
+ self.padding_idx = padding_idx
24
+
25
+ self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx=padding_idx)
26
+ if embedding_matrix is not None:
27
+ self.embedding.weight.data.copy_(torch.from_numpy(embedding_matrix))
28
+
29
+ self.spatial_dropout = nn.Dropout1d(spatial_dropout)
30
+
31
+ self.lstm = nn.LSTM(
32
+ input_size=embedding_dim,
33
+ hidden_size=lstm_units,
34
+ num_layers=1,
35
+ batch_first=True,
36
+ bidirectional=True,
37
+ )
38
+ self.attn = nn.Linear(lstm_units * 2, 1)
39
+ self.dropout = nn.Dropout(dropout)
40
+ self.fc = nn.Linear(lstm_units * 2, num_classes)
41
 
42
  def forward(self, x):
43
+ emb = self.embedding(x)
44
+
45
+ emb = emb.transpose(1, 2)
46
+ emb = self.spatial_dropout(emb)
47
+ emb = emb.transpose(1, 2)
48
+
49
+ out, _ = self.lstm(emb)
50
+
51
+ scores = self.attn(out).squeeze(-1)
52
+ mask = (x == self.padding_idx)
53
+ mask[:, 0] = False
54
+ scores = scores.masked_fill(mask, float("-inf"))
55
+ weights = F.softmax(scores, dim=1).unsqueeze(-1)
56
+ context = (out * weights).sum(dim=1)
57
+
58
+ return self.fc(self.dropout(context))
59
 
60
  path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
61
  obj = torch.load(path, map_location="cpu", weights_only=False)
62
  sd = obj["state_dict"]
63
 
 
 
 
 
 
 
 
 
64
  word2id = obj["word2id"]
65
  labels = obj["label_classes"]
66
  max_len = obj["max_len"]
67
 
68
+ model = BiLSTM(len(word2id), len(labels))
69
+ model.load_state_dict(sd)
70
+ model.eval()
71
+
72
 
73
  def predict(text):
74
  if not text.strip():
 
79
  probs = torch.softmax(model(torch.tensor(ids).unsqueeze(0)), dim=1)[0]
80
  return {labels[i]: float(probs[i]) for i in range(len(labels))}
81
 
82
+
83
  gr.Interface(fn=predict, inputs="text", outputs="text").launch()