Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,19 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
from transformers import BertModel, BertTokenizer
|
4 |
|
5 |
-
# Load
|
6 |
@st.cache_resource
|
7 |
-
def
|
8 |
-
|
9 |
-
model
|
10 |
-
return tokenizer, model
|
11 |
|
12 |
-
|
13 |
|
14 |
def calculate_similarity(word1, word2):
|
15 |
-
|
16 |
-
|
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()
|
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")
|