import numpy as np from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity from shiny import App, render, ui # Initialize the sentence transformer model model = SentenceTransformer('all-MiniLM-L6-v2') # Sample queries queries = [ "What is the weather today?", "How to learn Python?", "Best practices for data science.", "What is the capital of France?", "How to cook pasta?", "Latest trends in technology.", "What is machine learning?", "Tips for healthy living.", "How to invest in stocks?", "Best programming languages to learn.", "How to start a business?", "What are the benefits of exercise?", "History of the internet.", "How to improve communication skills?", "Understanding blockchain technology.", "What is artificial intelligence?", "Effective study techniques.", "Travel tips for Europe.", "How to write a resume?", "What is the best way to learn math?" ] # Precompute embeddings for the queries query_embeddings = model.encode(queries) # Define the UI app_ui = ui.page_fluid( ui.h2("Sentence Similarity Finder"), ui.input_text("user_input", "Enter your text:", placeholder="Type here..."), ui.action_button("submit", "Get Similar Queries"), ui.output_ui("results") ) # Define server logic def server(input, output): @output @render.ui def results(): if input.submit() > 0: user_text = input.user_input() if user_text: # Compute the embedding for the user input user_embedding = model.encode([user_text]) # Compute cosine similarities similarities = cosine_similarity(user_embedding, query_embeddings).flatten() # Get the indices of the top 5 similar queries top_indices = np.argsort(similarities)[-5:][::-1] # Prepare the results to display result_boxes = ui.tag_list() for idx in top_indices: result_boxes.append(ui.div(queries[idx], class_="result-box")) return result_boxes return ui.div("Please enter text and press the button.") # Create the Shiny app app = App(app_ui, server) if __name__ == "__main__": app.run()