File size: 1,123 Bytes
ec1e4e7
94c0025
ec1e4e7
 
 
 
 
 
 
 
 
 
2db744e
 
ec1e4e7
 
 
 
 
 
 
 
 
 
2db744e
 
 
 
 
 
ec1e4e7
 
2db744e
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
import streamlit as st
import numpy as np
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity

# Set page title
st.set_page_config(page_title='Sentence Similarity Demo')

# Create a title for the app
st.title('Sentence Similarity Demo')

# Input sentences
sentence1 = st.text_input('Enter the first sentence:', 'Convert this into a sea shanty')
sentence2 = st.text_input('Enter the second sentence:', 'Improve this text by making it a shanty.')

@st.cache_resource
def load_model():
    return SentenceTransformer('sentence-transformers/sentence-t5-base')

model = load_model()

# Calculate embeddings
embeddings = model.encode([sentence1, sentence2])

# Calculate Sharpened Cosine Similarity (SCS)
def sharpened_cosine_similarity(s, k, q=1e-6, p=2):
    cosine_sim = np.dot(s, k) / (np.linalg.norm(s) * np.linalg.norm(k))
    return np.sign(cosine_sim) * np.power(np.abs(cosine_sim) / (np.linalg.norm(s) + q), p)

similarity = sharpened_cosine_similarity(embeddings[0], embeddings[1])

# Display the result
st.write(f'Sharpened Cosine Similarity: {similarity:.4f}')