felipekitamura commited on
Commit
db01b5c
·
verified ·
1 Parent(s): 5dcc9ef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ cache = ""
11
+
12
+ # Function to reduce dimensions
13
+ def reduce_dimensions(data, method='PCA'):
14
+ if method == 'PCA':
15
+ model = PCA(n_components=2)
16
+ elif method == 'TSNE':
17
+ model = TSNE(n_components=2, learning_rate='auto', init='random', perplexity=3)
18
+ return model.fit_transform(data)
19
+
20
+ # Plotting function
21
+ def plot_reduced_data(reduced_data, labels, title):
22
+ plt.figure(figsize=(10, 8))
23
+ plt.scatter(reduced_data[:, 0], reduced_data[:, 1], alpha=0.6)
24
+ for i, label in enumerate(labels):
25
+ plt.annotate(label, (reduced_data[i, 0], reduced_data[i, 1]))
26
+ plt.title(title)
27
+ plt.xlabel('Component 1')
28
+ plt.ylabel('Component 2')
29
+ plt.grid(True)
30
+ plt.savefig(cache)
31
+
32
+ description = """
33
+ ### Word Embedding Demo App
34
+ Universidade Federal de São Paulo - Escola Paulista de Medicina
35
+
36
+ The output is Word3 + (Word2 - Word1)
37
+
38
+ Credits:
39
+ * Gensim
40
+ * Glove
41
+ """
42
+
43
+ Word1 = gr.Textbox()
44
+ Word2 = gr.Textbox()
45
+ Word3 = gr.Textbox()
46
+ label = gr.Label(show_label=True, label="Word4")
47
+ sp = gr.SimpleImage()
48
+
49
+
50
+ def inference(word1, word2, word3):
51
+ output = model.similar_by_vector(model[word3] + model[word2] - model[word1])
52
+ print(output)
53
+ word_list = [word1, word2, word3]
54
+ word_list.extend([x for x,y in [item for item in output[:4]]])
55
+ words = {key: model[key] for key in word_list}
56
+ data = np.concatenate([x[np.newaxis, :] for x in words.values()], axis=0)
57
+ print(data.shape)
58
+ labels = words.keys()
59
+ reduced_data_pca = reduce_dimensions(data, method='PCA')
60
+ print(reduced_data_pca.shape)
61
+ plot_reduced_data(reduced_data_pca, labels, 'PCA Results')
62
+ return cache
63
+
64
+ examples = [
65
+ ["woman", "man", "aunt"],
66
+ ["woman", "man", "girl"],
67
+ ["woman", "man", "granddaughter"],
68
+ ]
69
+
70
+ iface = gr.Interface(
71
+ fn=inference,
72
+ inputs=[Word1, Word2, Word3],
73
+ outputs=sp,
74
+ description=description,
75
+ examples=examples
76
+ )
77
+
78
+ iface.launch()