diff --git a/muzak/celery.py b/muzak/celery.py new file mode 100644 index 0000000..e85f54e --- /dev/null +++ b/muzak/celery.py @@ -0,0 +1,11 @@ +import os + +from celery import Celery + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'muzak.settings') + +app = Celery('muzak') + +app.config_from_object('django.conf:settings', namespace='CELERY') +app.autodiscover_tasks() + diff --git a/muzak/settings.py b/muzak/settings.py index 450d150..9bfdf25 100644 --- a/muzak/settings.py +++ b/muzak/settings.py @@ -94,6 +94,8 @@ LOGIN_REDIRECT_URL = "/" LOGOUT_REDIRECT_URL = "/" LOGIN_URL = "/" +CELERY_BROKER_URL = 'amqp://localhost:5672' + LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True diff --git a/playlist/models/track.py b/playlist/models/track.py index d41fe57..fb44a33 100644 --- a/playlist/models/track.py +++ b/playlist/models/track.py @@ -25,6 +25,7 @@ class Track(models.Model): popularity = models.PositiveIntegerField(null=True) banter = models.TextField(blank=True) + banter_done = models.BooleanField(default=False) old = models.BooleanField(default=False) diff --git a/playlist/tasks.py b/playlist/tasks.py new file mode 100644 index 0000000..254957b --- /dev/null +++ b/playlist/tasks.py @@ -0,0 +1,58 @@ +from celery import shared_task +from .models import Track + +import dotenv + +import os +import requests + +import time +import json +from random import choice + +dotenv.load_dotenv() + +@shared_task +def get_banter(id): + track = Track.objects.get(pk=id) + r = requests.post( + os.getenv('AI_ENDPOINT'), + #'http://localhost:11434/api/generate', + json={ + 'model': os.getenv('AI_MODEL'), + 'prompt': str(track) + }, + stream=True + ) + r.raise_for_status() + + for line in r.iter_lines(): + body = json.loads(line) + response_part = body.get('response', '') + # the response streams one token at a time, print that as we receive it + print(response_part, end='', flush=True) + track.banter += response_part + track.save() + + if 'error' in body: + raise Exception(body['error']) + + if body.get('done', False): + track.banter_done = True + track.save() + return body['context'] +@shared_task +def get_banter_test(id): + letters = ["a","b","c","d","e","q","w", " "] + track = Track.objects.get(pk=id) + time.sleep(20) + track.banter = "Lorem " + track.save() + time.sleep(.5) + + for i in range(100): + track.banter += choice(letters) + track.save() + time.sleep(.3) + track.banter_done = True + track.save() diff --git a/playlist/templates/Banter.html b/playlist/templates/Banter.html new file mode 100644 index 0000000..365efba --- /dev/null +++ b/playlist/templates/Banter.html @@ -0,0 +1,14 @@ +
+ {% if track.banter != "" %} + {{track.banter}} + {% else %} + {{ waiting | random }} + {% endif %} +
diff --git a/playlist/templates/Nominate.html b/playlist/templates/Nominate.html index 1d0bb50..e9a5df3 100644 --- a/playlist/templates/Nominate.html +++ b/playlist/templates/Nominate.html @@ -2,9 +2,6 @@ {% block main-content %}

Nominate

-{% if nominated %} -
{% if error %}❌{% else %}✅{% endif %} {{nominated}}
-{% endif %} {% if warning %}
{{warning}}
{% endif %} @@ -16,4 +13,8 @@ +{% if nominated %} +
{% if error %}❌{% else %}✅{% endif %} {{nominated}}
+{% include "Banter.html" with track=nominated %} +{% endif %} {% endblock %} diff --git a/playlist/urls.py b/playlist/urls.py index 2eebeb6..35aebfe 100644 --- a/playlist/urls.py +++ b/playlist/urls.py @@ -4,6 +4,7 @@ from . import views urlpatterns = [ path('track/', views.TrackDetailView.as_view(), name="track-detail"), path('track/', views.NominateView.as_view(), name="nominate"), + path('track//banter', views.BanterView.as_view(), name="track-banter"), path('vote/', views.VoteView.as_view(), name="vote"), path('vote/track', views.VoteTrackView.as_view(), name="vote-track"), path('', views.AuthView.as_view()), diff --git a/playlist/views.py b/playlist/views.py index e36033a..ef96857 100644 --- a/playlist/views.py +++ b/playlist/views.py @@ -7,6 +7,7 @@ import re from .models import Track, Artist, Album, Vote from .utils import from_json, get_unvoted from . import spotify +from .tasks import get_banter spt = spotify.Spotify() @@ -96,4 +97,19 @@ class NominateView(LoginRequiredMixin, View): track.artists.add(artist) track.album = album track.save() + get_banter.delay(track.id) + return TemplateResponse(request, "Nominate.html", {"nominated": track}) + +class BanterView(LoginRequiredMixin, View): + def get(self, request, spotify_id): + try: + track = Track.objects.get(pk=spotify_id) + return TemplateResponse(request, "Banter.html", { + "track": track, + "waiting": ["Nog even..", "Bijna klaar.", "Oké...", "Interessant", "Momentje"] + }) + except Track.DoesNotExist: + return HttpResponse("???") + + diff --git a/requirements.txt b/requirements.txt index f8c6d2e..3b3b36b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ pillow python-dotenv mozilla-django-oidc django-htmx +celery