Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from sklearn.decomposition import PCA
|
| 6 |
+
from sklearn.manifold import TSNE
|
| 7 |
+
model = np.load('gpt2-1k-words.npy',allow_pickle='TRUE').item()
|
| 8 |
+
|
| 9 |
+
cache = "/home/user/app/d.jpg"
|
| 10 |
+
|
| 11 |
+
# Function to reduce dimensions
|
| 12 |
+
def reduce_dimensions(data, method='PCA'):
|
| 13 |
+
if method == 'PCA':
|
| 14 |
+
model = PCA(n_components=2)
|
| 15 |
+
elif method == 'TSNE':
|
| 16 |
+
model = TSNE(n_components=2, learning_rate='auto', init='random', perplexity=3)
|
| 17 |
+
return model.fit_transform(data)
|
| 18 |
+
|
| 19 |
+
# Plotting function
|
| 20 |
+
def plot_reduced_data(reduced_data, labels, title):
|
| 21 |
+
plt.figure(figsize=(10, 8))
|
| 22 |
+
plt.scatter(reduced_data[:, 0], reduced_data[:, 1], alpha=0.6)
|
| 23 |
+
for i, label in enumerate(labels):
|
| 24 |
+
plt.annotate(" " + label, (reduced_data[i, 0], reduced_data[i, 1]), fontsize=18)
|
| 25 |
+
plt.title(title)
|
| 26 |
+
# Data for the arrow 1
|
| 27 |
+
start_point = (reduced_data[0, 0], reduced_data[0, 1]) # Starting point of the arrow
|
| 28 |
+
end_point = (reduced_data[1, 0], reduced_data[1, 1]) # Ending point of the arrow
|
| 29 |
+
|
| 30 |
+
# Adding an arrow 1
|
| 31 |
+
plt.annotate('', xy=end_point, xytext=start_point,
|
| 32 |
+
arrowprops=dict(arrowstyle="->", color='green', lw=3))
|
| 33 |
+
|
| 34 |
+
# Data for the arrow 2
|
| 35 |
+
end_point = (reduced_data[-1, 0] , reduced_data[-1, 1]) # Starting point of the arrow
|
| 36 |
+
start_point = (reduced_data[2, 0], reduced_data[2, 1]) # Ending point of the arrow
|
| 37 |
+
|
| 38 |
+
# Adding an arrow 2
|
| 39 |
+
plt.annotate('', xy=end_point, xytext=start_point,
|
| 40 |
+
arrowprops=dict(arrowstyle="->", color='green', lw=3))
|
| 41 |
+
|
| 42 |
+
plt.xlabel('Component 1')
|
| 43 |
+
plt.ylabel('Component 2')
|
| 44 |
+
plt.grid(True)
|
| 45 |
+
plt.savefig(cache)
|
| 46 |
+
|
| 47 |
+
description = """
|
| 48 |
+
### Word Embedding Demo App
|
| 49 |
+
Universidade Federal de São Paulo - Escola Paulista de Medicina
|
| 50 |
+
The output is Word3 + (Word2 - Word1)
|
| 51 |
+
Credits:
|
| 52 |
+
* Gensim
|
| 53 |
+
* Glove
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
Word1 = gr.Textbox()
|
| 57 |
+
Word2 = gr.Textbox()
|
| 58 |
+
Word3 = gr.Textbox()
|
| 59 |
+
label = gr.Label(show_label=True, label="Word4")
|
| 60 |
+
sp = gr.Image()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def inference(word1, word2, word3):
|
| 64 |
+
transform = model[word3] + model[word2] - model[word1]
|
| 65 |
+
output = model.similar_by_vector(transform)
|
| 66 |
+
print(output)
|
| 67 |
+
word_list = [word1, word2, word3]
|
| 68 |
+
word_list.extend([x for x,y in [item for item in output[:4]]])
|
| 69 |
+
words = {key: model[key] for key in word_list}
|
| 70 |
+
words[word3 + " + (" + word2 + " - " + word1 + ")"] = transform
|
| 71 |
+
data = np.concatenate([x[np.newaxis, :] for x in words.values()], axis=0)
|
| 72 |
+
print(data.shape)
|
| 73 |
+
labels = words.keys()
|
| 74 |
+
reduced_data_pca = reduce_dimensions(data, method='PCA')
|
| 75 |
+
print(reduced_data_pca.shape)
|
| 76 |
+
plot_reduced_data(reduced_data_pca, labels, 'PCA Results')
|
| 77 |
+
return cache
|
| 78 |
+
|
| 79 |
+
examples = [
|
| 80 |
+
["woman", "man", "aunt"],
|
| 81 |
+
["woman", "man", "girl"],
|
| 82 |
+
["woman", "man", "granddaughter"],
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
iface = gr.Interface(
|
| 86 |
+
fn=inference,
|
| 87 |
+
inputs=[Word1, Word2, Word3],
|
| 88 |
+
outputs=sp,
|
| 89 |
+
description=description,
|
| 90 |
+
examples=examples
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
iface.launch()
|