Alberthu233 commited on
Commit
2db744e
1 Parent(s): b03ebf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -9,10 +9,9 @@ st.set_page_config(page_title='Sentence Similarity Demo')
9
  st.title('Sentence Similarity Demo')
10
 
11
  # Input sentences
12
- sentence1 = st.text_input('Enter the first sentence:', 'This is an example sentence')
13
- sentence2 = st.text_input('Enter the second sentence:', 'Each sentence is converted')
14
 
15
- # Load the Sentence Transformer model
16
  @st.cache_resource
17
  def load_model():
18
  return SentenceTransformer('sentence-transformers/sentence-t5-base')
@@ -22,8 +21,12 @@ model = load_model()
22
  # Calculate embeddings
23
  embeddings = model.encode([sentence1, sentence2])
24
 
25
- # Calculate cosine similarity
26
- similarity = cosine_similarity([embeddings[0]], [embeddings[1]])[0][0]
 
 
 
 
27
 
28
  # Display the result
29
- st.write(f'Cosine Similarity: {similarity:.4f}')
 
9
  st.title('Sentence Similarity Demo')
10
 
11
  # Input sentences
12
+ sentence1 = st.text_input('Enter the first sentence:', 'Convert this into a sea shanty')
13
+ sentence2 = st.text_input('Enter the second sentence:', 'Improve this text by making it a shanty.')
14
 
 
15
  @st.cache_resource
16
  def load_model():
17
  return SentenceTransformer('sentence-transformers/sentence-t5-base')
 
21
  # Calculate embeddings
22
  embeddings = model.encode([sentence1, sentence2])
23
 
24
+ # Calculate Sharpened Cosine Similarity (SCS)
25
+ def sharpened_cosine_similarity(s, k, q=1e-6, p=2):
26
+ cosine_sim = np.dot(s, k) / (np.linalg.norm(s) * np.linalg.norm(k))
27
+ return np.sign(cosine_sim) * np.power(np.abs(cosine_sim) / (np.linalg.norm(s) + q), p)
28
+
29
+ similarity = sharpened_cosine_similarity(embeddings[0], embeddings[1])
30
 
31
  # Display the result
32
+ st.write(f'Sharpened Cosine Similarity: {similarity:.4f}')