Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
def greet(name):
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# def greet(name):
|
| 4 |
+
# return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
import numpy as np
|
| 8 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
# Load pre-trained SentenceTransformer model
|
| 11 |
+
embedding_model = SentenceTransformer("thenlper/gte-large")
|
| 12 |
+
|
| 13 |
+
# Example dataset with genres (replace with your actual data)
|
| 14 |
+
dataset = load_dataset("hugginglearners/netflix-shows")
|
| 15 |
+
|
| 16 |
+
# Combine description and genre for embedding
|
| 17 |
+
def combine_description_title_and_genre(description, listed_in, title):
|
| 18 |
+
return f"{description} Genre: {listed_in} Title: {title}"
|
| 19 |
+
|
| 20 |
+
# Generate embedding for the query
|
| 21 |
+
def get_embedding(text):
|
| 22 |
+
return embedding_model.encode(text)
|
| 23 |
+
|
| 24 |
+
# Vector search function
|
| 25 |
+
def vector_search(query):
|
| 26 |
+
query_embedding = get_embedding(query)
|
| 27 |
+
|
| 28 |
+
# Generate embeddings for the combined description and genre
|
| 29 |
+
embeddings = np.array([get_embedding(combine_description_title_and_genre(item["description"], item["listed_in"],item["title"])) for item in dataset])
|
| 30 |
+
|
| 31 |
+
# Calculate cosine similarity between the query and all embeddings
|
| 32 |
+
similarities = cosine_similarity([query_embedding], embeddings)
|
| 33 |
+
|
| 34 |
+
# Adjust similarity scores based on ratings
|
| 35 |
+
ratings = np.array([item["rating"] for item in dataset])
|
| 36 |
+
adjusted_similarities = similarities * ratings.reshape(-1, 1)
|
| 37 |
+
|
| 38 |
+
# Get top N most similar items (e.g., top 3)
|
| 39 |
+
top_n = 3
|
| 40 |
+
top_indices = adjusted_similarities[0].argsort()[-top_n:][::-1] # Get indices of the top N results
|
| 41 |
+
top_items = [dataset[i] for i in top_indices]
|
| 42 |
+
|
| 43 |
+
# Format the output for display
|
| 44 |
+
search_result = ""
|
| 45 |
+
for item in top_items:
|
| 46 |
+
search_result += f"Title: {item['title']}, Description: {item['description']}, Genre: {item['listed_in']}, Rating: {item['rating']}\n"
|
| 47 |
+
|
| 48 |
+
return search_result
|
| 49 |
+
|
| 50 |
+
# Gradio Interface
|
| 51 |
+
def movie_search(query):
|
| 52 |
+
return vector_search(query)
|
| 53 |
+
|
| 54 |
+
iface = gr.Interface(fn=movie_search,
|
| 55 |
+
inputs="text",
|
| 56 |
+
outputs="text",
|
| 57 |
+
live=True,
|
| 58 |
+
title="Netflix Recommendation System",
|
| 59 |
+
description="Enter a query to get Netflix recommendations based on description and genre.")
|
| 60 |
+
|
| 61 |
+
iface.launch()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 65 |
+
# demo.launch()
|