Spotify + AI = Tune Genie
Published: 2024-05-12 09:57:16

## Inspiration

Even though I enjoy music, I don't listen to it as frequently as most people do. I also tend not to explore new music extensively because I prefer avoiding disappointing tracks. In contrast, my girlfriend is a true music enthusiast who listens to a wide variety of genres from around the world on a regular basis. She played [SZA by Snooze](https://www.youtube.com/watch?v=LDY_XyxBu8A) in the background during one of our evening conversations, and I must say, I thoroughly enjoyed the song. After replaying the song continuously for about a week, I decided to search for similar tracks. I found it frustrating to look up songs individually and then add them one by one to a playlist. This process seemed unnecessarily tedious. As a software engineer, I began to think about how to simplify the entire process of getting song suggestions, searching for songs, and creating playlists. This is how Tune Genie was born.

<img width="500%" style="width:100%" src="https://media.giphy.com/media/l4FGlB1HXyee6oxtm/giphy.gif">

## Requirement clarification
 
### Functional requirements

**Songlist Suggestions**

- Users should be able to provide minimal input, such as country, genre, mood, activity, and year, to receive song list suggestions.
- Users should be able to view the generated list of suggested songs.

**Playlists**

- Users should be able to import the suggested song list into a music service and save it as a playlist.
- Users should be able to view all playlists they have created.
- Users should have direct access to their playlists within the music service through the app.
- Users should be able to preview the songs in any created playlist.

**Packages**

- Users can view various available packages within the app.
- Packages are tiered based on price, offering different levels of service.
- Each package specifies the number of playlists a user can create each month and the maximum number of songs per playlist.

**Subscriptions**

- Users can subscribe to services by choosing from the available packages within the app.
- The application should manage user subscriptions and automatic billing (auto-debits).

**Non-Functional Requirements**

- Music Search Efficiency: Searches within the application should be fast, providing users with prompt results.
- Songlist Suggestions: Suggestions should appear random yet strictly adhere to user inputs such as country, genre, mood, activity, and year.
- User-Application Integration: Interaction between the user and the application should be seamless and efficient, with minimal latency.
- Page Load Speed: Pages should load quickly to ensure a smooth user experience.
- Traffic Management: The system must efficiently handle large volumes of requests to core APIs without degradation in performance.
- External Service Protection: Rate limiting and caching should be implemented to protect external services from excessive calls.
- Data Security: Third-party user data must be securely handled and protected against unauthorized access.
- Token and Permission Security: Third-party tokens and permissions should adhere to the principle of least privilege, ensuring that the application only has access necessary for functionality.


**High level design**

The high-level design outlines the core components of the application, as illustrated in the diagram below. Users interact with the application through the frontend interface, which supports various actions such as logging in, searching, and viewing playlists.

The frontend communicates with multiple backend APIs to execute logic tailored to different user scenarios. The backend facilitates numerous interactions with external services, including:

- Federated Login: Integrations with services like Spotify to manage user authentication.
- Music Service Provision: Connections to music services such as Spotify to fetch and manage music content.
- Payment Processing: Integration with PayPal to handle transactions related to packages and subscriptions.
- Song list Suggestions: Utilization of a large language machine learning model to generate music suggestions based on user input.
- Data Management: A well structured data store to maintain persistent state information for users.

These components work in tandem to ensure a seamless and efficient user experience across the application.

![High level design](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzkv1jljdkf2tovezdh7.png)

## Tools
**Node**
I opted for the JavaScript ecosystem, using Node.js and Koa.js for the backend framework, largely due to my familiarity and comfort with these technologies. My extensive experience with Node.js in numerous projects has given me a solid understanding of its architecture and capabilities. Node.js is particularly well-suited for handling I/O-intensive operations, making it an ideal choice for this application, which requires frequent interactions with multiple external services. The efficiency and ease of building with Node.js further reinforce its suitability for managing the demands of this service.

**React**
I selected React.js, specifically Next.js, as the frontend framework for this application primarily for its server-side rendering capabilities and support for page caching. These features are critical to meeting the non-functional requirements related to enhancing user experience, such as fast page loads and efficient data handling.
![view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwi621ghhabi80kzlo2x.PNG)

Also, my familiarity with React.js and Next.js significantly influenced this choice. These technologies streamline the development process, making it easier to build and maintain the application due to their flexible ecosystem and comprehensive documentation.


![view1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3vr8ylilxfjj4ad5afm.PNG)



