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=3): 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}')