blazingbunny commited on
Commit
80f8209
·
verified ·
1 Parent(s): 9d4445b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -17
app.py CHANGED
@@ -1,27 +1,19 @@
1
  import streamlit as st
2
- import torch
3
- from transformers import BertModel, BertTokenizer
4
 
5
- # Load pre-trained BERT model and tokenizer
6
  @st.cache_resource
7
- def load_bert():
8
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
9
- model = BertModel.from_pretrained('bert-base-uncased')
10
- return tokenizer, model
11
 
12
- tokenizer, model = load_bert()
13
 
14
  def calculate_similarity(word1, word2):
15
- # Tokenize and get embeddings
16
- input_ids1 = torch.tensor([tokenizer.encode(word1, add_special_tokens=True)])
17
- input_ids2 = torch.tensor([tokenizer.encode(word2, add_special_tokens=True)])
18
-
19
- with torch.no_grad():
20
- embeddings1 = model(input_ids1)[0][0, 0, :]
21
- embeddings2 = model(input_ids2)[0][0, 0, :]
22
-
23
  cos_sim = torch.nn.functional.cosine_similarity(embeddings1, embeddings2, dim=0)
24
- return cos_sim.item() # Convert tensor to a float
25
 
26
  # Streamlit interface
27
  st.title("Word Similarity Checker")
 
1
  import streamlit as st
2
+ from sentence_transformers import SentenceTransformer
 
3
 
4
+ # Load SBERT model (choose a suitable model from https://www.sbert.net/docs/pretrained_models.html)
5
  @st.cache_resource
6
+ def load_sbert():
7
+ model = SentenceTransformer('all-MiniLM-L6-v2') # Example model
8
+ return model
 
9
 
10
+ model = load_sbert()
11
 
12
  def calculate_similarity(word1, word2):
13
+ embeddings1 = model.encode(word1)
14
+ embeddings2 = model.encode(word2)
 
 
 
 
 
 
15
  cos_sim = torch.nn.functional.cosine_similarity(embeddings1, embeddings2, dim=0)
16
+ return cos_sim.item()
17
 
18
  # Streamlit interface
19
  st.title("Word Similarity Checker")