Mohamed-BC commited on
Commit
ecbc596
1 Parent(s): 9865e38

upload-v1.5.12

Browse files
Files changed (1) hide show
  1. recommend.py +21 -49
recommend.py CHANGED
@@ -1,49 +1,21 @@
1
- # Streamlit app script
2
- import streamlit as st
3
- from recommend import recommend
4
- # A simple function to check login credentials (for demonstration purposes)
5
- def check_login(username, password):
6
- # Hardcoding a simple example username and password
7
- user = "admin"
8
- pwd = "pass123"
9
- return username == user and password == pwd
10
-
11
- # Main application code
12
- def main():
13
- # Initialize session state for login status
14
- if "logged_in" not in st.session_state:
15
- st.session_state.logged_in = False
16
-
17
- # If not logged in, display login form
18
- if not st.session_state.logged_in:
19
- st.title("Login Page")
20
- username = st.text_input("Username")
21
- password = st.text_input("Password", type="password")
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']