felipekitamura commited on
Commit
f5d7b87
·
verified ·
1 Parent(s): 67692e8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gensim.downloader
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ from sklearn.decomposition import PCA
7
+ from sklearn.manifold import TSNE
8
+ model = gensim.downloader.load("glove-wiki-gigaword-50")
9
+
10
+ # Function to reduce dimensions
11
+ def reduce_dimensions(data, method='PCA'):
12
+ if method == 'PCA':
13
+ model = PCA(n_components=2)
14
+ elif method == 'TSNE':
15
+ model = TSNE(n_components=2, learning_rate='auto', init='random', perplexity=3)
16
+ return model.fit_transform(data)
17
+
18
+ description = """
19
+ ### Word Embedding Demo App
20
+ Universidade Federal de São Paulo - Escola Paulista de Medicina
21
+
22
+ The output is Word3 + (Word2 - Word1)
23
+
24
+ Credits:
25
+ * Gensim
26
+ * Glove
27
+ """
28
+
29
+ Word1 = gr.Textbox()
30
+ Word2 = gr.Textbox()
31
+ Word3 = gr.Textbox()
32
+ label = gr.Label(show_label=True, label="Word4")
33
+ sp = gr.ScatterPlot(x="x", y="y", color="color")
34
+
35
+
36
+ def inference(word1, word2, word3):
37
+ output = model.similar_by_vector(model[word3] + model[word2] - model[word1])
38
+ word_list = [word1, word2, word3]
39
+ word_list.expand([x for x,y in output[:4]])
40
+ words = {key: model[key] for key in word_list}
41
+ data = np.concatenate([x[np.newaxis, :] for x in words.values()], axis=0)
42
+ labels = words.keys()
43
+ reduced_data_pca = reduce_dimensions(data, method='PCA')
44
+ df = pd.DataFrame({
45
+ "x": reduced_data[:, 0],
46
+ "y": reduced_data[:, 1],
47
+ "color": ["b", "b", "b", "r", "g", "g", "g"]
48
+ })
49
+
50
+ return df
51
+
52
+ examples = [
53
+ ["woman", "man", "aunt"],
54
+ ["woman", "man", "girl"],
55
+ ["woman", "man", "granddaughter"],
56
+ ]
57
+
58
+ iface = gr.Interface(
59
+ fn=inference,
60
+ inputs=[Word1, Word2, Word3],
61
+ outputs=sp,
62
+ description=description,
63
+ examples=examples
64
+ )
65
+
66
+ iface.launch()