File size: 1,956 Bytes
68ff734
 
 
 
31f420c
68ff734
 
 
 
 
31f420c
 
 
 
 
 
04711a3
31f420c
 
 
68ff734
 
 
 
 
 
 
 
8f5e518
68ff734
 
 
 
7bf2cd6
68ff734
 
 
3a4d1bc
 
68ff734
 
 
 
 
 
 
 
 
 
 
73a582c
04711a3
68ff734
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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()