Views
No views yet
1!pip install pandas torch sentence-transformers scikit-learn
21from sentence_transformers import SentenceTransformer, InputExample, losses, util
2import torch
3
4# Load model
5device = 'cuda' if torch.cuda.is_available() else 'cpu'
6model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', device=device)
7
8# pass the movie name
9recommend_by_movie_name("Toy Story")
10
11
12# Recommend Movies
13def recommend_by_movie_name(movie_name, top_k=5):
14 titles = movie_subset["title"].tolist()
15 matches = get_close_matches(movie_name, titles, n=1, cutoff=0.6)
16
17 if not matches:
18 print(f"❌ Movie '{movie_name}' not found in dataset.")
19 return
20
21 matched_title = matches[0]
22 movie_index = movie_subset[movie_subset["title"] == matched_title].index[0]
23
24 query_embedding = movie_embeddings[movie_index]
25 scores = util.pytorch_cos_sim(query_embedding, movie_embeddings)[0]
26 top_results = torch.topk(scores, k=top_k + 1)
27
28 print(f"\n🎬 Recommendations for: {matched_title}")
29 for score, idx_tensor in zip(top_results[0][1:], top_results[1][1:]): # skip itself
30 idx = idx_tensor.item() # ✅ Convert tensor to int
31 title = movie_subset.iloc[idx]["title"]
32 print(f" {title} (Score: {score:.4f})")
33Movie-Lens dataset. It contains 20,000 movies and their genres.epochhalf() precision (FP16) to reduce model size and inference time.1.
2├── quantized-model/ # Contains the quantized model files
3│ ├── config.json
4│ ├── model.safetensors
5│ ├── tokenizer_config.json
6│ ├── modules.json
7│ └── special_tokens_map.json
8│ ├── sentence_bert_config.jason
9│ └── tokenizer.json
10│ ├── config_sentence_transformers.jason
11│ └── vocab.txt
12
13├── README.md # Model documentation