Spaces:
Runtime error
Runtime error
Update app.py
Browse files35 unmodified lines363738393940414243444544464748495051525354555657582 unmodified lines61626359646561666768696 unmodified lines767778747576777879808182838479808182838485868788899091929394959697989910010110210310410510610710810911011111211311435 unmodified lines fig.update_layout(title="Word Vectors Visualization", xaxis_title="PCA 1", yaxis_title="PCA 2", showlegend=True) showlegend=True, width=600, # Adjust width as needed height=400) # Adjust height as needed
return fig
def gradio_interface(choice, custom_input=None):def gradio_interface(choice, custom_input): if choice == "Custom": if not custom_input or len(custom_input.split(", ")) != 3: return "Invalid input. Please enter exactly three words, separated by commas.", None, { "error": "Invalid input"} words = custom_input.split(", ") else: if not choice: return "Invalid input. Please select or enter words.", None, { "error": "Invalid input"} words = choice.split(", ")
word1, word2, word3 = words2 unmodified lines
if word4 in model.key_to_index: vector = model[word4] vector_display = {word4: [round(num, 2) for num in vector.tolist()]} vector_display = f"{word4}: {np.round(vector, 2).tolist()}" else: vector_display = {"error": "Vector not available for the resulting word"} vector_display = "Vector not available for the resulting word"
return word4, plot_fig, vector_display
6 unmodified lines "Custom"]
iface = gr.Interface( fn=gradio_interface, inputs=[ gr.Dropdown(choices=choices, label="Choose predefined words or enter custom words"), gr.Textbox(label="Custom words (comma-separated, required for custom choice; use only if 'Custom' is selected)", placeholder="Enter 3 words separated by commas") ], outputs=["text", "plot", "json"], title="Word Analogy and Vector Visualization with Plotly", description="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.")
def clear_inputs(): return "", "", "", "", None
# Define the layout using Rows and Columnswith gr.Blocks() as iface: with gr.Row(): with gr.Column(): gr.Markdown("# Word Analogy and Vector Visualization with Plotly") gr.Markdown( "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.")
radio = gr.Radio(choices=choices, label="Choose predefined words or enter custom words")
custom_words = gr.Textbox( label="Custom words (comma-separated, required for custom choice; use only if 'Custom' is selected)", placeholder="Enter 3 words separated by commas")
with gr.Row(): clear_btn = gr.Button("Clear") submit_btn = gr.Button("Submit")
output_word = gr.Textbox(label="Output Word")
word_plot = gr.Plot(label="Word Vectors Visualization")
with gr.Row(): word_vectorization = gr.Textbox(label="Word Vectorization for Output Word", lines=4, max_lines=4)
clear_btn.click(fn=clear_inputs, inputs=None, outputs=[radio, custom_words, output_word, word_vectorization, word_plot]) submit_btn.click(fn=gradio_interface, inputs=[radio, custom_words], outputs=[output_word, word_plot, word_vectorization])
iface.launch(share=True)app.py+43-15