Spaces:
Build error
Build error
Commit
Β·
0bad089
1
Parent(s):
59d79a5
Update app.py
Browse files
app.py
CHANGED
@@ -6,20 +6,14 @@ from transformers import AutoTokenizer
|
|
6 |
|
7 |
|
8 |
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased")
|
9 |
-
|
10 |
unnormalized_input_embeddings = np.load("unnormalized_bert_input_embeddings.npy")
|
11 |
|
12 |
-
index_L2 = IndexFlatL2(
|
13 |
-
index_L2.add(
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
index_L2_unnormalized = IndexFlatL2(unnormalized_input_embeddings.shape[-1])
|
19 |
-
index_L2_unnormalized.add(unnormalized_input_embeddings)
|
20 |
-
|
21 |
-
index_IP_unnormalized = IndexFlatIP(unnormalized_input_embeddings.shape[-1])
|
22 |
-
index_IP_unnormalized.add(unnormalized_input_embeddings)
|
23 |
|
24 |
|
25 |
vocab = {v:k for k,v in tokenizer.vocab.items()}
|
@@ -33,7 +27,7 @@ def get_first_subword(word):
|
|
33 |
|
34 |
def search(token_to_lookup, num_neighbors):
|
35 |
i = get_first_subword(token_to_lookup)
|
36 |
-
_ , I =
|
37 |
hits = lookup_table.take(I[0])
|
38 |
results = hits.values[1:]
|
39 |
results = [r for r in results if not "[unused" in r]
|
|
|
6 |
|
7 |
|
8 |
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased")
|
9 |
+
normalized_input_embeddings = np.load("normalized_bert_input_embeddings.npy")
|
10 |
unnormalized_input_embeddings = np.load("unnormalized_bert_input_embeddings.npy")
|
11 |
|
12 |
+
index_L2 = IndexFlatL2(unnormalized_input_embeddings.shape[-1])
|
13 |
+
index_L2.add(unnormalized_input_embeddings)
|
14 |
|
15 |
+
index_IP_normalized = IndexFlatIP(normalized_input_embeddings.shape[-1])
|
16 |
+
index_IP_normalized.add(normalized_input_embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
vocab = {v:k for k,v in tokenizer.vocab.items()}
|
|
|
27 |
|
28 |
def search(token_to_lookup, num_neighbors):
|
29 |
i = get_first_subword(token_to_lookup)
|
30 |
+
_ , I = index_IP_normalized.search(normalized_input_embeddings[i:i+1], num_neighbors)
|
31 |
hits = lookup_table.take(I[0])
|
32 |
results = hits.values[1:]
|
33 |
results = [r for r in results if not "[unused" in r]
|