- {% endif %}
+
{% endblock %}
diff --git a/core/views.py b/core/views.py
index 1a7838b..ee94801 100644
--- a/core/views.py
+++ b/core/views.py
@@ -2,6 +2,23 @@ from django.shortcuts import render
from django.http import JsonResponse
from ai.local_ai_api import LocalAIApi
import json
+from django.conf import settings
+from googleapiclient.discovery import build
+
+def youtube_search(query):
+ if not settings.YOUTUBE_API_KEY or settings.YOUTUBE_API_KEY == "REPLACE_WITH_YOUR_YOUTUBE_API_KEY":
+ return None
+ youtube = build('youtube', 'v3', developerKey=settings.YOUTUBE_API_KEY)
+ request = youtube.search().list(
+ q=query,
+ part='snippet',
+ maxResults=1,
+ type='video'
+ )
+ response = request.execute()
+ if response['items']:
+ return response['items'][0]['id']['videoId']
+ return None
def get_playlist_for_mood(mood):
"""
@@ -41,7 +58,7 @@ def index(request):
response = LocalAIApi.create_response({
"input": [
- {'role': 'system', 'content': 'You are a friendly AI that helps users find music based on their mood. Ask up to 6 questions to understand their mood. Once you have determined the mood, respond with ONLY a JSON object with a single key "mood" and the mood as the value (e.g. {"mood": "happy"}).'},
+ {'role': 'system', 'content': 'You are a friendly AI that helps users find music based on their mood. Ask up to 6 questions to understand their mood. Once you have determined the mood, respond with ONLY a JSON object with a single key "playlist" and the value as a list of songs, where each song is a JSON object with "title" and "artist" as keys (e.g. {"playlist": [{"title": "Happy", "artist": "Pharrell Williams"}]}).'},
*conversation
],
})
@@ -49,10 +66,12 @@ def index(request):
ai_message = 'Sorry, I had an error.'
if response.get("success"):
json_response = LocalAIApi.decode_json_from_response(response)
- if json_response and 'mood' in json_response:
- mood = json_response['mood']
- playlist = get_playlist_for_mood(mood)
- ai_message = f"I've created a playlist for your {mood} mood. I hope you like it!"
+ if json_response and 'playlist' in json_response:
+ playlist = json_response['playlist']
+ for song in playlist:
+ video_id = youtube_search(f"{song['title']} {song['artist']}")
+ song['video_id'] = video_id
+ ai_message = "I've created a playlist for you. I hope you like it!"
else:
ai_message = LocalAIApi.extract_text(response)
@@ -67,4 +86,4 @@ def index(request):
else:
# Start of a new conversation
request.session['conversation'] = []
- return render(request, 'core/index.html', {'ai_message': 'Hi! How are you feeling today?'})
+ return render(request, 'core/index.html', {'ai_message': 'Hi! How are you feeling today?'})
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index e22994c..a2de096 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
Django==5.2.7
mysqlclient==2.2.7
python-dotenv==1.1.1
+google-api-python-client>=2.136.0
diff --git a/static/js/chat.js b/static/js/chat.js
index da7b297..b5f9315 100644
--- a/static/js/chat.js
+++ b/static/js/chat.js
@@ -77,7 +77,11 @@ document.addEventListener('DOMContentLoaded', function() {
playlist.forEach(song => {
const li = document.createElement('li');
li.classList.add('playlist-item');
- li.innerHTML = `${song.title} - ${song.artist}`;
+ if (song.video_id) {
+ li.innerHTML = ``;
+ } else {
+ li.innerHTML = `${song.title} - ${song.artist} (Video not found)`;
+ }
ul.appendChild(li);
});
diff --git a/staticfiles/js/chat.js b/staticfiles/js/chat.js
index da7b297..b5f9315 100644
--- a/staticfiles/js/chat.js
+++ b/staticfiles/js/chat.js
@@ -77,7 +77,11 @@ document.addEventListener('DOMContentLoaded', function() {
playlist.forEach(song => {
const li = document.createElement('li');
li.classList.add('playlist-item');
- li.innerHTML = `${song.title} - ${song.artist}`;
+ if (song.video_id) {
+ li.innerHTML = ``;
+ } else {
+ li.innerHTML = `${song.title} - ${song.artist} (Video not found)`;
+ }
ul.appendChild(li);
});