-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_app.py
122 lines (101 loc) · 3.18 KB
/
run_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from gensim.models import KeyedVectors
from gensim.scripts.glove2word2vec import glove2word2vec
from gensim.test.utils import datapath, get_tmpfile
import pandas as pd
import os
import numpy as np
from figures import *
import streamlit as st
# set configs
st.set_option('server.headless', 'true')
st.set_option('browser.serverAddress', '0.0.0.0')
st.set_option('browser.gatherUsageStats', 'false')
# ---------------------
# Read the model
# ---------------------
@st.cache(ignore_hash=True)
def load_model(path):
glove_file = datapath(path)
tmp_file = get_tmpfile("glove_word2vec.txt")
_ = glove2word2vec(glove_file, tmp_file)
model = KeyedVectors.load_word2vec_format(
tmp_file,
binary=False
)
return model
# ---------------------
# Common part
# ---------------------
st.sidebar.title('GloVe Twitter')
st.sidebar.markdown("""
GloVe is an unsupervised learning algorithm for obtaining vector representations for words. Pretrained on
2 billion tweets with vocabulary size of 1.2 million. Download from [Stanford NLP](http://nlp.stanford.edu/data/glove.twitter.27B.zip).
Jeffrey Pennington, Richard Socher, and Christopher D. Manning. 2014. *GloVe: Global Vectors for Word Representation*.
""")
model_type = st.sidebar.selectbox(
'Choose the model',
('25d', '50d', '100d', '200d'),
index=3
)
# load the model
dir_path = os.path.dirname(os.path.realpath(__file__))
model = load_model(dir_path + '/glove.twitter.27B.%s.txt' % model_type)
# -------------------------
# Choose option
# -------------------------
option = st.sidebar.selectbox(
'Task type',
('Compare weights','Most similar'),
index=1
)
if option == 'Most similar':
st.title('Most similar')
word = st.text_input(
'Type your word',
value='dog'
)
title = 'Most similar to %s' % word.upper()
# run gensim
try:
ret = model.wv.most_similar(
positive=word,
topn=10
)
except Exception as e:
ret = None
st.markdown('Ups! The word **%s** is not in dictionary.' % word)
if ret is not None:
# convert to pandas
data = pd.DataFrame(ret, columns=['word','distance'])
chart = render_most_similar(data, title)
st.altair_chart(chart)
elif option == 'Compare weights':
st.title('Compare weights')
word1 = st.text_input(
'First word',
'dog'
)
word2 = st.text_input(
'Second word',
'cat'
)
try:
vec1 = model.wv.word_vec(word1, use_norm=True)
vec2 = model.wv.word_vec(word2, use_norm=True)
data = pd.DataFrame(
{
'word': [word1] * len(vec1) + [word2] * len(vec2),
'x': list(range(0, len(vec1))) + list(range(0, len(vec2))),
'weight': np.concatenate((vec1, vec2), axis=0)
}
)
chart = render_compare(data)
st.altair_chart(chart)
data = pd.DataFrame({
'index': list( range(0,len(vec1)) ),
'weight': np.abs(vec1-vec2)
})
chart = render_absolute_compare(data)
st.altair_chart(chart)
except:
st.markdown('Ups! One of the words is not present in dictionary.')