Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer, util
|
| 4 |
from datasets import load_dataset
|
| 5 |
|
|
@@ -8,14 +11,58 @@ from datasets import load_dataset
|
|
| 8 |
def load_model():
|
| 9 |
return SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2')
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
model = load_model()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if 'words' not in st.session_state:
|
| 17 |
st.session_state['words'] = []
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
st.write('Try to guess a secret word by semantic similarity')
|
| 20 |
|
| 21 |
word = st.text_input("Input a word")
|
|
@@ -25,8 +72,21 @@ used_words = [w for w, s in st.session_state['words']]
|
|
| 25 |
if st.button("Guess") or word:
|
| 26 |
if word not in used_words:
|
| 27 |
word_embedding = model.encode(word)
|
| 28 |
-
similarity = util.pytorch_cos_sim(
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
words_df = pd.DataFrame(
|
| 32 |
st.session_state['words'],
|
|
@@ -35,11 +95,5 @@ words_df = pd.DataFrame(
|
|
| 35 |
st.dataframe(words_df)
|
| 36 |
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def load_words_dataset():
|
| 41 |
-
dataset = load_dataset("marksverdhei/wordnet-definitions-en-2021", split="train")
|
| 42 |
-
return dataset["Word"]
|
| 43 |
-
|
| 44 |
-
all_words = load_words_dataset()
|
| 45 |
-
st.write(all_words)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import plotly.express as px
|
| 3 |
import pandas as pd
|
| 4 |
+
import random
|
| 5 |
+
from umap import UMAP
|
| 6 |
from sentence_transformers import SentenceTransformer, util
|
| 7 |
from datasets import load_dataset
|
| 8 |
|
|
|
|
| 11 |
def load_model():
|
| 12 |
return SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2')
|
| 13 |
|
| 14 |
+
|
| 15 |
+
@st.cache_data
|
| 16 |
+
def load_words_dataset():
|
| 17 |
+
dataset = load_dataset("marksverdhei/wordnet-definitions-en-2021", split="train")
|
| 18 |
+
return dataset["Word"]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def prepare_umap():
|
| 23 |
+
all_enc = model.encode(all_words)
|
| 24 |
+
umap_3d = UMAP(n_components=3, init='random', random_state=0)
|
| 25 |
+
proj_3d = umap_3d.fit_transform(all_enc)
|
| 26 |
+
return umap_3d
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
all_words = load_words_dataset()
|
| 30 |
+
|
| 31 |
model = load_model()
|
| 32 |
|
| 33 |
+
umap_3d = prepare_umap()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
secret_word = random.choice(all_words)
|
| 37 |
+
secret_embedding = model.encode(secret_word)
|
| 38 |
+
|
| 39 |
|
| 40 |
if 'words' not in st.session_state:
|
| 41 |
st.session_state['words'] = []
|
| 42 |
|
| 43 |
+
if 'words_umap_df' not in st.session_state:
|
| 44 |
+
st.session_state['words_umap_df'] = pd.DataFrame({
|
| 45 |
+
"x": [],
|
| 46 |
+
"y": [],
|
| 47 |
+
"z": [],
|
| 48 |
+
"similarity": [],
|
| 49 |
+
"s": [],
|
| 50 |
+
"l": [],
|
| 51 |
+
})
|
| 52 |
+
words_umap_df = st.session_state['words_umap_df']
|
| 53 |
+
|
| 54 |
+
secret_embedding_3d = umap_3d.transform([secret_embedding])[0]
|
| 55 |
+
words_umap_df.loc[len(words_umap_df)] = {
|
| 56 |
+
"x": secret_embedding_3d[0],
|
| 57 |
+
"y": secret_embedding_3d[1],
|
| 58 |
+
"z": secret_embedding_3d[2],
|
| 59 |
+
"similarity": 1,
|
| 60 |
+
"s": 10,
|
| 61 |
+
"l": "Secret word"
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
words_umap_df = st.session_state['words_umap_df']
|
| 65 |
+
|
| 66 |
st.write('Try to guess a secret word by semantic similarity')
|
| 67 |
|
| 68 |
word = st.text_input("Input a word")
|
|
|
|
| 72 |
if st.button("Guess") or word:
|
| 73 |
if word not in used_words:
|
| 74 |
word_embedding = model.encode(word)
|
| 75 |
+
similarity = util.pytorch_cos_sim(
|
| 76 |
+
secret_embedding,
|
| 77 |
+
word_embedding
|
| 78 |
+
).cpu().numpy()[0][0]
|
| 79 |
+
st.session_state['words'].append((str(word), similarity))
|
| 80 |
+
|
| 81 |
+
pt = umap_3d.transform([word_embedding])[0]
|
| 82 |
+
words_umap_df.loc[len(words_umap_df)] = {
|
| 83 |
+
"x": pt[0],
|
| 84 |
+
"y": pt[1],
|
| 85 |
+
"z": pt[2],
|
| 86 |
+
"similarity": similarity,
|
| 87 |
+
"s": 3,
|
| 88 |
+
"l": str(word)
|
| 89 |
+
}
|
| 90 |
|
| 91 |
words_df = pd.DataFrame(
|
| 92 |
st.session_state['words'],
|
|
|
|
| 95 |
st.dataframe(words_df)
|
| 96 |
|
| 97 |
|
| 98 |
+
fig_3d = px.scatter_3d(word_points, x="x", y="y", z="z", color="similarity", hover_name="l", hover_data={"x": False, "y": False, "z": False, "s": False}, size="s", size_max=10, range_color=(0,1))
|
| 99 |
+
st.plotly_chart(fig_3d, theme="streamlit", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|