prelim code test
Browse files
app.py
CHANGED
@@ -1,162 +1,72 @@
|
|
1 |
-
import
|
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 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
ui.card_header("Tips data")
|
74 |
-
|
75 |
-
@render.data_frame
|
76 |
-
def table():
|
77 |
-
return render.DataGrid(tips_data())
|
78 |
-
|
79 |
-
with ui.card(full_screen=True):
|
80 |
-
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
81 |
-
"Total bill vs tip"
|
82 |
-
with ui.popover(title="Add a color variable", placement="top"):
|
83 |
-
ICONS["ellipsis"]
|
84 |
-
ui.input_radio_buttons(
|
85 |
-
"scatter_color",
|
86 |
-
None,
|
87 |
-
["none", "sex", "smoker", "day", "time"],
|
88 |
-
inline=True,
|
89 |
-
)
|
90 |
-
|
91 |
-
@render_plotly
|
92 |
-
def scatterplot():
|
93 |
-
color = input.scatter_color()
|
94 |
-
return px.scatter(
|
95 |
-
tips_data(),
|
96 |
-
x="total_bill",
|
97 |
-
y="tip",
|
98 |
-
color=None if color == "none" else color,
|
99 |
-
trendline="lowess",
|
100 |
-
)
|
101 |
-
|
102 |
-
with ui.card(full_screen=True):
|
103 |
-
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
104 |
-
"Tip percentages"
|
105 |
-
with ui.popover(title="Add a color variable"):
|
106 |
-
ICONS["ellipsis"]
|
107 |
-
ui.input_radio_buttons(
|
108 |
-
"tip_perc_y",
|
109 |
-
"Split by:",
|
110 |
-
["sex", "smoker", "day", "time"],
|
111 |
-
selected="day",
|
112 |
-
inline=True,
|
113 |
-
)
|
114 |
-
|
115 |
-
@render_plotly
|
116 |
-
def tip_perc():
|
117 |
-
from ridgeplot import ridgeplot
|
118 |
-
|
119 |
-
dat = tips_data()
|
120 |
-
dat["percent"] = dat.tip / dat.total_bill
|
121 |
-
yvar = input.tip_perc_y()
|
122 |
-
uvals = dat[yvar].unique()
|
123 |
-
|
124 |
-
samples = [[dat.percent[dat[yvar] == val]] for val in uvals]
|
125 |
-
|
126 |
-
plt = ridgeplot(
|
127 |
-
samples=samples,
|
128 |
-
labels=uvals,
|
129 |
-
bandwidth=0.01,
|
130 |
-
colorscale="viridis",
|
131 |
-
colormode="row-index",
|
132 |
-
)
|
133 |
-
|
134 |
-
plt.update_layout(
|
135 |
-
legend=dict(
|
136 |
-
orientation="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5
|
137 |
-
)
|
138 |
-
)
|
139 |
-
|
140 |
-
return plt
|
141 |
-
|
142 |
-
|
143 |
-
ui.include_css(app_dir / "styles.css")
|
144 |
-
|
145 |
-
# --------------------------------------------------------
|
146 |
-
# Reactive calculations and effects
|
147 |
-
# --------------------------------------------------------
|
148 |
-
|
149 |
-
|
150 |
-
@reactive.calc
|
151 |
-
def tips_data():
|
152 |
-
bill = input.total_bill()
|
153 |
-
idx1 = tips.total_bill.between(bill[0], bill[1])
|
154 |
-
idx2 = tips.time.isin(input.time())
|
155 |
-
return tips[idx1 & idx2]
|
156 |
-
|
157 |
-
|
158 |
-
@reactive.effect
|
159 |
-
@reactive.event(input.reset)
|
160 |
-
def _():
|
161 |
-
ui.update_slider("total_bill", value=bill_rng)
|
162 |
-
ui.update_checkbox_group("time", selected=["Lunch", "Dinner"])
|
|
|
1 |
+
import numpy as np
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
4 |
+
from shiny import App, render, ui
|
5 |
+
|
6 |
+
# Initialize the sentence transformer model
|
7 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
+
|
9 |
+
# Sample queries
|
10 |
+
queries = [
|
11 |
+
"What is the weather today?",
|
12 |
+
"How to learn Python?",
|
13 |
+
"Best practices for data science.",
|
14 |
+
"What is the capital of France?",
|
15 |
+
"How to cook pasta?",
|
16 |
+
"Latest trends in technology.",
|
17 |
+
"What is machine learning?",
|
18 |
+
"Tips for healthy living.",
|
19 |
+
"How to invest in stocks?",
|
20 |
+
"Best programming languages to learn.",
|
21 |
+
"How to start a business?",
|
22 |
+
"What are the benefits of exercise?",
|
23 |
+
"History of the internet.",
|
24 |
+
"How to improve communication skills?",
|
25 |
+
"Understanding blockchain technology.",
|
26 |
+
"What is artificial intelligence?",
|
27 |
+
"Effective study techniques.",
|
28 |
+
"Travel tips for Europe.",
|
29 |
+
"How to write a resume?",
|
30 |
+
"What is the best way to learn math?"
|
31 |
+
]
|
32 |
+
|
33 |
+
# Precompute embeddings for the queries
|
34 |
+
query_embeddings = model.encode(queries)
|
35 |
+
|
36 |
+
# Define the UI
|
37 |
+
app_ui = ui.page_fluid(
|
38 |
+
ui.h2("Sentence Similarity Finder"),
|
39 |
+
ui.input_text("user_input", "Enter your text:", placeholder="Type here..."),
|
40 |
+
ui.action_button("submit", "Get Similar Queries"),
|
41 |
+
ui.output_ui("results")
|
42 |
+
)
|
43 |
+
|
44 |
+
# Define server logic
|
45 |
+
def server(input, output):
|
46 |
+
@output
|
47 |
+
@render.ui
|
48 |
+
def results():
|
49 |
+
if input.submit() > 0:
|
50 |
+
user_text = input.user_input()
|
51 |
+
if user_text:
|
52 |
+
# Compute the embedding for the user input
|
53 |
+
user_embedding = model.encode([user_text])
|
54 |
+
|
55 |
+
# Compute cosine similarities
|
56 |
+
similarities = cosine_similarity(user_embedding, query_embeddings).flatten()
|
57 |
+
|
58 |
+
# Get the indices of the top 5 similar queries
|
59 |
+
top_indices = np.argsort(similarities)[-5:][::-1]
|
60 |
+
|
61 |
+
# Prepare the results to display
|
62 |
+
result_boxes = ui.tag_list()
|
63 |
+
for idx in top_indices:
|
64 |
+
result_boxes.append(ui.div(queries[idx], class_="result-box"))
|
65 |
+
return result_boxes
|
66 |
+
return ui.div("Please enter text and press the button.")
|
67 |
+
|
68 |
+
# Create the Shiny app
|
69 |
+
app = App(app_ui, server)
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
app.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|