Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +23 -13
src/streamlit_app.py
CHANGED
|
@@ -6,26 +6,31 @@ import json
|
|
| 6 |
from datetime import datetime
|
| 7 |
import requests
|
| 8 |
import difflib
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
from recommendation_utils import (
|
| 11 |
load_nn_model, load_svd_model, load_trainset,
|
| 12 |
recommend_with_nn, recommend_with_svd
|
| 13 |
)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
import requests
|
| 19 |
|
| 20 |
@st.cache_resource(show_spinner=False)
|
| 21 |
def download_models_once():
|
|
|
|
|
|
|
| 22 |
files = {
|
| 23 |
"encodings.pkl": "1EzpdpaopfUp-Tfc7YjxPVYQUwnU_BX5-",
|
| 24 |
"recommender_model.keras": "1OwdH3RxlQfAX9UbUB7RwlS13i9uDZuG-",
|
| 25 |
"svd_model.pkl": "1fN2biQruVjJHHv2vX1g1hLuMeJoPqyFX",
|
| 26 |
"trainset.pkl": "1IDVVAQ57Xvf3HCAikbOSQgAigdHP7Ik7"
|
| 27 |
}
|
| 28 |
-
|
| 29 |
def gdrive_download(file_id, destination):
|
| 30 |
URL = "https://drive.google.com/uc?export=download"
|
| 31 |
session = requests.Session()
|
|
@@ -40,23 +45,27 @@ def download_models_once():
|
|
| 40 |
for chunk in response.iter_content(32768):
|
| 41 |
if chunk:
|
| 42 |
f.write(chunk)
|
| 43 |
-
|
| 44 |
for filename, file_id in files.items():
|
| 45 |
-
|
|
|
|
| 46 |
with st.spinner(f"Downloading {filename}..."):
|
| 47 |
-
gdrive_download(file_id,
|
|
|
|
| 48 |
|
| 49 |
download_models_once()
|
| 50 |
|
| 51 |
@st.cache_resource
|
| 52 |
def load_models():
|
| 53 |
-
nn_model = load_nn_model("recommender_model.keras")
|
| 54 |
-
svd_model = load_svd_model("svd_model.pkl")
|
| 55 |
-
trainset = load_trainset("trainset.pkl")
|
| 56 |
return nn_model, svd_model, trainset
|
| 57 |
|
| 58 |
nn_model, svd_model, trainset = load_models()
|
| 59 |
|
|
|
|
|
|
|
| 60 |
st.set_page_config(layout="wide")
|
| 61 |
|
| 62 |
MOVIES_PATH = os.path.join(os.path.dirname(__file__), "movies.csv")
|
|
@@ -544,7 +553,8 @@ else:
|
|
| 544 |
if user_ratings_dict:
|
| 545 |
if st.session_state["model_selection"] == "Neural Network":
|
| 546 |
available_movies = movie_df["movieId"].tolist()
|
| 547 |
-
recommendations = recommend_with_nn(
|
|
|
|
| 548 |
else:
|
| 549 |
ratings_full = pd.DataFrame(all_ratings_data)
|
| 550 |
ratings_full["userId"] = 999999 # Dummy user
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
import requests
|
| 8 |
import difflib
|
| 9 |
+
import pickle
|
| 10 |
+
from keras.models import load_model
|
| 11 |
+
import os
|
| 12 |
+
import requests
|
| 13 |
|
| 14 |
from recommendation_utils import (
|
| 15 |
load_nn_model, load_svd_model, load_trainset,
|
| 16 |
recommend_with_nn, recommend_with_svd
|
| 17 |
)
|
| 18 |
+
from recommendation_utils import (
|
| 19 |
+
load_nn_model, load_svd_model, load_trainset,
|
| 20 |
+
recommend_with_nn, recommend_with_svd, load_encodings
|
| 21 |
+
)
|
|
|
|
| 22 |
|
| 23 |
@st.cache_resource(show_spinner=False)
|
| 24 |
def download_models_once():
|
| 25 |
+
DOWNLOAD_DIR = "/tmp"
|
| 26 |
+
|
| 27 |
files = {
|
| 28 |
"encodings.pkl": "1EzpdpaopfUp-Tfc7YjxPVYQUwnU_BX5-",
|
| 29 |
"recommender_model.keras": "1OwdH3RxlQfAX9UbUB7RwlS13i9uDZuG-",
|
| 30 |
"svd_model.pkl": "1fN2biQruVjJHHv2vX1g1hLuMeJoPqyFX",
|
| 31 |
"trainset.pkl": "1IDVVAQ57Xvf3HCAikbOSQgAigdHP7Ik7"
|
| 32 |
}
|
| 33 |
+
|
| 34 |
def gdrive_download(file_id, destination):
|
| 35 |
URL = "https://drive.google.com/uc?export=download"
|
| 36 |
session = requests.Session()
|
|
|
|
| 45 |
for chunk in response.iter_content(32768):
|
| 46 |
if chunk:
|
| 47 |
f.write(chunk)
|
| 48 |
+
|
| 49 |
for filename, file_id in files.items():
|
| 50 |
+
full_path = os.path.join(DOWNLOAD_DIR, filename)
|
| 51 |
+
if not os.path.exists(full_path):
|
| 52 |
with st.spinner(f"Downloading {filename}..."):
|
| 53 |
+
gdrive_download(file_id, full_path)
|
| 54 |
+
|
| 55 |
|
| 56 |
download_models_once()
|
| 57 |
|
| 58 |
@st.cache_resource
|
| 59 |
def load_models():
|
| 60 |
+
nn_model = load_nn_model("/tmp/recommender_model.keras")
|
| 61 |
+
svd_model = load_svd_model("/tmp/svd_model.pkl")
|
| 62 |
+
trainset = load_trainset("/tmp/trainset.pkl")
|
| 63 |
return nn_model, svd_model, trainset
|
| 64 |
|
| 65 |
nn_model, svd_model, trainset = load_models()
|
| 66 |
|
| 67 |
+
encodings = load_encodings("/tmp/encodings.pkl")
|
| 68 |
+
|
| 69 |
st.set_page_config(layout="wide")
|
| 70 |
|
| 71 |
MOVIES_PATH = os.path.join(os.path.dirname(__file__), "movies.csv")
|
|
|
|
| 553 |
if user_ratings_dict:
|
| 554 |
if st.session_state["model_selection"] == "Neural Network":
|
| 555 |
available_movies = movie_df["movieId"].tolist()
|
| 556 |
+
recommendations = recommend_with_nn(user_ratings, nn_model, encodings, top_n=10)
|
| 557 |
+
|
| 558 |
else:
|
| 559 |
ratings_full = pd.DataFrame(all_ratings_data)
|
| 560 |
ratings_full["userId"] = 999999 # Dummy user
|