jsodoge's picture
Update app.py
31f420c verified
raw
history blame
1.96 kB
import numpy as np
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
from shiny import App, render, ui
import pandas as pd
# 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?",
# ... (other queries)
#]
queries = pd.read_excel("egu_session_descriptions.xlsx").Description
# 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.input_action_button("submit", "Get Similar Queries"),
ui.output_ui("results")
)
# Define server logic
def server(input, output, session):
@output
@render.ui
def results():
if input.submit(): # Note the () to call the input
user_text = input.user_input() # Note the () to call the 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.div(queries[idx], class_="result-box") for idx in top_indices]
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()