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 @@ +