Let's Build a Movie Recommendation App Using OpenAI & Python!
Tags: python, ai, beginners, tutorial
Published: 2023-09-22 12:23:30

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. 

![movie recommendation app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ih0wfuq8cem11wv3h5t.png)

**Let's Get Started!**

## Pre-Requisites:
- Free [SingleStore cloud account](https://www.singlestore.com/cloud-trial/?utm_campaign=singlestore-for-ai&utm_medium=referral&utm_source=pavan&utm_term=pavanmovierec) so you can access the Notebook feature
- Python 3.x installed
- OpenAI's API key. Where can you find your API Key? [Here](https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key)

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](https://dev.to/pavanbelagatti/jupyter-notebooks-tutorial-for-beginners-12a8), 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.
![singlestore notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xobayhhnj2mr4r87frf1.png)

Create a blank Notebook.
![blank notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtf05my08cpanvyvhpb4.png)

### 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. 

![Install numpy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flu4w755hm7trm7cxtk3.png)

### Steps:
#### 1. Import Libraries

```
import openai
import pandas as pd
import numpy as np
```
![ss playground](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49d85c1q860tokpr6dg2.png)

#### 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])
```

![movies added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y26suelk8kkj2es4kujk.png)

```
import pandas as pd

df_data = pd.read_csv("movies.csv")
df_data
```

![movie list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f60kzufqvyfcmseyldn.png)

#### 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]

![movie genre](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nlbkiow0o5a2kh3x3rbu.png)

#### 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")
```

![drama movies](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yy5px0mnu2v3srxzaogn.png)
#### 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)
```
![user input movies](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ng4b1rax77c8iqw2e0b.png)

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. 

If you are really interested in more such tutorials, I have the below ones for you to try.
{% embed https://dev.to/pavanbelagatti/learn-how-to-build-a-langchain-audio-app-with-python-in-just-5-minutes-n4o %}

{% embed https://dev.to/pavanbelagatti/sentiment-analysis-using-python-a-beginner-friendly-tutorial-34da %}