Spaces:
Sleeping
Sleeping
Commit
·
de79df8
1
Parent(s):
d68b3f4
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,20 @@ def isNoneWords(word):
|
|
7 |
return True
|
8 |
else:
|
9 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def top_similarity_route(word):
|
12 |
if isNoneWords(word):
|
@@ -18,10 +32,90 @@ def top_similarity_route(word):
|
|
18 |
sim_res += f'{item[0]}: {round(item[1], 4)}\n'
|
19 |
return sim_res
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
title = 'Calculate word similarity based on Tencent AI Lab Embedding'
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
return True
|
8 |
else:
|
9 |
return False
|
10 |
+
|
11 |
+
def word_analogy(word1, word2, word3):
|
12 |
+
analogy_words = model.similar_by_vector(model.word_vec(word1) - model.word_vec(word2) + model.word_vec(word3))
|
13 |
+
sim_res = ""
|
14 |
+
for item in analogy_words:
|
15 |
+
sim_res += f'{item[0]}: {round(item[1], 4)}\n'
|
16 |
+
return sim_res
|
17 |
+
|
18 |
+
def similarity_route(word1, word2):
|
19 |
+
if isNoneWords(word1) or isNoneWords(word2):
|
20 |
+
return "word is null or not in model!"
|
21 |
+
else:
|
22 |
+
return float(model.similarity(word1, word2))
|
23 |
+
|
24 |
|
25 |
def top_similarity_route(word):
|
26 |
if isNoneWords(word):
|
|
|
32 |
sim_res += f'{item[0]}: {round(item[1], 4)}\n'
|
33 |
return sim_res
|
34 |
|
35 |
+
def top_similar_words_layout():
|
36 |
+
with gr.Column():
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column():
|
39 |
+
word = gr.Textbox(lines=1, label='Input word', placeholder='Input word here')
|
40 |
+
with gr.Row():
|
41 |
+
clear = gr.ClearButton()
|
42 |
+
submit = gr.Button("Submit")
|
43 |
+
output = gr.Textbox(lines=20, label='Similar words', placeholder='Output here')
|
44 |
|
45 |
+
submit.click(fn=top_similarity_route, inputs=[word], outputs=[output])
|
46 |
+
|
47 |
+
examples=[['兔子', '松鼠']]
|
48 |
+
ex = gr.Examples(
|
49 |
+
examples=examples,
|
50 |
+
fn=top_similarity_route,
|
51 |
+
inputs=[word],
|
52 |
+
outputs=[output],
|
53 |
+
cache_examples=False,
|
54 |
+
run_on_click=False
|
55 |
+
)
|
56 |
+
|
57 |
|
58 |
+
def similarity_layout():
|
59 |
+
with gr.Column():
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
with gr.Row():
|
63 |
+
word1 = gr.Textbox(lines=1, label='Input word1', placeholder='Input word1 here')
|
64 |
+
word2 = gr.Textbox(lines=1, label='Input word2', placeholder='Input word2 here')
|
65 |
+
with gr.Row():
|
66 |
+
clear = gr.ClearButton()
|
67 |
+
submit = gr.Button("Submit")
|
68 |
+
output = gr.Textbox(lines=1, label='Similar words', placeholder='Output here')
|
69 |
+
|
70 |
+
submit.click(fn=similarity_route, inputs=[word1, word2], outputs=[output])
|
71 |
+
|
72 |
+
examples=[['淘宝', '京东', 0.7887385]]
|
73 |
+
ex = gr.Examples(
|
74 |
+
examples=examples,
|
75 |
+
fn=similarity_route,
|
76 |
+
inputs=[word1, word2],
|
77 |
+
outputs=[output],
|
78 |
+
cache_examples=False,
|
79 |
+
run_on_click=False
|
80 |
+
)
|
81 |
+
|
82 |
+
def word_analogy_layout():
|
83 |
+
with gr.Column():
|
84 |
+
with gr.Row():
|
85 |
+
with gr.Column():
|
86 |
+
with gr.Row():
|
87 |
+
word1 = gr.Textbox(lines=1, label='Input word1', placeholder='Input word1 here')
|
88 |
+
word2 = gr.Textbox(lines=1, label='Input word2', placeholder='Input word2 here')
|
89 |
+
word3 = gr.Textbox(lines=1, label='Input word3', placeholder='Input word3 here')
|
90 |
+
with gr.Row():
|
91 |
+
clear = gr.ClearButton()
|
92 |
+
submit = gr.Button("Submit")
|
93 |
+
output = gr.Textbox(lines=1, label='Analogy words', placeholder='Output here')
|
94 |
+
|
95 |
+
submit.click(fn=word_analogy, inputs=[word1, word2, word3], outputs=[output])
|
96 |
+
|
97 |
+
examples=[['国王', '男人', '女人', '王后']]
|
98 |
+
ex = gr.Examples(
|
99 |
+
examples=examples,
|
100 |
+
fn=word_analogy,
|
101 |
+
inputs=[word1, word2, word3],
|
102 |
+
outputs=[output],
|
103 |
+
cache_examples=False,
|
104 |
+
run_on_click=False
|
105 |
+
)
|
106 |
+
|
107 |
+
if __name__ == '__main__':
|
108 |
+
model = KeyedVectors.load_word2vec_format('../word_sim_demo/tencent-ailab-embedding-zh-d100-v0.2.0-s/tencent-ailab-embedding-zh-d100-v0.2.0-s.txt', binary=False)
|
109 |
title = 'Calculate word similarity based on Tencent AI Lab Embedding'
|
110 |
+
|
111 |
+
with gr.Blocks() as demo:
|
112 |
+
gr.HTML(title)
|
113 |
+
with gr.Column(elem_id="col-container"):
|
114 |
+
with gr.Tab("Top similar words"):
|
115 |
+
top_similar_words_layout()
|
116 |
+
with gr.Tab("Similarity of words"):
|
117 |
+
similarity_layout()
|
118 |
+
with gr.Tab("Word analogy"):
|
119 |
+
word_analogy_layout()
|
120 |
+
|
121 |
+
demo.queue(max_size=64).launch()
|