Alberthu233 commited on
Commit
ec1e4e7
·
verified ·
1 Parent(s): 7de384e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from sentence_transformers import SentenceTransformer
3
+ from sklearn.metrics.pairwise import cosine_similarity
4
+
5
+ # Set page title
6
+ st.set_page_config(page_title='Sentence Similarity Demo')
7
+
8
+ # Create a title for the app
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')
19
+
20
+ model = load_model()
21
+
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}')