Building a Personalized Playlist Generator with Lyzr Automata
Tags: ai, playlist, lyzr, songs
Published: 2024-04-24 14:39:58

Are you tired of endlessly scrolling through music streaming platforms trying to find the perfect playlist? Look no further! In this post, we’ll walk through how to create a personalized playlist generator using **Lyzr Automata**, **Streamlit**, and the **OpenAI GPT-4 model**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0ky73qadg4gtt8dfvnu.png)

Imagine having your own personal DJ who curates a playlist tailored specifically to your music taste. With advancements in artificial intelligence and natural language processing, this is now a reality. In this tutorial, we’ll leverage **Lyzr Automata**, a powerful AI toolkit, to build a personalized playlist generator.

**Why use Lyzr SDK’s?**

With **Lyzr SDKs**, crafting your own GenAI application is a breeze, requiring only a few lines of code to get up and running swiftly.

[Checkout the Lyzr SDK’s](https://docs.lyzr.ai/homepage)

**Lets get Started!**

Create a new file **app.py** and use that

**Getting Started**
To get started, make sure you have Python installed on your system along with Streamlit and the necessary dependencies. You can install Streamlit using pip

We’ll also need an API key from **OpenAI** to access their GPT-4 model for text completion. Once you have your API key, set it as an environment variable:

```
import os
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
```
**Creating the Streamlit App**
We start by importing the required libraries and setting up the Streamlit app. We’ll also load our Lyzr Automata models and define the interface for users to input their music preferences.

```
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent, Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from lyzr_automata.tasks.task_literals import InputType, OutputType
```

**Generating the Playlist**
Once the user inputs their preferences, we’ll utilize Lyzr Automata to generate a personalized playlist. We define a function playlist_generation that takes the user input, interacts with our AI model, and returns the generated playlist.


```
input = st.text_input("Enter your favourite songs, your genre or your favourite artists : ",placeholder=f"""Type here""")

open_ai_text_completion_model = OpenAIModel(
    api_key=st.secrets["apikey"],
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)
def playlist_generation(code):
    generator_agent = Agent(
        role="PLAYLIST CURATOR expert",
        prompt_persona=f"Your task is to CREATE a CUSTOMIZED playlist that PERFECTLY aligns with the user's musical preferences."
    )

    prompt = f"""
You are an Expert PLAYLIST CURATOR and MUSIC ENTHUSIAST. Your task is to CREATE a CUSTOMIZED playlist that PERFECTLY aligns with the user's musical preferences.

To execute this task EFFECTIVELY, follow these steps:

1. COLLECT detailed insights regarding the user's FAVORITE SONGS, GENRES, and ARTISTS.

2. EXAMINE the gathered information to identify commonalities in BEATS, MELODIES, and THEMES.

3. SEARCH for tracks that ALIGN with these patterns, including both popular anthems and undiscovered gems.

4. ASSEMBLE a selection of 10-30 TRACKS that resonate with the user's taste while ensuring a seamless auditory journey.

5. SEQUENCE the songs strategically to maintain ENGAGEMENT, balancing energy levels throughout the playlist.

6. EVALUATE your choices for DIVERSITY and HARMONY to ensure each track enhances the collective mood.

7. TITLE the playlist CREATIVELY and DISPLAY it in an attractive format exclusively featuring the playlist title and songs.

You MUST ONLY display the CUSTOMIZED PLAYLIST TITLE followed by the song list.
    """
```
  

```
      generator_agent_task = Task(
        name="Playlist Generation",
        model=open_ai_text_completion_model,
        agent=generator_agent,
        instructions=prompt,
        default_input=code,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
    ).execute()

    return generator_agent_task
```
**Displaying the Playlist**
Finally, we add a button to trigger the playlist generation process. Once the user clicks the button, the generated playlist will be displayed on the app.

```
if st.button("Generate!"):
    solution = playlist_generation(input)
    st.markdown(solution)Conclusion
```
We’ve demonstrated how to build a personalized playlist generator using Lyzr Automata, Streamlit, and OpenAI’s GPT-4 model. With this powerful combination of tools, you can create a seamless and enjoyable music discovery experience for users. Feel free to explore further customization and enhancements to make your playlist generator even more dynamic and user-friendly!

**App link**: https://playlistgenerator-lyzr.streamlit.app/

**Source Code**:https://github.com/isakshay007/Playlist_Generator

**Connect with Lyzr**
To learn more about **Lyzr and its SDK’s**, visit our website or get in touch with our team:

**Website**: [Lyzr.ai](https://www.lyzr.ai/)
**Book a Demo**: [Book a Demo](https://www.lyzr.ai/book-demo/)
**Discord**: [Join our Discord community](https://discord.com/invite/nm7zSyEFA2)
**Slack**: [Join our Slack channel](https://anybodycanai.slack.com/join/shared_invite/zt-2a7fr38f7-_QDOY1W1WSlSiYNAEncLGw#/shared-invite/email)