Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
import faiss
|
4 |
+
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
from gradio.components import Label
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
# Load the dataset
|
11 |
+
dataset = datasets.load_dataset("SandipPalit/Movie_Dataset")
|
12 |
+
title = dataset['train']['Title']
|
13 |
+
overview = dataset['train']['Overview']
|
14 |
+
|
15 |
+
model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
|
16 |
+
|
17 |
+
vectors = model.encode(overview)
|
18 |
+
|
19 |
+
vector_dimension = vectors.shape[1]
|
20 |
+
index = faiss.IndexFlatL2(vector_dimension)
|
21 |
+
faiss.normalize_L2(vectors)
|
22 |
+
index.add(vectors)
|
23 |
+
|
24 |
+
def get_model_generated_vector(text):
|
25 |
+
search_vector = model.encode(text)
|
26 |
+
vector = np.array([search_vector])
|
27 |
+
faiss.normalize_L2(vector)
|
28 |
+
return vector
|
29 |
+
|
30 |
+
def find_top_k_matched(vector):
|
31 |
+
distances, ann = index.search(vector, k=5)
|
32 |
+
return [title[ann[0][0]], title[ann[0][1]], title[ann[0][2]], title[ann[0][3]], title[ann[0][4]]]
|
33 |
+
|
34 |
+
|
35 |
+
def movie_recommandation(text):
|
36 |
+
vector = get_model_generated_vector(text)
|
37 |
+
matches = find_top_k_matched(vector)
|
38 |
+
# print(matches)
|
39 |
+
return matches[0], matches[1], matches[2], matches[3], matches[4]
|
40 |
+
|
41 |
+
demo = gr.Interface(
|
42 |
+
fn=movie_recommandation,
|
43 |
+
inputs=gr.Textbox(placeholder="Enter the Movie Name"),
|
44 |
+
outputs=[Label() for i in range(5)],
|
45 |
+
examples=[["Scarlet Macaw on Perch"], ["horror"]])
|
46 |
+
|
47 |
+
demo.launch(debug=True)
|