I was faced with the decision of whether to develop a native application or a web application. I opted for a hybrid approach by choosing [Next-pwa](https://www.npmjs.com/package/next-pwa), which is built on  [Workbox](https://developer.chrome.com/docs/workbox/). This tool allows for a native-like experience by enabling users to bookmark the web application on their smartphones as if it were a native app, while also providing a full-screen view without the browser controls and panels. This solution bridges the gap between web and native applications, combining the accessibility of web technology with the immersive user experience of a native app.

**Github**
I use github because its easy and available. I personally like the workflow functionality that manages CI/CD for me. Most other repository managers have this too.

<img width="500%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExajFiNDdycWNwMWdkbnBsZDM2Mnh0ZnFidWZyNTQ0M2NyZjRiZXRjdyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/du3J3cXyzhj75IOgvA/giphy.gif">

**GCP**
Currently, my primary cloud provider is Google Cloud Platform (GCP), chosen mainly due to my familiarity with its services and controls. Within GCP, I have selected Cloud Run to host both my frontend and backend. Cloud Run is particularly well-suited for my needs as it allows me to specify the minimum number of instances and scales horizontally to accommodate varying loads. This level of horizontal scaling control is adequate for my requirements, as I do not need the finer granularity of management that Kubernetes or a dedicated virtual machine setup might offer in a microservices architecture.

**Paypal**
I selected PayPal as our payment processor due to its widespread popularity and comprehensive documentation. PayPal offers an easy-to-use abstraction layer for managing payments, packages, and subscriptions, which significantly simplifies implementation. In addition, like many payment systems, PayPal utilizes the saga pattern to manage long-running payment processes. This is achieved through the use of webhooks, which allow our application to respond appropriately to various payment events, ensuring smooth and reliable transaction handling.

<img width="500%" style="width:100%" src="https://media.giphy.com/media/455s3y6flyHgljVahL/giphy.gif">


**ChatGPT**
For the development of Tune Genie, a crucial component was integrating a language learning model (LLM) to enhance interaction capabilities and provide dynamic content responses. After evaluating various options, ChatGPT, specifically its underlying model gpt-3.5-turbo, was selected for this purpose. 

<img width="500%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExdThzbXp5cDFzNGN0dm14dzVkNzRvcXllOTgwcWs2NDBmMGFtZ2N6biZlcD12MV9naWZzX3NlYXJjaCZjdD1n/PltDTEIBLucQdn6vfO/giphy.gif">

- **Model Choice:**
GPT-3.5 Turbo: This model excels in delivering high-quality outputs at a faster pace than its predecessors, such as text-davinci-003, aligning with the speed requirements outlined in our non-functional requirements. It is particularly well-suited for Tune Genie's specific needs, which include generating a diverse song list tailored to user preferences. Among the three models evaluated, including text-davinci-003 and Ada, GPT-3.5 Turbo not only yielded the best results but also efficiently formatted these results into JSON objects. This capability to process moderately complex queries quickly ensures that user interactions are both seamless and precise, enhancing the overall user experience.

<img width="500%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZTdndzVzcXpmbjNjN2UzcWR3ZXp1aXBzYjJobnZuemYwbjJkdnBkOCZlcD12MV9naWZzX3NlYXJjaCZjdD1n/cRH5deQTgTMR2/giphy.gif">

**Mitigating Drawbacks and Limitations:**

Integrating GPT-3.5 Turbo into the Tune Genie application provides a good example of how understanding specific use-case requirements can mitigate some common drawbacks associated with using advanced AI models. Here’s how the specific context of Tune Genie aligns with the limitations of GPT-3.5 Turbo and how it addresses them:

- **Privacy Concerns**: In the context of Tune Genie, where queries do not need to be private and are primarily related to generating music suggestions, the typical data privacy issues associated with sending data to an external AI service are less concerning. This alleviates major privacy worries as the data involved is non-sensitive and user-specific confidential data is not being processed.

- **General-Purpose Model Suitability**: The use of GPT-3.5 Turbo as a general-purpose model is particularly advantageous for Tune Genie. Since the application’s core functionality—generating music suggestions—is not heavily specialized, the model’s general capabilities are sufficient and effective. This avoids the need for specialized AI models, which can be more complex and costly to develop and maintain.

- **Cost-to-Efficiency Ratio**: 
For Tune Genie, the cost-to-efficiency ratio of employing GPT-3.5 Turbo is highly advantageous. Despite the inherent costs associated with this model, its value is clearly demonstrated through the efficiency and quality of its outputs—especially with respect to the speed and relevance of music suggestions. This alignment ensures that it remains a cost-effective solution for the application's requirements, effectively balancing performance with operational expenses. Also, the subscription packages available to users not only extend greater access to the GPT-3.5 Turbo but also assist the Tune Genie team in managing these costs more effectively.


<img width="500%" style="width:100%" src="https://media.giphy.com/media/uyWTOgNGGWfks/giphy.gif?cid=790b7611f3eg81v4j45gvw57ta26apsnb8fe2oei33jijft7&ep=v1_gifs_search&rid=giphy.gif&ct=g">

- **Handling of Rate Limits and Vendor Lock-In**: While GPT-3.5 Turbo is subject to rate limits, Tune Genie has implemented well thought out caching strategies to effectively manage these constraints. By caching commonly requested data and responses, the application reduces the number of API calls necessary, thereby minimizing the impact of rate limits. This approach not only ensures smoother user experiences during high-demand periods but also optimizes API usage to stay within the operational capacity that GPT-3.5 Turbo supports.
Additionally, the use of caching as part of the application’s architecture contributes significantly to managing operational efficiency. It reduces the dependency on real-time API calls for every user interaction, thus not only conserving resources but also enhancing performance by providing faster response times. This strategic use of technology underscores a thoughtful approach to mitigating the risks associated with vendor lock-in and the limitations imposed by rate limits, ensuring that Tune Genie can deliver a consistent and reliable service to its users.


**Music service**
The Tune Genie team plans to begin by integrating with Spotify and later expand to include additional music services such as Apple Music. The choice to start with Spotify was driven primarily by the team's familiarity with the platform and the relative ease of implementation. Spotify offers great APIs for music search, music preview, and playlist creation, which align well with Tune Genie's functionalities. In addition, incorporating Spotify for federated login significantly simplifies the user onboarding process, enhancing the overall user experience by streamlining access and interaction.


## Expanding high level design
The [diagram](https://drive.google.com/file/d/13d6KnrWsEovgHX7qHuPM1JLee2uJ0bz9/view?usp=sharing) below details the interaction between all core components of the system.


![Detailed design](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nvfu8iwi8vjyl96nfsl.png)


### Registration and authentication ceremony

![Registration ceremony](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n73jcvi82aq3gmgqefhs.png)
The image above explains the registration and authentication ceremony between the different layers as well as the the external service (spotify). Tune Genie leverages federated login from Spotify in addition to extracting vital user information such as spotify IDs, package types and emails. The information along with the tokens provided by spotify are used to perform actions in spotify on behalf of the user

![view2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dj2amqmn4um24zrkyzuk.PNG)




### Core functionality

![Core functionality](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqudz8ov7qbf5bz6sjnv.png)
The image above explains the core functionality or functional requirements of Tune Genie. It provides a guided sequence of user actions from input entry to playlist generation. It also sheds light on the system's integration with chatGpt and the involvement of spotify's playlist widget.

![view2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2k8wk57d1xf15qigs5t.jpeg)



### Payment and subscription

![payment and subscription](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ez6a6gih2t652ml48uxc.png)
The image above explains the relationship between Tune Genie and the chosen payment subscription service. Though Tune Genie a number of trial song list generation attempts, it also provides a subscription model for users may want decide to use the service beyond the number of trial attempts. Tune Ginie not only leverages the payment gateway of paypal to execute one time payments, it also uses paypals subscription model to track subscription statuses and obtain recurring payments from users. 

![view3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs8runjo7zm08rg6ddvj.PNG)



### Why it never shipped

Spotify offers a development platform where engineers can register their application to obtain a client ID and API key. However, these credentials do not grant unrestricted access to user accounts. To perform actions on behalf of users within the sandbox environment of limited users, applications must whitelist user emails to enable access to user tokens during the registration or authentication process through Spotify's federated login. As part of this, to increase the allowed quota, Spotify requires developers to apply for an extension, justifying the use of their service within the application. My initial application was criticized for errors in importing artist album artwork. Although I addressed these issues, my subsequent application was met with a warning against using Spotify's content with any large language models (LLMs) or AI tools. This effectively meant whole application had to be build on the back of another platform.

<img width="500%" style="width:100%" src="https://media.giphy.com/media/LMEyKCRaNgzZhalkHm/giphy.gif?cid=ecf05e47qhlv60l4tm6kw7uc2gppn988u3foq9jfstm6kwbz&ep=v1_gifs_search&rid=giphy.gif&ct=g">


###Lessons
While I am proud to have single-handedly built this entire application, some thorough research about Spotify's policies regarding AI tools would have better set my expectations before I began this engineering journey.

During the development process, I gained valuable experience building a Progressive Web App (PWA) and explored various enhancements to improve its performance. Optimizing the front end to be lightweight, fast, and fluid involved a strategic combination of managing configurations, implementing effective caching, and minimizing backend requests to only those necessary.

I had the opportunity to work with GitHub Actions, which proved to be a seamless and enjoyable tool, especially when integrated with Google Cloud Platform's Cloud Builds.

A completely new challenge for me was implementing PayPal's payment and subscription models. Although it was somewhat complicated, I am grateful for the experience of integrating complex payment processes and record-keeping systems into the application.

###Conclusion

In conclusion, developing Tune Genie was an exciting mix of passion and technology aimed at making music discovery easier. This project tackled a common issue for music lovers and used somewhat advanced technology to create a smooth and engaging experience. The app combines different requirements to be efficient, quick, and user friendly. Even though there were challenges with Spotify and using AI models, Tune Genie sets a meaningful precedence similar apps. Tune Genie shows that combining music with LLMs opens up exciting ways to enhance how we find and enjoy music. See you in the next one 😁

<img width="500%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExeDc0cWF2cnQ0cGJiN2Zwajc1dXZ0eWdpZmhkOHltOTMzeWEyanFhNCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/fnKgEwqbYM6YCGmTad/giphy.gif">