File size: 1,257 Bytes
f3d628e
fcecd40
80f8209
f3d628e
80f8209
28f3fdc
80f8209
 
 
f3d628e
80f8209
f3d628e
aa90abd
9d4445b
80f8209
 
aa90abd
 
 
 
 
9d4445b
80f8209
f3d628e
 
 
28f3fdc
 
f3d628e
28f3fdc
 
 
 
 
 
f3d628e
28f3fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import streamlit as st
import torch
from sentence_transformers import SentenceTransformer

# Load SBERT model (choose a suitable model from https://www.sbert.net/docs/pretrained_models.html)
@st.cache_resource
def load_sbert():
    model = SentenceTransformer('all-MiniLM-L6-v2')  # Example model
    return model

model = load_sbert()


def calculate_similarity(word1, word2):
    embeddings1 = model.encode(word1)
    embeddings2 = model.encode(word2)

    # Convert NumPy arrays to tensors
    embeddings1 = torch.tensor(embeddings1)
    embeddings2 = torch.tensor(embeddings2)

    cos_sim = torch.nn.functional.cosine_similarity(embeddings1, embeddings2, dim=0)
    return cos_sim.item()
# Streamlit interface
st.title("Word Similarity Checker")

reference_word = st.text_input("Enter the reference word:")
word_list = st.text_area("Enter a list of words (one word per line):")

if st.button("Analyze"):
    if reference_word and word_list:
        words = word_list.splitlines()
        for word in words:
            similarity = calculate_similarity(reference_word, word)
            st.write(f"Similarity between '{reference_word}' and '{word}': {similarity:.4f}")
    else:
        st.warning("Please enter a reference word and a list of words.")