Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +58 -31
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,67 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
|
| 13 |
-
|
| 14 |
-
"""
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
})
|
| 32 |
|
| 33 |
-
|
| 34 |
-
.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
+
from annoy import AnnoyIndex
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
PLACEHOLDER_POSTER = "https://upload.wikimedia.org/wikipedia/commons/3/3f/Placeholder_view_vector.svg"
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Download from your Hugging Face repo
|
| 10 |
+
movies_path = hf_hub_download(repo_id="Smiley0707/Movie-recommendation", filename="movies_df.csv")
|
| 11 |
+
feature_path = hf_hub_download(repo_id="Smiley0707/Movie-recommendation", filename="feature_array.npz")
|
| 12 |
+
index_path = hf_hub_download(repo_id="Smiley0707/Movie-recommendation", filename="my_index.ann")
|
| 13 |
|
| 14 |
+
# Load files
|
| 15 |
+
new_movies = pd.read_csv(movies_path)
|
| 16 |
+
feature_array = np.load(feature_path)['arr_0']
|
| 17 |
+
f = feature_array.shape[1]
|
| 18 |
+
annoy_index = AnnoyIndex(f, 'angular')
|
| 19 |
+
annoy_index.load(index_path)
|
| 20 |
+
new_movies['title'] = new_movies['title'].fillna('')
|
| 21 |
+
new_movies['poster_path'] = new_movies['poster_path'].fillna('')
|
| 22 |
|
| 23 |
+
# Main UI
|
| 24 |
+
st.set_page_config(page_title="🎬 Movie Recommender", layout="wide")
|
| 25 |
+
st.title("🎬 Movie Recommendation System")
|
| 26 |
|
| 27 |
+
query = st.text_input("Search for a movie:")
|
|
|
|
| 28 |
|
| 29 |
+
def get_poster(idx):
|
| 30 |
+
url = new_movies.iloc[idx]['poster_path']
|
| 31 |
+
if not url or str(url).strip() == "":
|
| 32 |
+
return PLACEHOLDER_POSTER
|
| 33 |
+
return url
|
|
|
|
| 34 |
|
| 35 |
+
if query:
|
| 36 |
+
mask = new_movies['title'].str.lower().str.contains(query.lower(), na=False)
|
| 37 |
+
results = new_movies[mask]
|
| 38 |
+
if results.empty:
|
| 39 |
+
st.info("No movies found.")
|
| 40 |
+
else:
|
| 41 |
+
st.subheader("Search Results:")
|
| 42 |
+
cols = st.columns(5)
|
| 43 |
+
movie_indices = list(results.index[:5])
|
| 44 |
+
for i, idx in enumerate(movie_indices):
|
| 45 |
+
with cols[i]:
|
| 46 |
+
st.markdown(
|
| 47 |
+
f"""<div style="border-radius:15px;border:2px solid #eee;padding:10px;text-align:center;">
|
| 48 |
+
<img src="{get_poster(idx)}" alt="poster" style="width:100px;height:150px;border-radius:10px;"><br>
|
| 49 |
+
<b>{new_movies.iloc[idx]['title']}</b>
|
| 50 |
+
</div>
|
| 51 |
+
""",
|
| 52 |
+
unsafe_allow_html=True
|
| 53 |
+
)
|
| 54 |
+
recommend_for_idx = movie_indices[0]
|
| 55 |
+
st.markdown(f"### Recommendations for {new_movies.iloc[recommend_for_idx]['title']}:")
|
| 56 |
+
recs = annoy_index.get_nns_by_item(recommend_for_idx, 6)[1:]
|
| 57 |
+
rec_cols = st.columns(min(len(recs), 5))
|
| 58 |
+
for i, r in enumerate(recs):
|
| 59 |
+
with rec_cols[i]:
|
| 60 |
+
st.markdown(
|
| 61 |
+
f"""<div style="border-radius:15px;border:2px solid #eee;padding:10px;text-align:center;">
|
| 62 |
+
<img src="{get_poster(r)}" alt="poster" style="width:100px;height:150px;border-radius:10px;"><br>
|
| 63 |
+
<b>{new_movies.iloc[r]['title']}</b>
|
| 64 |
+
</div>
|
| 65 |
+
""",
|
| 66 |
+
unsafe_allow_html=True
|
| 67 |
+
)
|