Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import gensim.downloader as api
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from sklearn.manifold import TSNE
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import plotly.express as px
|
| 8 |
+
|
| 9 |
+
# Load pre-trained Word2Vec model
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
return api.load("word2vec-google-news-300")
|
| 13 |
+
|
| 14 |
+
model = load_model()
|
| 15 |
+
|
| 16 |
+
# Streamlit UI
|
| 17 |
+
st.title("🔍 Word Embedding Visualization")
|
| 18 |
+
st.write("Enter words to visualize their embeddings using t-SNE.")
|
| 19 |
+
|
| 20 |
+
# User input
|
| 21 |
+
words = st.text_input("Enter words (comma-separated)", "king, queen, man, woman, dog, cat")
|
| 22 |
+
|
| 23 |
+
# Process input
|
| 24 |
+
words = [word.strip() for word in words.split(",") if word.strip() in model]
|
| 25 |
+
|
| 26 |
+
if len(words) < 2:
|
| 27 |
+
st.warning("Please enter at least two valid words from the Word2Vec model.")
|
| 28 |
+
else:
|
| 29 |
+
# Get embeddings
|
| 30 |
+
vectors = np.array([model[word] for word in words])
|
| 31 |
+
|
| 32 |
+
# Reduce to 2D using t-SNE
|
| 33 |
+
tsne = TSNE(n_components=2, perplexity=5, random_state=42)
|
| 34 |
+
vectors_2d = tsne.fit_transform(vectors)
|
| 35 |
+
|
| 36 |
+
# Plot using Matplotlib
|
| 37 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
| 38 |
+
ax.scatter(vectors_2d[:, 0], vectors_2d[:, 1])
|
| 39 |
+
|
| 40 |
+
for i, word in enumerate(words):
|
| 41 |
+
ax.text(vectors_2d[i, 0] + 0.01, vectors_2d[i, 1] + 0.01, word, fontsize=12)
|
| 42 |
+
|
| 43 |
+
st.pyplot(fig)
|
| 44 |
+
|
| 45 |
+
# 3D Visualization with Plotly
|
| 46 |
+
tsne_3d = TSNE(n_components=3, perplexity=5, random_state=42)
|
| 47 |
+
vectors_3d = tsne_3d.fit_transform(vectors)
|
| 48 |
+
|
| 49 |
+
df = pd.DataFrame(vectors_3d, columns=["x", "y", "z"])
|
| 50 |
+
df["word"] = words
|
| 51 |
+
|
| 52 |
+
fig3d = px.scatter_3d(df, x="x", y="y", z="z", text="word", title="3D t-SNE Word Embeddings")
|
| 53 |
+
st.plotly_chart(fig3d)
|