|
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 |
|
|
|
|
|
model = SentenceTransformer('all-MiniLM-L6-v2') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
queries = pd.read_excel("egu_session_descriptions.xlsx").Description |
|
|
|
|
|
query_embeddings = model.encode(queries) |
|
|
|
|
|
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") |
|
) |
|
|
|
|
|
def server(input, output, session): |
|
@output |
|
@render.ui |
|
def results(): |
|
if input.submit(): |
|
user_text = input.user_input() |
|
if user_text: |
|
|
|
user_embedding = model.encode([user_text]) |
|
|
|
|
|
similarities = cosine_similarity(user_embedding, query_embeddings).flatten() |
|
|
|
|
|
top_indices = np.argsort(similarities)[-5:][::-1] |
|
|
|
|
|
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.") |
|
|
|
|
|
app = App(app_ui, server) |
|
|
|
if __name__ == "__main__": |
|
app.run() |
|
|