In today's digital age, personalization is key. Whether you're browsing an online store or searching for a movie to watch, tailored recommendations enhance user experience. In this tutorial, we'll embark on a journey to create a simple movie recommendation app using OpenAI, Python and SingleStore's Notebook feature. Harness the power of cutting-edge language models to provide movie suggestions based on user interests.
Let's Get Started!
Pre-Requisites:
Free SingleStore cloud account so you can access the Notebook feature
Python 3.x installed
OpenAI's API key. Where can you find your API Key? Here
We will be using SingleStore Notebook feature as our playground to execute all our commands inside. I'll show you how in the tutorial:)
What is SingleStore Notebooks?
Notebooks have become increasingly popular in the data science community as they provide an efficient way to explore, analyze and visualize data, making it easier to communicate insights and results. SingleStore's Notebook feature is based on the popular Jupyter Notebook, which is widely used in data science and machine learning communities.
One interesting fact about SingleStore Notebooks is that, they allow users to query SingleStore's distributed SQL database directly from within the notebook interface.
Let's get started with our tutorial, Once you signup, go to the Notebooks tab and create a blank Notebook.
Create a blank Notebook.
Libraries:
openai - to interface with OpenAI's API pandas - for data manipulation numpy - for numerical operations
First, you'll need to install the libraries if you haven't already:
pip install openai pandas numpy
Like I said above, we will add this command in our SingleStore Notebook's playground. Also, make sure you run the cell every time you add any new command in your Notebook.
Steps:
1. Import Libraries
import openai
import pandas as pd
import numpy as np
2. Load Data
Let's load movie names
import csv
import random
movie_names = ["The Godfather", "Casablanca", "Star Wars: A New Hope", "Inception", "Pulp Fiction", "Schindler's List", "Gone with the Wind",
"Shawshank Redemption", "The Matrix", "Jaws", "Jurassic Park", "Citizen Kane", "Avatar", "The Dark Knight", "Forrest Gump",
"Fight Club", "Titanic", "E.T. the Extra-Terrestrial", "2001: A Space Odyssey", "The Silence of the Lambs", "Goodfellas",
"To Kill a Mockingbird", "The Wizard of Oz", "Saving Private Ryan", "The Lord of the Rings: The Fellowship of the Ring",
"Terminator 2: Judgment Day", "One Flew Over the Cuckoo's Nest", "The Sixth Sense", "Psycho", "The Social Network", "Apocalypse Now",
"Rear Window", "Braveheart", "The Lion King", "The Shining", "Toy Story", "Memento", "La La Land", "The Departed", "Black Panther",
"The Avengers", "Indiana Jones: Raiders of the Lost Ark", "Rocky", "Amélie", "Alien", "The Good, the Bad and the Ugly",
"The Big Lebowski", "Inglourious Basterds", "The Princess Bride", "The Graduate"]
with open('movies.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["MovieName"])
for movie in movie_names:
writer.writerow([movie])
import pandas as pd
df_data = pd.read_csv("movies.csv")
df_data
3. Initialize OpenAI API
Replace 'your-api-key-here' with your actual API key.
openai.api_key = "your-api-key-here"
4. Map each movie to its category
# Dictionary mapping movie names to categories
movie_categories = {
"The Godfather": "Drama",
"Casablanca": "Romance",
"Star Wars: A New Hope": "Sci-Fi",
"Inception": "Sci-Fi",
"Pulp Fiction": "Crime",
"Schindler's List": "Drama",
"Gone with the Wind": "Romance",
"Shawshank Redemption": "Drama",
"The Matrix": "Sci-Fi",
"Jaws": "Thriller",
"Jurassic Park": "Adventure",
"Citizen Kane": "Drama",
"Avatar": "Sci-Fi",
"The Dark Knight": "Action",
"Forrest Gump": "Drama",
"Fight Club": "Drama",
"Titanic": "Romance",
"E.T. the Extra-Terrestrial": "Family",
"2001: A Space Odyssey": "Sci-Fi",
"The Silence of the Lambs": "Thriller",
"Goodfellas": "Crime",
"To Kill a Mockingbird": "Drama",
"The Wizard of Oz": "Family",
"Saving Private Ryan": "War",
"The Lord of the Rings: The Fellowship of the Ring": "Fantasy",
"Terminator 2: Judgment Day": "Action",
"One Flew Over the Cuckoo's Nest": "Drama",
"The Sixth Sense": "Thriller",
"Psycho": "Horror",
"The Social Network": "Drama",
"Apocalypse Now": "War",
"Rear Window": "Thriller",
"Braveheart": "War",
"The Lion King": "Family",
"The Shining": "Horror",
"Toy Story": "Family",
"Memento": "Thriller",
"La La Land": "Musical",
"The Departed": "Crime",
"Black Panther": "Action",
"The Avengers": "Action",
"Indiana Jones: Raiders of the Lost Ark": "Adventure",
"Rocky": "Sports",
"Amélie": "Romantic Comedy",
"Alien": "Sci-Fi",
"The Good, the Bad and the Ugly": "Western",
"The Big Lebowski": "Comedy",
"Inglourious Basterds": "War",
"The Princess Bride": "Fantasy",
"The Graduate": "Drama"
}
[Note: Make sure you run the cell when you map these movies to their respective categories]
5. Let's ask our app to recommend some 'Drama' movies.
def recommend_movies(genre):
# Find movies of the given genre
recommendations = [movie for movie, category in movie_categories.items() if category == genre]
if recommendations:
print(f"Recommended {genre} movies:")
for rec in recommendations:
print(f"- {rec}")
else:
print(f"Sorry, no movies of the {genre} genre found.")
# Test the function
recommend_movies("Drama")
6. Let's ask user input this time
# Get user input for the genre
user_genre = input("What type of movie are you in the mood for? ")
# Recommend movies based on user input
recommend_movies(user_genre)
In wrapping up, we've journeyed through the simplicity and power of combining OpenAI with Python to craft a personalized movie recommendation engine. While our approach is straightforward, it offers a glimpse into the vast potential of AI-driven solutions in enhancing user experiences. With just a few lines of code, we've transformed a basic query into tailored movie suggestions.