SohaAyub commited on
Commit
4549d03
·
verified ·
1 Parent(s): 2b7cbb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
- import torch
3
  from sentence_transformers import SentenceTransformer, util
4
  import pandas as pd
 
5
 
6
  # -- static dataset (10 items) --
7
  movies = [
@@ -20,7 +20,7 @@ movies = [
20
  df = pd.DataFrame(movies)
21
  df["title_lower"] = df["title"].str.lower()
22
 
23
- # -- load sentence transformer --
24
  MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
25
  model = SentenceTransformer(MODEL_NAME)
26
 
@@ -35,18 +35,19 @@ def recommend_movie(movie_title):
35
 
36
  idx = df.index[df["title_lower"] == movie_title][0]
37
  query_emb = movie_embeddings[idx]
38
-
 
39
  similarity = util.cos_sim(query_emb, movie_embeddings)[0]
40
- top_idxs = torch.topk(similarity, k=6).indices.tolist() # 6 to exclude itself
41
-
42
- recs = []
43
- for i in top_idxs:
44
- if i != idx:
45
- recs.append(df.iloc[i]["title"])
46
-
47
  return "\n".join(recs)
48
 
49
- # -- Gradio Interface --
50
  interface = gr.Interface(
51
  fn=recommend_movie,
52
  inputs=gr.Textbox(label="Enter Movie title"),
 
1
  import gradio as gr
 
2
  from sentence_transformers import SentenceTransformer, util
3
  import pandas as pd
4
+ import torch
5
 
6
  # -- static dataset (10 items) --
7
  movies = [
 
20
  df = pd.DataFrame(movies)
21
  df["title_lower"] = df["title"].str.lower()
22
 
23
+ # -- load sentence transformer model --
24
  MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
25
  model = SentenceTransformer(MODEL_NAME)
26
 
 
35
 
36
  idx = df.index[df["title_lower"] == movie_title][0]
37
  query_emb = movie_embeddings[idx]
38
+
39
+ # cosine similarity
40
  similarity = util.cos_sim(query_emb, movie_embeddings)[0]
41
+
42
+ # get top 6 indices (including the movie itself)
43
+ top_idxs = torch.topk(similarity, k=6).indices.tolist()
44
+
45
+ # exclude the original movie
46
+ recs = [df.iloc[i]["title"] for i in top_idxs if i != idx]
47
+
48
  return "\n".join(recs)
49
 
50
+ # -- Gradio interface --
51
  interface = gr.Interface(
52
  fn=recommend_movie,
53
  inputs=gr.Textbox(label="Enter Movie title"),