Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import numpy.linalg as la
|
4 |
+
import pickle
|
5 |
+
#import streamlit_analytics
|
6 |
+
|
7 |
+
|
8 |
+
# Compute Cosine Similarity
|
9 |
+
def cosine_similarity(x,y):
|
10 |
+
|
11 |
+
x_arr = np.array(x)
|
12 |
+
y_arr = np.array(y)
|
13 |
+
return np.dot(x_arr,y_arr)/(la.norm(x_arr)*la.norm(y_arr))
|
14 |
+
|
15 |
+
|
16 |
+
# Function to Load Glove Embeddings
|
17 |
+
def load_glove_embeddings(glove_path="Data/embeddings.pkl"):
|
18 |
+
|
19 |
+
with open(glove_path,"rb") as f:
|
20 |
+
embeddings_dict = pickle.load(f)
|
21 |
+
|
22 |
+
return embeddings_dict
|
23 |
+
|
24 |
+
# Get Averaged Glove Embedding of a sentence
|
25 |
+
def averaged_glove_embeddings(sentence, embeddings_dict):
|
26 |
+
words = sentence.split(" ")
|
27 |
+
glove_embedding = np.zeros(50)
|
28 |
+
count_words = 0
|
29 |
+
for word in words:
|
30 |
+
if word in embeddings_dict:
|
31 |
+
glove_embedding += embeddings_dict[word]
|
32 |
+
count_words += 1
|
33 |
+
|
34 |
+
return glove_embedding/max(count_words,1)
|
35 |
+
|
36 |
+
# Load glove embeddings
|
37 |
+
glove_embeddings = load_glove_embeddings()
|
38 |
+
|
39 |
+
# Gold standard words to search from
|
40 |
+
gold_words = ["flower","mountain","tree","car","building"]
|
41 |
+
|
42 |
+
# Text Search
|
43 |
+
#with streamlit_analytics.track():
|
44 |
+
st.title("Search Based Retrieval Demo")
|
45 |
+
st.subheader("Pass in an input word or even a sentence (e.g. jasmine or mount adams)")
|
46 |
+
text_search = st.text_input("", value="")
|
47 |
+
|
48 |
+
|
49 |
+
# Find closest word to an input word
|
50 |
+
if text_search:
|
51 |
+
input_embedding = averaged_glove_embeddings(text_search, glove_embeddings)
|
52 |
+
cosine_sim = {}
|
53 |
+
for index in range(len(gold_words)):
|
54 |
+
cosine_sim[index] = cosine_similarity(input_embedding, glove_embeddings[gold_words[index]])
|
55 |
+
|
56 |
+
print(cosine_sim)
|
57 |
+
sorted_cosine_sim = sorted(cosine_sim.items(), key = lambda x: x[1], reverse=True)
|
58 |
+
|
59 |
+
st.write("(My search uses glove embeddings)")
|
60 |
+
st.write("Closest word I have between flower, mountain, tree, car and building for your input is: ")
|
61 |
+
st.subheader(gold_words[sorted_cosine_sim[0][0]] )
|
62 |
+
st.write("")
|
63 |
+
st.write("Demo developed by Dr. Karthik Mohan")
|
64 |
+
|