Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,18 +36,23 @@ def plot_words_plotly(model, words):
|
|
| 36 |
fig.update_layout(title="Word Vectors Visualization",
|
| 37 |
xaxis_title="PCA 1",
|
| 38 |
yaxis_title="PCA 2",
|
| 39 |
-
showlegend=True
|
|
|
|
|
|
|
| 40 |
|
| 41 |
return fig
|
| 42 |
|
| 43 |
|
| 44 |
-
def gradio_interface(choice, custom_input
|
| 45 |
if choice == "Custom":
|
| 46 |
if not custom_input or len(custom_input.split(", ")) != 3:
|
| 47 |
return "Invalid input. Please enter exactly three words, separated by commas.", None, {
|
| 48 |
"error": "Invalid input"}
|
| 49 |
words = custom_input.split(", ")
|
| 50 |
else:
|
|
|
|
|
|
|
|
|
|
| 51 |
words = choice.split(", ")
|
| 52 |
|
| 53 |
word1, word2, word3 = words
|
|
@@ -56,9 +61,9 @@ def gradio_interface(choice, custom_input=None):
|
|
| 56 |
|
| 57 |
if word4 in model.key_to_index:
|
| 58 |
vector = model[word4]
|
| 59 |
-
vector_display = {word4:
|
| 60 |
else:
|
| 61 |
-
vector_display =
|
| 62 |
|
| 63 |
return word4, plot_fig, vector_display
|
| 64 |
|
|
@@ -71,16 +76,39 @@ choices = [
|
|
| 71 |
"Custom"
|
| 72 |
]
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
iface.launch(share=True)
|
|
|
|
| 36 |
fig.update_layout(title="Word Vectors Visualization",
|
| 37 |
xaxis_title="PCA 1",
|
| 38 |
yaxis_title="PCA 2",
|
| 39 |
+
showlegend=True,
|
| 40 |
+
width=600, # Adjust width as needed
|
| 41 |
+
height=400) # Adjust height as needed
|
| 42 |
|
| 43 |
return fig
|
| 44 |
|
| 45 |
|
| 46 |
+
def gradio_interface(choice, custom_input):
|
| 47 |
if choice == "Custom":
|
| 48 |
if not custom_input or len(custom_input.split(", ")) != 3:
|
| 49 |
return "Invalid input. Please enter exactly three words, separated by commas.", None, {
|
| 50 |
"error": "Invalid input"}
|
| 51 |
words = custom_input.split(", ")
|
| 52 |
else:
|
| 53 |
+
if not choice:
|
| 54 |
+
return "Invalid input. Please select or enter words.", None, {
|
| 55 |
+
"error": "Invalid input"}
|
| 56 |
words = choice.split(", ")
|
| 57 |
|
| 58 |
word1, word2, word3 = words
|
|
|
|
| 61 |
|
| 62 |
if word4 in model.key_to_index:
|
| 63 |
vector = model[word4]
|
| 64 |
+
vector_display = f"{word4}: {np.round(vector, 2).tolist()}"
|
| 65 |
else:
|
| 66 |
+
vector_display = "Vector not available for the resulting word"
|
| 67 |
|
| 68 |
return word4, plot_fig, vector_display
|
| 69 |
|
|
|
|
| 76 |
"Custom"
|
| 77 |
]
|
| 78 |
|
| 79 |
+
|
| 80 |
+
def clear_inputs():
|
| 81 |
+
return "", "", "", "", None
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# Define the layout using Rows and Columns
|
| 85 |
+
with gr.Blocks() as iface:
|
| 86 |
+
with gr.Row():
|
| 87 |
+
with gr.Column():
|
| 88 |
+
gr.Markdown("# Word Analogy and Vector Visualization with Plotly")
|
| 89 |
+
gr.Markdown(
|
| 90 |
+
"Select a predefined triplet of words or choose 'Custom' and enter your own (comma-separated) to find a fourth word by analogy, and see their vectors plotted with Plotly.")
|
| 91 |
+
|
| 92 |
+
radio = gr.Radio(choices=choices, label="Choose predefined words or enter custom words")
|
| 93 |
+
|
| 94 |
+
custom_words = gr.Textbox(
|
| 95 |
+
label="Custom words (comma-separated, required for custom choice; use only if 'Custom' is selected)",
|
| 96 |
+
placeholder="Enter 3 words separated by commas")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
clear_btn = gr.Button("Clear")
|
| 100 |
+
submit_btn = gr.Button("Submit")
|
| 101 |
+
|
| 102 |
+
output_word = gr.Textbox(label="Output Word")
|
| 103 |
+
|
| 104 |
+
word_plot = gr.Plot(label="Word Vectors Visualization")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
word_vectorization = gr.Textbox(label="Word Vectorization for Output Word", lines=4, max_lines=4)
|
| 108 |
+
|
| 109 |
+
clear_btn.click(fn=clear_inputs, inputs=None,
|
| 110 |
+
outputs=[radio, custom_words, output_word, word_vectorization, word_plot])
|
| 111 |
+
submit_btn.click(fn=gradio_interface, inputs=[radio, custom_words],
|
| 112 |
+
outputs=[output_word, word_plot, word_vectorization])
|
| 113 |
|
| 114 |
iface.launch(share=True)
|