Spaces:
Runtime error
Runtime error
Update app.py
Browse files35 unmodified lines3637383940414243444546474849505152532 unmodified lines5657585960616263646 unmodified lines7172737475767778798081828384858635 unmodified linesfig.update_layout(title="Word Vectors Visualization",xaxis_title="PCA 1",yaxis_title="PCA 2",showlegend=True)return figdef gradio_interface(choice, custom_input=None):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:words = choice.split(", ")word1, word2, word3 = words2 unmodified linesif word4 in model.key_to_index:vector = model[word4]vector_display = {word4: [round(num, 2) for num in vector.tolist()]}else:vector_display = {"error": "Vector not available for the resulting word"}return word4, plot_fig, vector_display6 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.")iface.launch(share=True)35 unmodified lines36373839404142434445464748495051525354555657582 unmodified lines6162636465666768696 unmodified lines76777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311435 unmodified linesfig.update_layout(title="Word Vectors Visualization",xaxis_title="PCA 1",yaxis_title="PCA 2",showlegend=True,width=600, # Adjust width as neededheight=400) # Adjust height as neededreturn figdef 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 linesif word4 in model.key_to_index:vector = model[word4]vector_display = f"{word4}: {np.round(vector, 2).tolist()}"else:vector_display = "Vector not available for the resulting word"return word4, plot_fig, vector_display6 unmodified lines"Custom"]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