Smiley0707 commited on
Commit
9c1217e
·
verified ·
1 Parent(s): 0282be4

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +58 -31
src/streamlit_app.py CHANGED
@@ -1,40 +1,67 @@
1
- import altair as alt
 
2
  import numpy as np
3
  import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
 
 
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
 
 
 
 
 
 
18
 
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
 
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
 
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
 
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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
+ )