Spaces:
Running
Running
Update apps/similarity.py
Browse files- apps/similarity.py +24 -1
apps/similarity.py
CHANGED
@@ -1,4 +1,27 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
def app():
|
4 |
-
st.title("Text Similarity")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
+
import numpy as np
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
|
6 |
def app():
|
7 |
+
st.title("Text Similarity")
|
8 |
+
|
9 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
10 |
+
|
11 |
+
with st.container():
|
12 |
+
col1, col2 = st.columns(2)
|
13 |
+
with col1:
|
14 |
+
word_to_embed1 = st.text_input("Text 1", value="",)
|
15 |
+
with col2:
|
16 |
+
word_to_embed2 = st.text_input("Text 2", value="",)
|
17 |
+
|
18 |
+
if st.button("Embed"):
|
19 |
+
with st.spinner("Embedding comparing your inputs"):
|
20 |
+
|
21 |
+
document = [word_to_embed1 ,word_to_embed2]
|
22 |
+
#Encode paragraphs
|
23 |
+
document_embeddings = model.encode(document, show_progress_bar=False)
|
24 |
+
#Compute cosine similarity between labels sentences and paragraphs
|
25 |
+
similarity_matrix = cosine_similarity(label_embeddings, document_embeddings)
|
26 |
+
|
27 |
+
st.write("Text similarity:" similarity_matrix)
|