From b0df731824912908695e37900604f9ada70eb4ab Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Tue, 25 Nov 2025 16:13:14 +0000 Subject: [PATCH] Auto commit: 2025-11-25T16:13:14.454Z --- core/__pycache__/views.cpython-311.pyc | Bin 3147 -> 3377 bytes core/templates/core/index.html | 5 +- core/views.py | 18 +++--- static/js/chat.js | 86 +++++++++++++++++++++++++ staticfiles/js/chat.js | 86 +++++++++++++++++++++++++ 5 files changed, 185 insertions(+), 10 deletions(-) create mode 100644 static/js/chat.js create mode 100644 staticfiles/js/chat.js diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index 9b9de93c1edc9e70bf4ad15c0f6de9ac113d2c84..bbf14058d1f5eb2d97da378ae21dca679ea2cdc6 100644 GIT binary patch delta 1228 zcmZuwOK2NM7@l42dnLc*IDSO(_>n5>IwDDfFBS65jY>*u`(hlZylAG@I+9kIT_v_y zBML2q5L$=51e$_RX-X~)z4YiyPra;+AYviJmmJ!YOLHhal+H*>ib{X}{bu%m&V2jt zFXPW9W8cMM8laMFowwQ#ViR2UL27QfEN+S%KmZ9PX;VVt7hqFH5=8|G>%fqIWhn+1 zN(5IBCUh75(6K9P*s0nT2R|#z5pjx!Nd8{rH%+Cx2z~xI^2-9`uKHgI#{yFKMSn}2 z7}g$h06e8nSIBk$o|yVT@+2~gbWgesNYay#KJg|1?91-(+ZW);{rUSGihA-l0L8wI z&<=l!hA4WGL&0wfaS@;aPxQ0Wg#i!jbneXx9rDb11E9hFQfTNV&-`S>J^gA&Jt6P_8u54*=|xcdk>Ed(&Zqw4N2BPB#XY9PJZ0o`gmOFf<7??-u2COb zw+Y!>ggIC=5i~0h6JnEeC_A-60Xq&Ery2E`T}rF|^#7P&FNgMw=D4KD2w0;9E89&_WmQELd(a9N75i`Zd?B zu66AOA8MwRFVp7cHV&0uFaL8T4uvpUb1-@1BnCYkSzrJe82(FT{+(DTm9bm25lJ%R z7zKJATF$a7p=4I8beM?f8|tM78=f$VcU@u@+`LOSp2JjQA&G>37z8XYVUk#o)xV#-qUpmSJc`LZnI0ayh|CFc8 zd-)#z4#3bB|GI axu9R$U_R(q|8IS9W}ZXbDfoZvH2Vj{u1rq= delta 971 zcmZuwO-vI(6rOFnyX|(%k1ZAol>%Z}O$rK8gM@=Y;2?0|Vz@M$&IXpW-8!>1h+84* zL5xPo#DqjNo)F=p@xqbBqnDO6k!)h(MH4+?#EU0q7O)Y0dGq$o+xNbCKQnJ4@9P6E z0|5z8+S_i|A1wtMxN}SKwzdfm^xOjzJmjeqpnDuB!dK>|aJGQ7EY#4O1<}n%sEH>} z+}|WG)!cObxI-2^F|LlRd$!3u-%#sY|mr%t;&lbA9yX~k{Jd90yihC-;5 z8ae9=ljq)WmEuBi3Wz!!L2mj^p7Ps5Ml+`jh@#BM(xj);INiwFf||?Gp&<`>=j)nd zL$hUV#zbn$Oqxt#)hk|GMB4SdhE1d}4Cjbu=Ilip&KX&(*@0=qm)iMV0n|5>HOUCcp~rm>`j&kqe{0#_x(lj&w>i@3 zzce?zIZ%ua@AIOxD1DB`%h8_K*Gkd;aBs(Mn* z>4@D=+ryfNSo@u>yaI@!INkc??aLT2pVs=(MQUTu1t-a-+(f>}r(4=M$nApvO9$gO D0^|0o diff --git a/core/templates/core/index.html b/core/templates/core/index.html index 0675bd0..4d7fcc4 100644 --- a/core/templates/core/index.html +++ b/core/templates/core/index.html @@ -26,9 +26,9 @@ {% endif %} -
+ {% csrf_token %} - +
@@ -46,4 +46,5 @@ {% endif %} + {% endblock %} diff --git a/core/views.py b/core/views.py index 4c90199..1a7838b 100644 --- a/core/views.py +++ b/core/views.py @@ -1,4 +1,5 @@ from django.shortcuts import render +from django.http import JsonResponse from ai.local_ai_api import LocalAIApi import json @@ -35,7 +36,6 @@ def index(request): if request.method == 'POST': user_message = request.POST.get('message') - # Get conversation history from session conversation = request.session.get('conversation', []) conversation.append({'role': 'user', 'content': user_message}) @@ -46,21 +46,23 @@ 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"Here is a playlist for your {mood} mood:" + ai_message = f"I've created a playlist for your {mood} mood. I hope you like it!" else: ai_message = LocalAIApi.extract_text(response) - conversation.append({'role': 'assistant', 'content': ai_message}) - request.session['conversation'] = conversation - return render(request, 'core/index.html', {'user_message': user_message, 'ai_message': ai_message, 'playlist': playlist}) - else: - # Handle error - return render(request, 'core/index.html', {'user_message': user_message, 'ai_message': 'Sorry, I had an error.'}) + conversation.append({'role': 'assistant', 'content': ai_message}) + request.session['conversation'] = conversation + + if request.headers.get('x-requested-with') == 'XMLHttpRequest': + return JsonResponse({'ai_message': ai_message, 'playlist': playlist}) + + return render(request, 'core/index.html', {'user_message': user_message, 'ai_message': ai_message, 'playlist': playlist, 'conversation': conversation}) else: # Start of a new conversation diff --git a/static/js/chat.js b/static/js/chat.js new file mode 100644 index 0000000..da7b297 --- /dev/null +++ b/static/js/chat.js @@ -0,0 +1,86 @@ + +document.addEventListener('DOMContentLoaded', function() { + const chatForm = document.getElementById('chat-form'); + const chatBox = document.getElementById('chat-box'); + const chatInput = document.getElementById('chat-input'); + const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value; + + chatForm.addEventListener('submit', function(event) { + event.preventDefault(); + + const userMessage = chatInput.value.trim(); + if (userMessage === '') { + return; + } + + // Add user message to chat box + appendMessage(userMessage, 'user-message'); + chatInput.value = ''; + + fetch(window.location.href, { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-CSRFToken': csrfToken, + 'X-Requested-With': 'XMLHttpRequest', + }, + body: new URLSearchParams({ + 'message': userMessage + }) + }) + .then(response => response.json()) + .then(data => { + // Add AI message to chat box + appendMessage(data.ai_message, 'ai-message'); + + // If a playlist is returned, display it + if (data.playlist && data.playlist.length > 0) { + displayPlaylist(data.playlist); + } + }) + .catch(error => { + console.error('Error:', error); + appendMessage('Sorry, something went wrong.', 'ai-message'); + }); + }); + + function appendMessage(message, className) { + const messageElement = document.createElement('div'); + messageElement.classList.add('chat-message', className); + const p = document.createElement('p'); + p.textContent = message; + messageElement.appendChild(p); + chatBox.appendChild(messageElement); + chatBox.scrollTop = chatBox.scrollHeight; // Scroll to bottom + } + + function displayPlaylist(playlist) { + let playlistContainer = document.querySelector('.playlist-container'); + if (!playlistContainer) { + playlistContainer = document.createElement('div'); + playlistContainer.classList.add('playlist-container'); + + const heroSection = document.querySelector('.hero-section'); + heroSection.appendChild(playlistContainer); + } + + playlistContainer.innerHTML = ''; // Clear previous playlist + + const title = document.createElement('h2'); + title.classList.add('playlist-title'); + title.textContent = 'Your Playlist'; + playlistContainer.appendChild(title); + + const ul = document.createElement('ul'); + ul.classList.add('playlist'); + + playlist.forEach(song => { + const li = document.createElement('li'); + li.classList.add('playlist-item'); + li.innerHTML = `${song.title} - ${song.artist}`; + ul.appendChild(li); + }); + + playlistContainer.appendChild(ul); + } +}); diff --git a/staticfiles/js/chat.js b/staticfiles/js/chat.js new file mode 100644 index 0000000..da7b297 --- /dev/null +++ b/staticfiles/js/chat.js @@ -0,0 +1,86 @@ + +document.addEventListener('DOMContentLoaded', function() { + const chatForm = document.getElementById('chat-form'); + const chatBox = document.getElementById('chat-box'); + const chatInput = document.getElementById('chat-input'); + const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value; + + chatForm.addEventListener('submit', function(event) { + event.preventDefault(); + + const userMessage = chatInput.value.trim(); + if (userMessage === '') { + return; + } + + // Add user message to chat box + appendMessage(userMessage, 'user-message'); + chatInput.value = ''; + + fetch(window.location.href, { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-CSRFToken': csrfToken, + 'X-Requested-With': 'XMLHttpRequest', + }, + body: new URLSearchParams({ + 'message': userMessage + }) + }) + .then(response => response.json()) + .then(data => { + // Add AI message to chat box + appendMessage(data.ai_message, 'ai-message'); + + // If a playlist is returned, display it + if (data.playlist && data.playlist.length > 0) { + displayPlaylist(data.playlist); + } + }) + .catch(error => { + console.error('Error:', error); + appendMessage('Sorry, something went wrong.', 'ai-message'); + }); + }); + + function appendMessage(message, className) { + const messageElement = document.createElement('div'); + messageElement.classList.add('chat-message', className); + const p = document.createElement('p'); + p.textContent = message; + messageElement.appendChild(p); + chatBox.appendChild(messageElement); + chatBox.scrollTop = chatBox.scrollHeight; // Scroll to bottom + } + + function displayPlaylist(playlist) { + let playlistContainer = document.querySelector('.playlist-container'); + if (!playlistContainer) { + playlistContainer = document.createElement('div'); + playlistContainer.classList.add('playlist-container'); + + const heroSection = document.querySelector('.hero-section'); + heroSection.appendChild(playlistContainer); + } + + playlistContainer.innerHTML = ''; // Clear previous playlist + + const title = document.createElement('h2'); + title.classList.add('playlist-title'); + title.textContent = 'Your Playlist'; + playlistContainer.appendChild(title); + + const ul = document.createElement('ul'); + ul.classList.add('playlist'); + + playlist.forEach(song => { + const li = document.createElement('li'); + li.classList.add('playlist-item'); + li.innerHTML = `${song.title} - ${song.artist}`; + ul.appendChild(li); + }); + + playlistContainer.appendChild(ul); + } +});