Mohamed-BC
commited on
Commit
•
ecbc596
1
Parent(s):
9865e38
upload-v1.5.12
Browse files- recommend.py +21 -49
recommend.py
CHANGED
@@ -1,49 +1,21 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
if st.button("Login"):
|
23 |
-
if check_login(username, password):
|
24 |
-
# Update session state to indicate user is logged in
|
25 |
-
# st.session_state.username = username
|
26 |
-
st.session_state.logged_in = True
|
27 |
-
st.rerun() # Rerun the script to reflect the new state
|
28 |
-
else:
|
29 |
-
st.error("Invalid credentials. Please try again.")
|
30 |
-
|
31 |
-
# If logged in, redirect to another page or show different content
|
32 |
-
else:
|
33 |
-
# This can be another Streamlit page, or a condition to render a different view
|
34 |
-
st.title(f"Welcome :)!")
|
35 |
-
cols = st.columns([3,1])
|
36 |
-
with cols[0]:
|
37 |
-
query = st.text_input('Search here', placeholder="Describe what you're looking for", label_visibility="collapsed")
|
38 |
-
with cols[1]:
|
39 |
-
btn = st.button('Search')
|
40 |
-
if btn and query:
|
41 |
-
with st.spinner('Searching...'):
|
42 |
-
st.write_stream(recommend(query))
|
43 |
-
# Example: Provide a logout button
|
44 |
-
if st.sidebar.button("Logout"):
|
45 |
-
st.session_state.logged_in = False
|
46 |
-
st.rerun()
|
47 |
-
|
48 |
-
if __name__ == "__main__":
|
49 |
-
main()
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer
|
2 |
+
from scipy.spatial.distance import cosine
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from datasets import load_dataset
|
6 |
+
import pickle as pkl
|
7 |
+
def recommend(query, n=5):
|
8 |
+
# Load the model
|
9 |
+
model = SentenceTransformer('all-MiniLM-L6-v2', device='cpu')
|
10 |
+
# Load the data
|
11 |
+
# data = pd.read_csv('data/medium_articles.csv')
|
12 |
+
data = load_dataset('Mohamed-BC/Articles')['train'].to_pandas()
|
13 |
+
# get the embeddings
|
14 |
+
a_embeddings = pkl.load(open('data/articles_embeddings.pkl', 'rb'))
|
15 |
+
# Encode the query
|
16 |
+
q_embedding = model.encode(query)
|
17 |
+
# Calculate the cosine similarity
|
18 |
+
cos_sim = np.array([1 - cosine(q_embedding, emb) for emb in a_embeddings[:1000]])
|
19 |
+
# Get the top n recommendations
|
20 |
+
top_n = np.argsort(cos_sim)[-n:]
|
21 |
+
return data.iloc[top_n]['title']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|