Template refactor, task update

Banter no longer fails if AI offline. Refactoring of templates.
This commit is contained in:
2024-02-18 23:33:09 +01:00
parent c7bd7fda9b
commit fcf31cb896
14 changed files with 90 additions and 46 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.
+1
View File
@@ -0,0 +1 @@
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
+4
View File
@@ -1,3 +1,7 @@
body {
background-color: #01817F;
}
.window:not([role="tabpanel"]) {
position: fixed;
z-index:99;
+38 -30
View File
@@ -1,46 +1,54 @@
from celery import shared_task
from .models import Track
import os
import dotenv
import os
import requests
import time
import json
from random import choice
from requests import get, post
from requests.exceptions import ConnectionError
dotenv.load_dotenv()
UPDATE_FREQ = 10 # How often to save to database
@shared_task
def get_banter(id):
"""
Requests an AI model for some banter.
Currently expects an ollama instance to be running,
but might be possible to use OpenAI API in future.
"""
track = Track.objects.get(pk=id)
r = requests.post(
os.getenv('AI_ENDPOINT'),
json={
'model': os.getenv('AI_MODEL'),
'prompt': str(track)
},
stream=True
)
r.raise_for_status()
token = 0
try:
get(os.getenv('AI_ENDPOINT'))
except ConnectionError:
track.banter = "Error"
track.banter_done = True
track.save()
return f"{track}: AI endpoint not available."
for line in r.iter_lines():
body = json.loads(line)
response_part = body.get('response', '')
token += 1
track.banter += response_part
try:
r = post(
os.getenv('AI_ENDPOINT'),
json={
'model': os.getenv('AI_MODEL'),
'prompt': str(track)
},
stream=True
)
r.raise_for_status()
if 'error' in body:
raise Exception(body['error'])
for line in r.iter_lines():
body = json.loads(line)
if 'error' in body:
raise Exception(body['error'])
track.banter += body.get('response', '')
if body.get('done', False):
track.banter_done = True
track.save()
return body['context']
elif token % UPDATE_FREQ == 0:
track.save()
if body.get('done', False):
track.banter_done = True
track.save()
return f"{track}: {body['context']}"
except Exception as e:
track.banter_done = True
track.save()
return f"{track}: {e}"
+5 -14
View File
@@ -1,17 +1,10 @@
{% load static %}
<html>
<head>
{% load static %}
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="{% static 'js/htmx.min.js' %}" defer></script>
<script src="https://unpkg.com/interactjs"></script>
<link rel="stylesheet" href="https://unpkg.com/98.css" >
<link href="{% static 'style.css'%}" rel="stylesheet" type="text/css" />
<script src="https://open.spotify.com/embed/iframe-api/v1" async></script>
{% include "head.html" %}
</head>
<body hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' style="background:#01817F;">
<div class="window">
<body hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>
<div class="window" id="main-window">
<div class="title-bar">
<div class="title-bar-text">
{% block window-title %}{% endblock %}
@@ -42,14 +35,12 @@
</div>
{% block clippy %}{% endblock %}
<script>
position = { x: 0, y: 0 }
position.get_pos(); // Gets position from localStorage
interact('.title-bar').draggable({
listeners: {
move (event) {
position.x += event.dx
position.y += event.dy
event.target.parentElement.style.transform =
`translate(${position.x}px, ${position.y}px)`
},
+2 -1
View File
@@ -1,4 +1,5 @@
{% load static %}
{% if body %}
<script>
function closeclippy(e) {
console.log(e)
@@ -14,4 +15,4 @@
<img src="{% static img %}" />
{% endif %}
</div>
{% endif %}
+39
View File
@@ -0,0 +1,39 @@
{% load static %}
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="{% static 'js/htmx.min.js' %}" defer></script>
<script src="https://unpkg.com/interactjs"></script>
<link rel="stylesheet" href="https://unpkg.com/98.css" >
<link href="{% static 'style.css'%}" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="{% static 'apple-touch-icon.png' %}">
<link rel="icon" type="image/png" sizes="32x32" href="{% static 'favicon-32x32.png' %}">
<link rel="icon" type="image/png" sizes="16x16" href="{% static 'favicon-16x16.png' %}">
<link rel="manifest" href="/site.webmanifest">
<script src="https://open.spotify.com/embed/iframe-api/v1" async></script>
<title>HD Muzak: {{ request.path }}</title>
<script>
//Global position object for cross-page window positioning
position = {
set x(val) {
this._x = val
localStorage.setItem("position", JSON.stringify({x: val, y: this._y}));
},
set y(val) {
this._y = val
localStorage.setItem("position", JSON.stringify({x: this._x, y: val}));
},
get x() { return this._x },
get y() { return this._y },
_x: 0, _y: 0,
get_pos() {
stored_position = JSON.parse(localStorage.getItem("position"));
if (stored_position != null) {
position.x = stored_position.x
position.y = stored_position.y
document.getElementById("main-window").style.transform =
`translate(${position.x}px, ${position.y}px)`
} else {
localStorage.removeItem("position")
}
},
}
</script>
+1 -1
View File
@@ -11,7 +11,7 @@ def from_json(cls, json):
def get_unvoted(user):
votes = set(Vote.objects.filter(user=user).values_list('track_id', flat=True))
tracks = set(Track.objects.values_list('pk', flat=True))
tracks = set(Track.objects.filter(banter_done=True).values_list('pk', flat=True))
seed(user.username)
try:
random_track = choice(list(tracks - votes))