|
import numpy as np |
|
import pandas as pd |
|
import random |
|
from gensim.models import Word2Vec |
|
import gradio as gr |
|
from sklearn.decomposition import PCA |
|
import plotly.graph_objects as go |
|
|
|
|
|
|
|
def train_word2vec(sentences): |
|
|
|
model = Word2Vec(sentences, vector_size=50, window=4, min_count=1, sg=0, epochs=100) |
|
return model |
|
|
|
def apply_pca(word_vectors): |
|
pca = PCA(n_components=3) |
|
return pca.fit_transform(word_vectors) |
|
|
|
|
|
def get_unique(model): |
|
vocablist1=list(model.wv.index_to_key) |
|
vocablist =[] |
|
for i in vocablist1: |
|
vocablist.append(i) |
|
return vocablist |
|
|
|
def train_model(sentence): |
|
|
|
sentences=sentence |
|
|
|
|
|
model = train_word2vec(sentences) |
|
unique_words = get_unique(model) |
|
|
|
return model, unique_words |
|
|
|
def process_model(target_word): |
|
target_word =target_word.lower() |
|
|
|
|
|
model = Word2Vec.load("word2vec.model") |
|
unique_words = get_unique(model) |
|
|
|
|
|
word_vectors = np.array([model.wv[word] for word in unique_words]) |
|
|
|
|
|
word_vectors_3d = apply_pca(word_vectors) |
|
|
|
|
|
colors = ['rgba(255, 255, 255, 0.15)' if word != target_word else 'rgba(255, 20, 147, 0.9)' for word in unique_words] |
|
|
|
|
|
if target_word in model.wv: |
|
similar_words = model.wv.most_similar(target_word, topn=10) |
|
similar_word_indices = [unique_words.index(word) for word, _ in similar_words] |
|
for idx in similar_word_indices: |
|
colors[idx] = 'rgba(255, 165, 0, 1)' |
|
|
|
|
|
if target_word in model.wv: |
|
all_words = model.wv.index_to_key |
|
dissimilar_words = sorted( |
|
[(word, model.wv.similarity(target_word, word)) for word in all_words if word != target_word], |
|
key=lambda x: x[1] |
|
)[:10] |
|
|
|
dissimilar_word_indices = [unique_words.index(word) for word, _ in dissimilar_words] |
|
for idx in dissimilar_word_indices: |
|
colors[idx] = 'rgba(138, 43, 226, 0.8)' |
|
|
|
|
|
|
|
fig = go.Figure(data=[go.Scatter3d( |
|
x=word_vectors_3d[:, 0], |
|
y=word_vectors_3d[:, 1], |
|
z=word_vectors_3d[:, 2], |
|
mode='markers+text', |
|
text=unique_words, |
|
textposition="top center", |
|
marker=dict( |
|
size=4, |
|
color=colors, |
|
) |
|
)]) |
|
|
|
fig.update_layout( |
|
title="Word Embeddings 3D Visualization", |
|
scene=dict( |
|
xaxis_title="X", |
|
yaxis_title="Y", |
|
zaxis_title="Z" |
|
), |
|
width=1100, |
|
height=900 |
|
) |
|
|
|
|
|
similar_words_text = "" |
|
if target_word in model.wv: |
|
similar_words_text = "\n".join([f"{word}: {score:.4f}" for word, score in similar_words]) |
|
|
|
dissimlar_words_Text="" |
|
if target_word in model.wv: |
|
dissimilar_words_text = "\n".join([f"{word}: {score:.4f}" for word, score in dissimilar_words]) |
|
|
|
return fig, similar_words_text, dissimilar_words_text |
|
|
|
|
|
|
|
|
|
with gr.Blocks(css=".plot-box {width: 70%; height: 500px;}") as iface: |
|
gr.Markdown("# Word Embedding 3D ์๊ฐํ") |
|
gr.Markdown("<Inside Out 2> ๋จ์ด ์๋ฏธ ์ง๋(์๋ฒ ๋ฉ ๋ฒกํฐ) 3D ์๊ฐํ ๋๊ตฌ") |
|
|
|
with gr.Row(): |
|
|
|
with gr.Column(): |
|
word_input = gr.Textbox(label="**๋จ์ด ์
๋ ฅ**", elem_id="input-box", placeholder="ex. emotion, puberty, hockey, friend, anxiety, memory, ...", lines=1) |
|
submit_btn = gr.Button("์ ์ถ", elem_id="submit-btn") |
|
bulletin = gr.Textbox(label="์ฌ์ฉ๋ฒ ์๋ด", interactive=False, lines=4, value="1. ์์ค์ ๋์จ ๋จ์ด๋ฅผ ์
๋ ฅํ๊ณ [์ ์ถ] ๋ฒํผ์ด๋ [Enter]๋ฅผ ๋๋ฅด์ธ์ \n2. ์
๋ ฅ ๋จ์ด๋ ๋นจ๊ฐ์, ๊ฐ๊น์ด ๋จ์ด๋ค์ ์ฃผํฉ์, ๋จผ ๋จ์ด๋ค์ ๋ณด๋ผ์์ผ๋ก ๊ฐ์กฐ๋ฉ๋๋ค. \n3. <Error>๊ฐ ๋ํ๋๋ ๊ฒฝ์ฐ, ๋ค๋ฅธ ๋จ์ด๋ฅผ ์
๋ ฅํด๋ณด์ธ์.\n4. ๋ง์ฐ์ค ๋๋๊ทธ ๋ฐ ์คํฌ๋กค์ ํ์ฉํ์ฌ 3D ํ๋ฉด์ ์ดํด๋ณด์ธ์. \n5. ๋จ์ด ์
๋ ฅ์ฐฝ์ ๋ค๋ฅธ ๋จ์ด๋ค๋ ์
๋ ฅํด๋ณด์ธ์.") |
|
|
|
with gr.Row(): |
|
|
|
plot_output = gr.Plot(label="Word Embedding 3D ์๊ฐํ", elem_id="plot-box") |
|
|
|
with gr.Column(scale=0.3): |
|
similar_words_output = gr.Textbox(label="๊ฐ์ฅ ๊ฐ๊น์ด ๋จ์ด 10๊ฐ", interactive=False, lines=5) |
|
dissimilar_words_output = gr.Textbox(label="๊ฐ์ฅ ๋จผ ๋จ์ด 10๊ฐ", interactive=False, lines=5) |
|
gr.Image(value="https://compote.slate.com/images/8324cd2e-21f5-4b20-84d5-f08ece97ac38.jpeg?crop=1560%2C1040%2Cx0%2Cy0&width=1280", label="URL ์ด๋ฏธ์ง", interactive=False) |
|
|
|
submit_btn.click( |
|
fn=process_text, |
|
|
|
inputs=[word_input], |
|
outputs=[plot_output, similar_words_output, dissimilar_words_output], |
|
|
|
) |
|
|
|
|
|
word_input.submit( |
|
fn=process_text, |
|
|
|
inputs=[word_input], |
|
outputs=[plot_output, similar_words_output, dissimilar_words_output], |
|
preprocess=lambda word: word.lower() if word else "" |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |