converted nearest neighbours from gradio to streamlit
Browse files- app.py +19 -3
- word2vec.py +3 -10
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from streamlit_option_menu import option_menu
|
3 |
from word2vec import *
|
|
|
4 |
|
5 |
st.set_page_config(page_title="Ancient Greek Word2Vec", layout="centered")
|
6 |
|
@@ -17,14 +18,29 @@ if active_tab == "Nearest neighbours":
|
|
17 |
word = st.text_input("Enter a word", placeholder="ἀνήρ")
|
18 |
|
19 |
with col2:
|
20 |
-
time_slice = st.
|
21 |
|
22 |
-
st.slider("Number of neighbours", 1, 50, 15)
|
23 |
|
24 |
nearest_neighbours_button = st.button("Find nearest neighbours")
|
25 |
|
|
|
26 |
if nearest_neighbours_button:
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
|
30 |
# Cosine similarity tab
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit_option_menu import option_menu
|
3 |
from word2vec import *
|
4 |
+
import pandas as pd
|
5 |
|
6 |
st.set_page_config(page_title="Ancient Greek Word2Vec", layout="centered")
|
7 |
|
|
|
18 |
word = st.text_input("Enter a word", placeholder="ἀνήρ")
|
19 |
|
20 |
with col2:
|
21 |
+
time_slice = st.selectbox("Time slice", ["Archaic", "Classical", "Hellenistic", "Early Roman", "Late Roman"])
|
22 |
|
23 |
+
n = st.slider("Number of neighbours", 1, 50, 15)
|
24 |
|
25 |
nearest_neighbours_button = st.button("Find nearest neighbours")
|
26 |
|
27 |
+
# If the button to calculate nearest neighbours is clicked
|
28 |
if nearest_neighbours_button:
|
29 |
+
|
30 |
+
# Rewrite timeslices to model names: Archaic -> archaic_cbow
|
31 |
+
time_slice = time_slice.lower() + "_cbow"
|
32 |
+
st.write(time_slice)
|
33 |
+
|
34 |
+
# Check if all fields are filled in
|
35 |
+
if validate_nearest_neighbours(word, time_slice, n) == False:
|
36 |
+
st.error('Please fill in all fields')
|
37 |
+
else:
|
38 |
+
nearest_neighbours = get_nearest_neighbours(word, time_slice, n)
|
39 |
+
df = pd.DataFrame(nearest_neighbours, columns=["Word", "Time slice", "Similarity"])
|
40 |
+
st.table(df)
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
|
45 |
|
46 |
# Cosine similarity tab
|
word2vec.py
CHANGED
@@ -124,7 +124,7 @@ def validate_nearest_neighbours(word, time_slice_model, n):
|
|
124 |
'''
|
125 |
Validate the input of the nearest neighbours function
|
126 |
'''
|
127 |
-
if word == '' or time_slice_model ==
|
128 |
return False
|
129 |
return True
|
130 |
|
@@ -140,15 +140,8 @@ def get_nearest_neighbours(word, time_slice_model, n=10, models=load_all_models(
|
|
140 |
|
141 |
Return: list of tuples with the word, the time slice and
|
142 |
the cosine similarity of the nearest neighbours
|
143 |
-
'''
|
144 |
-
|
145 |
-
# Check if all parameters are set
|
146 |
-
valid = validate_nearest_neighbours(word, time_slice_model, n)
|
147 |
-
if valid == False:
|
148 |
-
return [['Error: not all parameters are set', '', '']]
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
time_slice_model = load_word2vec_model(f'models/{time_slice_model}.model')
|
153 |
vector_1 = get_word_vector(time_slice_model, word)
|
154 |
nearest_neighbours = []
|
|
|
124 |
'''
|
125 |
Validate the input of the nearest neighbours function
|
126 |
'''
|
127 |
+
if word == '' or time_slice_model == [] or n == '':
|
128 |
return False
|
129 |
return True
|
130 |
|
|
|
140 |
|
141 |
Return: list of tuples with the word, the time slice and
|
142 |
the cosine similarity of the nearest neighbours
|
143 |
+
'''
|
144 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
time_slice_model = load_word2vec_model(f'models/{time_slice_model}.model')
|
146 |
vector_1 = get_word_vector(time_slice_model, word)
|
147 |
nearest_neighbours = []
|