blazingbunny commited on
Commit
cb42075
·
verified ·
1 Parent(s): 28f3fdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -12,18 +12,18 @@ def load_bert():
12
 
13
  tokenizer, model = load_bert()
14
 
15
- def calculate_similarity(word1, word2):
16
- def calculate_similarity(word1, word2):
17
- # Tokenize and get embeddings
18
- input_ids1 = torch.tensor([tokenizer.encode(word1, add_special_tokens=True)])
19
- input_ids2 = torch.tensor([tokenizer.encode(word2, add_special_tokens=True)])
20
-
21
- with torch.no_grad():
22
- embeddings1 = model(input_ids1)[0][0, 0, :]
23
- embeddings2 = model(input_ids2)[0][0, 0, :]
24
-
25
- cos_sim = torch.nn.functional.cosine_similarity(embeddings1, embeddings2, dim=0)
26
- return cos_sim.item() # Convert tensor to a float
27
 
28
  # Streamlit interface
29
  st.title("Word Similarity Checker")
 
12
 
13
  tokenizer, model = load_bert()
14
 
15
+ def calculate_similarity(word1, word2):
16
+ def calculate_similarity(word1, word2):
17
+ # Tokenize and get embeddings
18
+ input_ids1 = torch.tensor([tokenizer.encode(word1, add_special_tokens=True)])
19
+ input_ids2 = torch.tensor([tokenizer.encode(word2, add_special_tokens=True)])
20
+
21
+ with torch.no_grad():
22
+ embeddings1 = model(input_ids1)[0][0, 0, :]
23
+ embeddings2 = model(input_ids2)[0][0, 0, :]
24
+
25
+ cos_sim = torch.nn.functional.cosine_similarity(embeddings1, embeddings2, dim=0)
26
+ return cos_sim.item() # Convert tensor to a float
27
 
28
  # Streamlit interface
29
  st.title("Word Similarity Checker")