File size: 7,391 Bytes
770e786 aca3b1d 9fcb29b 770e786 aca3b1d 9fcb29b 1ed0340 9fcb29b aca3b1d 770e786 9fcb29b aca3b1d 9fcb29b aca3b1d 9fcb29b aca3b1d 770e786 aca3b1d ff648fb 9fcb29b ff648fb 1ed0340 ff648fb 9fcb29b 1432ce2 9fcb29b 7112f5e 770e786 aca3b1d 770e786 aca3b1d 770e786 7112f5e aca3b1d 770e786 7112f5e aca3b1d 7112f5e 9fcb29b aca3b1d 7112f5e 9fcb29b ff648fb 770e786 7112f5e 770e786 aca3b1d 770e786 7112f5e 770e786 1ed0340 770e786 aca3b1d 770e786 1ed0340 aca3b1d 1ed0340 7112f5e 1ed0340 7112f5e 770e786 0131a54 9fcb29b c729869 8c42cdb 770e786 9fcb29b 1ed0340 c729869 1ed0340 c729869 aca3b1d 9fcb29b aca3b1d 9fcb29b c729869 aca3b1d 770e786 1ed0340 aca3b1d c729869 0131a54 1ed0340 770e786 1ed0340 |
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
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
# Word2Vec ๋ชจ๋ธ ํ์ต ํจ์
def train_word2vec(sentences):
# model = Word2Vec(sentences, vector_size=100, window=4, min_count=6, workers=4, sg=0, epochs=100)
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
# Word2Vec ๋ชจ๋ธ ํ์ต
model = train_word2vec(sentences)
unique_words = get_unique(model)
return model, unique_words
def process_text(target_word):
target_word =target_word.lower() #################
# Word2Vec ๋ชจ๋ธ ๋ก๋
model = Word2Vec.load("word2vec.model")
unique_words = get_unique(model)
# ๊ฐ ๋จ์ด์ ์๋ฒ ๋ฉ ๋ฒกํฐ ์ถ์ถ
word_vectors = np.array([model.wv[word] for word in unique_words])
# PCA๋ก ์ฐจ์ ์ถ์
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]
# ๊ฐ์ฅ ๊ฐ๊น์ด ๋จ์ด 10๊ฐ ์ฐพ๊ธฐ
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)' # ๊ฐ๊น์ด ๋จ์ด๋ค์ ์ฃผํฉ์์ผ๋ก ํ์
# ๊ฐ์ฅ ๋จผ ๋จ์ด 10๊ฐ ์ฐพ๊ธฐ
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] # ์ ์ฌ๋๊ฐ ๊ฐ์ฅ ๋ฎ์ 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)' # ๊ฐ์ฅ ๋จผ ๋จ์ด๋ค์ ๋ณด๋ผ์์ผ๋ก ํ์
# Plotly๋ฅผ ์ฌ์ฉํ 3D ์ฐ์ ๋ ์์ฑ
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
)
# ๊ฐ์ฅ ๊ฐ๊น์ด ๋จ์ด 10๊ฐ ๋ชฉ๋ก ์์ฑ
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
# Gradio ์ธํฐํ์ด์ค ์์
with gr.Blocks(css="""
#input-box {
background-color: #ffeef3; /* ์ฐํ ํ์คํ
ํํฌ */
border: 2px solid #ffccd5; /* ์ฐํ ํํฌ ํ
๋๋ฆฌ */
color: #000; /* ํ
์คํธ ์์ */
border-radius: 8px; /* ๋ฅ๊ทผ ํ
๋๋ฆฌ */
}
#submit-btn {
background-color: #ebfbea; /* ์ฐํ ํ์คํ
์ฐ๋์ */
border: 2px solid #d6f5d6; /* ์ฐํ ์ฐ๋์ ํ
๋๋ฆฌ */
color: #000; /* ํ
์คํธ ์์ */
border-radius: 8px; /* ๋ฅ๊ทผ ํ
๋๋ฆฌ */
}
#bulletin {
background-color: #eaf9ff; /* ์ฐํ ํ์คํ
ํ๋์ */
border: 2px solid #d3f0f7; /* ์ฐํ ํ๋์ ํ
๋๋ฆฌ */
color: #000; /* ํ
์คํธ ์์ */
border-radius: 8px; /* ๋ฅ๊ทผ ํ
๋๋ฆฌ */
}
#similar-words {
background-color: #fff0e6; /* ์ฐํ ํ์คํ
์ฃผํฉ์ */
border: 2px solid #ffe3cc; /* ์ฐํ ์ฃผํฉ ํ
๋๋ฆฌ */
color: #000; /* ํ
์คํธ ์์ */
border-radius: 8px; /* ๋ฅ๊ทผ ํ
๋๋ฆฌ */
}
#dissimilar-words {
background-color: #f2e6ff; /* ์ฐํ ํ์คํ
๋ณด๋ผ์ */
border: 2px solid #e0ccff; /* ์ฐํ ๋ณด๋ผ ํ
๋๋ฆฌ */
color: #000; /* ํ
์คํธ ์์ */
border-radius: 8px; /* ๋ฅ๊ทผ ํ
๋๋ฆฌ */
}
label {
font-weight: bold; /* ์ ๋ชฉ ๋ณผ๋์ฒด */
}
""") as iface:
gr.Markdown("# <Inside Out 2> ๋จ์ด ์๋ฏธ ์ง๋ 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]๋ฅผ ๋๋ฅด์ธ์\n"
"2. ์
๋ ฅ ๋จ์ด๋ ๋นจ๊ฐ์, ๊ฐ๊น์ด ๋จ์ด๋ค์ ์ฃผํฉ์, ๋จผ ๋จ์ด๋ค์ ๋ณด๋ผ์์ผ๋ก ๊ฐ์กฐ๋ฉ๋๋ค.\n"
"3. <Error>๊ฐ ๋ํ๋๋ ๊ฒฝ์ฐ, ๋ค๋ฅธ ๋จ์ด๋ฅผ ์
๋ ฅํด๋ณด์ธ์.\n"
"4. ๋ง์ฐ์ค ๋๋๊ทธ ๋ฐ ์คํฌ๋กค์ ํ์ฉํ์ฌ 3D ํ๋ฉด์ ์ดํด๋ณด์ธ์.\n"
"5. ๋จ์ด ์
๋ ฅ์ฐฝ์ ๋ค๋ฅธ ๋จ์ด๋ค๋ ์
๋ ฅํด๋ณด์ธ์."
),
elem_id="bulletin"
)
with gr.Row():
# ์๊ฐํ ํ๋ฉด์ ํฌ๊ธฐ๋ฅผ CSS๋ก ์ฆ๊ฐ
plot_output = gr.Plot(label="Word Embedding 3D ์๊ฐํ", elem_id="plot-box")
with gr.Column(scale=0.3): # ์ปฌ๋ผ์ ๋๋น๋ฅผ ์ค์ด๊ธฐ ์ํด scale ๊ฐ์ ๋ฎ์ถค
similar_words_output = gr.Textbox(
label="๊ฐ์ฅ ๊ฐ๊น์ด ๋จ์ด 10๊ฐ",
interactive=False,
lines=5,
elem_id="similar-words"
)
dissimilar_words_output = gr.Textbox(
label="๊ฐ์ฅ ๋จผ ๋จ์ด 10๊ฐ",
interactive=False,
lines=5,
elem_id="dissimilar-words"
)
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 "" # None ์ฒดํฌ ํ ์๋ฌธ์ ๋ณํ
)
if __name__ == "__main__":
iface.launch() |