diff --git a/assets/pasted-20251125-161531-9dca6399.png b/assets/pasted-20251125-161531-9dca6399.png new file mode 100644 index 0000000..d417e24 Binary files /dev/null and b/assets/pasted-20251125-161531-9dca6399.png differ diff --git a/config/__pycache__/settings.cpython-311.pyc b/config/__pycache__/settings.cpython-311.pyc index eaa0232..e1666d5 100644 Binary files a/config/__pycache__/settings.cpython-311.pyc and b/config/__pycache__/settings.cpython-311.pyc differ diff --git a/config/settings.py b/config/settings.py index 001b8c8..d69dbe4 100644 --- a/config/settings.py +++ b/config/settings.py @@ -20,6 +20,9 @@ load_dotenv(BASE_DIR.parent / ".env") SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "change-me") DEBUG = os.getenv("DJANGO_DEBUG", "true").lower() == "true" +YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY", "REPLACE_WITH_YOUR_YOUTUBE_API_KEY") + + # Trust proxy headers from Cloudflare Tunnel / Apache SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') USE_X_FORWARDED_HOST = True diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index bbf1405..a7d6ada 100644 Binary files a/core/__pycache__/views.cpython-311.pyc and b/core/__pycache__/views.cpython-311.pyc differ diff --git a/core/templates/core/index.html b/core/templates/core/index.html index 4d7fcc4..f2ad44b 100644 --- a/core/templates/core/index.html +++ b/core/templates/core/index.html @@ -33,18 +33,7 @@ - {% if playlist %} -
-

Your Playlist

- -
- {% 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); });