Spaces:
Sleeping
Sleeping
Commit ·
5f9f2be
1
Parent(s): 12fcc4e
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,8 +14,8 @@ nltk.download(['punkt', 'punkt_tab'], quiet=True)
|
|
| 14 |
|
| 15 |
DEVICE = torch.device("cpu")
|
| 16 |
|
| 17 |
-
CACHE_FILE = "ubuntu_data_cache.pt" #
|
| 18 |
-
MODEL_FILE = "ubuntu_chatbot_best.pt" # trained model
|
| 19 |
|
| 20 |
|
| 21 |
# ------------- tokenization + helpers -------------
|
|
@@ -93,21 +93,21 @@ class Encoder(nn.Module):
|
|
| 93 |
)
|
| 94 |
# projection from 1024 (2 * 512) back to 512
|
| 95 |
self.fc = nn.Linear(1024, 512)
|
| 96 |
-
self.norm = nn.LayerNorm(512)
|
| 97 |
|
| 98 |
def forward(self, x):
|
| 99 |
# x: [B, T]
|
| 100 |
-
e = self.emb(x)
|
| 101 |
-
out, h = self.gru(e)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
out = self.fc(out)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
h = h.view(2, 2, h.size(1), -1)
|
| 108 |
-
h = torch.sum(h, dim=1)
|
| 109 |
|
| 110 |
-
return out, h
|
| 111 |
|
| 112 |
|
| 113 |
class Decoder(nn.Module):
|
|
@@ -132,19 +132,19 @@ class Decoder(nn.Module):
|
|
| 132 |
hidden: [2, B, 512] encoder hidden (num_layers, batch, hidden)
|
| 133 |
enc_out:[B, T, 512]
|
| 134 |
"""
|
| 135 |
-
e = self.dropout(self.emb(inp))
|
| 136 |
|
| 137 |
# attention over encoder outputs
|
| 138 |
-
energy = self.attn(enc_out)
|
| 139 |
# use top layer hidden state for attention
|
| 140 |
attn_scores = torch.bmm(hidden[-1].unsqueeze(1), energy.transpose(1, 2)) # [B,1,T]
|
| 141 |
attn_weights = F.softmax(attn_scores.squeeze(1), dim=-1).unsqueeze(1) # [B,1,T]
|
| 142 |
-
ctx = torch.bmm(attn_weights, enc_out)
|
| 143 |
|
| 144 |
-
x = torch.cat((e, ctx), dim=-1)
|
| 145 |
-
out, hidden = self.gru(x, hidden)
|
| 146 |
-
out = self.norm(out.squeeze(1))
|
| 147 |
-
logits = self.out(out)
|
| 148 |
return logits, hidden
|
| 149 |
|
| 150 |
|
|
|
|
| 14 |
|
| 15 |
DEVICE = torch.device("cpu")
|
| 16 |
|
| 17 |
+
CACHE_FILE = "ubuntu_data_cache.pt" # To get the Vocab from cache
|
| 18 |
+
MODEL_FILE = "ubuntu_chatbot_best.pt" # trained model
|
| 19 |
|
| 20 |
|
| 21 |
# ------------- tokenization + helpers -------------
|
|
|
|
| 93 |
)
|
| 94 |
# projection from 1024 (2 * 512) back to 512
|
| 95 |
self.fc = nn.Linear(1024, 512)
|
| 96 |
+
self.norm = nn.LayerNorm(512)
|
| 97 |
|
| 98 |
def forward(self, x):
|
| 99 |
# x: [B, T]
|
| 100 |
+
e = self.emb(x)
|
| 101 |
+
out, h = self.gru(e)
|
| 102 |
|
| 103 |
+
|
| 104 |
+
out = self.fc(out)
|
| 105 |
|
| 106 |
+
|
| 107 |
+
h = h.view(2, 2, h.size(1), -1)
|
| 108 |
+
h = torch.sum(h, dim=1)
|
| 109 |
|
| 110 |
+
return out, h
|
| 111 |
|
| 112 |
|
| 113 |
class Decoder(nn.Module):
|
|
|
|
| 132 |
hidden: [2, B, 512] encoder hidden (num_layers, batch, hidden)
|
| 133 |
enc_out:[B, T, 512]
|
| 134 |
"""
|
| 135 |
+
e = self.dropout(self.emb(inp))
|
| 136 |
|
| 137 |
# attention over encoder outputs
|
| 138 |
+
energy = self.attn(enc_out)
|
| 139 |
# use top layer hidden state for attention
|
| 140 |
attn_scores = torch.bmm(hidden[-1].unsqueeze(1), energy.transpose(1, 2)) # [B,1,T]
|
| 141 |
attn_weights = F.softmax(attn_scores.squeeze(1), dim=-1).unsqueeze(1) # [B,1,T]
|
| 142 |
+
ctx = torch.bmm(attn_weights, enc_out)
|
| 143 |
|
| 144 |
+
x = torch.cat((e, ctx), dim=-1)
|
| 145 |
+
out, hidden = self.gru(x, hidden)
|
| 146 |
+
out = self.norm(out.squeeze(1))
|
| 147 |
+
logits = self.out(out)
|
| 148 |
return logits, hidden
|
| 149 |
|
| 150 |
|