Files
FireworksApp/fireworks_detect/views.py
T
2026-10-02 13:13:38 +02:00

480 lines
19 KiB
Python

import math
from datetime import timedelta
from decimal import Decimal, InvalidOperation
import json
import mimetypes
import uuid
from pathlib import Path
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
from django.contrib.admin.views.decorators import staff_member_required
from django.http import FileResponse, Http404, JsonResponse
from django.core import signing
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
from django.utils.dateparse import parse_date
from django.utils.translation import gettext as _
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from .h3_utils import cell_boundary, cell_center, cell_from_coordinates, h3_resolution
from .models import ClassificationRule, Event, PendingEvent, Sensor
CLASSIFIER_URL = "http://192.168.1.122:5000/classify"
MAX_AUDIO_SIZE = 25 * 1024 * 1024 # 25 MiB: enough for this demo, and avoids accidental large uploads.
FIREWORK_TERMS = (
"firework", "firecracker", "explosion", "explosive", "bang", "boom",
"hiss", "crackle", "burst", "gunshot", "artillery", "cannon", "rocket",
)
SPEECH_TERMS = (
"speech", "conversation", "narration", "monologue", "babbling", "whisper",
"speaking", "speech synthesizer",
)
AUDIO_EXTENSIONS = {".aac", ".aif", ".aiff", ".flac", ".m4a", ".mp3", ".ogg", ".opus", ".wav", ".weba"}
@never_cache
def fireworks_view(request):
return render(request, "fireworks_detect/home.html")
MAP_DATE_RANGES = {
"7": 7,
"14": 14,
"30": 30,
"all": None,
"custom": None,
}
def _map_filters(request):
"""Return the selected map filters, using an inclusive date range."""
selected_range = request.GET.get("range", "14")
if selected_range not in MAP_DATE_RANGES:
selected_range = "14"
if selected_range == "custom":
date_from = parse_date(request.GET.get("from", ""))
date_to = parse_date(request.GET.get("to", ""))
elif MAP_DATE_RANGES[selected_range]:
date_to = timezone.localdate()
date_from = date_to - timedelta(days=MAP_DATE_RANGES[selected_range] - 1)
else:
date_from = date_to = None
visual = request.GET.get("visual", "all")
if visual not in {"all", "yes", "no"}:
visual = "all"
return {
"date_from": date_from,
"date_to": date_to,
"range": selected_range,
"visual": visual,
}
def _events_for_map(filters):
"""Create privacy-preserving map data for reviewed manual reports only."""
events = Event.objects.filter(source_type=Event.SourceType.MANUAL)
if filters["date_from"]:
events = events.filter(created_at__date__gte=filters["date_from"])
if filters["date_to"]:
events = events.filter(created_at__date__lte=filters["date_to"])
if filters["visual"] == "yes":
events = events.filter(visually_confirmed=True)
elif filters["visual"] == "no":
events = events.filter(visually_confirmed=False)
cells = {}
mapped_events = []
for event in events.values(
"h3_cell", "latitude", "longitude", "occurrences", "visually_confirmed",
"direction_degrees", "direction_accuracy_degrees", "created_at",
):
# Coordinates are used only here to derive an H3 cell; they never leave the server.
h3_cell = event["h3_cell"] or cell_from_coordinates(event["latitude"], event["longitude"])
center = cell_center(h3_cell) if h3_cell else None
boundary = cell_boundary(h3_cell) if h3_cell else None
if not center or not boundary:
continue
aggregate = cells.setdefault(h3_cell, {
"cell": h3_cell,
"boundary": boundary,
"center": center,
"reports": 0,
"occurrences": 0,
})
aggregate["reports"] += 1
aggregate["occurrences"] += event["occurrences"]
mapped_events.append({
"cell": h3_cell,
"center": center,
"occurrences": event["occurrences"],
"visually_confirmed": event["visually_confirmed"],
# A day is sufficient for this map and avoids sharing a precise report time.
"reported_on": event["created_at"].date().isoformat(),
"direction_degrees": float(event["direction_degrees"]) if event["direction_degrees"] is not None else None,
"direction_accuracy_degrees": (
float(event["direction_accuracy_degrees"])
if event["direction_accuracy_degrees"] is not None else None
),
})
return mapped_events, list(cells.values())
@never_cache
def fireworks_map_view(request):
filters = _map_filters(request)
map_events, h3_cells = _events_for_map(filters)
return render(request, "fireworks_detect/map.html", {
"h3_resolution": h3_resolution(),
"h3_cells": h3_cells,
"map_events": map_events,
"mapped_event_count": len(map_events),
"map_filters": {
"date_from": filters["date_from"].isoformat() if filters["date_from"] else "",
"date_to": filters["date_to"].isoformat() if filters["date_to"] else "",
"range": filters["range"],
"visual": filters["visual"],
},
})
@never_cache
def dashboard_view(request):
map_events, h3_cells = _events_for_map({
"date_from": None,
"date_to": None,
"range": "all",
"visual": "all",
})
return render(request, "fireworks_detect/dashboard.html", {
"h3_resolution": h3_resolution(),
"h3_cells": h3_cells,
"map_events": map_events,
})
def events_view(request):
return render(request, "fireworks_detect/events.html")
def _classification_context(payload):
predictions = _decorate_predictions(payload.get("predictions"))
peak_predictions = _decorate_predictions(payload.get("peak_predictions"))
return {
"result": payload,
"predictions": predictions,
"peak_predictions": peak_predictions,
"firework_count": sum(item["kind"] == "firework" for item in predictions + peak_predictions),
"speech_count": sum(item["kind"] == "speech" for item in predictions + peak_predictions),
}
def _label_kind(label):
lowered = label.lower()
if any(term in lowered for term in SPEECH_TERMS):
return "speech"
if any(term in lowered for term in FIREWORK_TERMS):
return "firework"
return "default"
def _score_details(score):
try:
percentage = max(0, min(float(score) * 100, 100))
except (TypeError, ValueError):
return "—", 0
return f"{percentage:.1f}%", round(percentage, 1)
def _decorate_predictions(predictions):
decorated = []
for prediction in predictions if isinstance(predictions, list) else []:
if not isinstance(prediction, dict):
continue
label = str(prediction.get("class", "Unknown"))
score, score_width = _score_details(prediction.get("score"))
decorated.append({
**prediction,
"label": label,
"kind": _label_kind(label),
"score_display": score,
"score_width": score_width,
})
return decorated
def _encode_audio_multipart(uploaded_file):
"""Build the multipart request expected by the classifier without a third-party HTTP client."""
boundary = f"----DjangoAudioUpload{uuid.uuid4().hex}"
filename = Path(uploaded_file.name).name.replace('"', "")
content_type = getattr(uploaded_file, "content_type", None) or mimetypes.guess_type(filename)[0] or "application/octet-stream"
header = (
f"--{boundary}\r\n"
f'Content-Disposition: form-data; name="file"; filename="{filename}"\r\n'
f"Content-Type: {content_type}\r\n\r\n"
).encode()
body = header + uploaded_file.read() + f"\r\n--{boundary}--\r\n".encode()
return body, f"multipart/form-data; boundary={boundary}"
def _render_error(request, message, status=400):
return render(request, "fireworks_detect/classification_result.html", {"error": message}, status=status)
@require_http_methods(["GET", "POST"])
@never_cache
def classify_upload(request):
# Opening this endpoint in a browser should return the upload screen rather
# than a 405 page. Only POST requests perform classification.
if request.method == "GET":
return redirect("fireworks_detect:fireworks")
uploaded_file = request.FILES.get("audio")
if not uploaded_file:
return _render_error(request, _("Choose an audio file before starting the analysis."))
extension = Path(uploaded_file.name).suffix.lower()
if not (uploaded_file.content_type or "").startswith("audio/") and extension not in AUDIO_EXTENSIONS:
return _render_error(request, _("Please upload a supported audio file (such as WAV, MP3, M4A, or OGG)."))
if uploaded_file.size > MAX_AUDIO_SIZE:
return _render_error(request, _("This demo accepts audio files up to 25 MiB."))
body, content_type = _encode_audio_multipart(uploaded_file)
classifier_request = Request(
CLASSIFIER_URL,
data=body,
headers={"Content-Type": content_type, "Accept": "application/json"},
method="POST",
)
try:
with urlopen(classifier_request, timeout=60) as response:
payload = json.loads(response.read().decode("utf-8"))
except HTTPError as error:
return _render_error(request, _("The classifier returned an error (HTTP %(code)s).") % {"code": error.code}, status=502)
except (URLError, TimeoutError):
return _render_error(request, _("The classifier could not be reached. Please try again shortly."), status=502)
except json.JSONDecodeError:
return _render_error(request, _("The classifier returned an unreadable response."), status=502)
if not isinstance(payload, dict):
return _render_error(request, _("The classifier returned an unexpected response."), status=502)
# Keep the raw payload in this request flow; this is the future hand-off point
# for saving the upload and its classification to a database or queue.
context = _classification_context(payload)
context["submission_token"] = signing.dumps(payload, salt="fireworks_detect.classification")
return render(request, "fireworks_detect/classification_result.html", context)
@require_http_methods(["POST"])
@never_cache
def submit_pending_event(request):
uploaded_file = request.FILES.get("audio")
token = request.POST.get("classification_token")
if not uploaded_file:
return render(request, "fireworks_detect/pending_submission.html", {"error": _("Keep the analysed audio file selected before submitting it for review.")}, status=400)
if not token:
return render(request, "fireworks_detect/pending_submission.html", {"error": _("Analyse the file before submitting it for review.")}, status=400)
try:
payload = signing.loads(token, salt="fireworks_detect.classification", max_age=60 * 60)
except signing.BadSignature:
return render(request, "fireworks_detect/pending_submission.html", {"error": _("This analysis has expired. Please analyse the audio again.")}, status=400)
if not isinstance(payload, dict):
return render(request, "fireworks_detect/pending_submission.html", {"error": _("The saved analysis is not valid.")}, status=400)
pending = PendingEvent.objects.create(
audio_file=uploaded_file,
original_filename=uploaded_file.name,
classifier_response=payload,
)
return render(request, "fireworks_detect/pending_submission.html", {"pending": pending})
def _manual_coordinates(request):
try:
latitude = Decimal(request.POST.get("latitude", ""))
longitude = Decimal(request.POST.get("longitude", ""))
if not -90 <= latitude <= 90 or not -180 <= longitude <= 180:
return None, None
return latitude.quantize(Decimal("0.000001")), longitude.quantize(Decimal("0.000001"))
except (InvalidOperation, TypeError, ValueError):
return None, None
def _manual_direction(request):
try:
heading = float(request.POST.get("direction_degrees", ""))
except (TypeError, ValueError):
return None, None
if not math.isfinite(heading) or not 0 <= heading < 360:
return None, None
try:
accuracy = float(request.POST.get("direction_accuracy_degrees", ""))
except (TypeError, ValueError):
accuracy = None
if accuracy is not None and (not math.isfinite(accuracy) or accuracy < 0):
accuracy = None
return heading, accuracy
@require_http_methods(["POST"])
@never_cache
def submit_manual_pending_event(request):
try:
occurrences = int(request.POST.get("occurrences", ""))
except (TypeError, ValueError):
occurrences = 0
if not 1 <= occurrences <= 1000:
return render(
request,
"fireworks_detect/manual_pending_submission.html",
{"error": _("Choose between 1 and 1,000 occurrences.")},
status=400,
)
uploaded_file = request.FILES.get("audio")
if uploaded_file:
if uploaded_file.size > MAX_AUDIO_SIZE or uploaded_file.content_type not in ("audio/wav", "audio/wave", "audio/x-wav"):
return render(request, "fireworks_detect/manual_pending_submission.html", {"error": _("Please attach a WAV clip no larger than 25 MiB.")}, status=400)
header = uploaded_file.read(12)
uploaded_file.seek(0)
if len(header) != 12 or header[:4] != b"RIFF" or header[8:] != b"WAVE":
return render(request, "fireworks_detect/manual_pending_submission.html", {"error": _("The attached audio is not a WAV file.")}, status=400)
uploaded_file.name = f"sound-signal-manual-{timezone.now():%Y%m%d-%H%M%S}-{uuid.uuid4().hex[:8]}.wav"
latitude, longitude = _manual_coordinates(request)
h3_cell = cell_from_coordinates(latitude, longitude)
direction_degrees, direction_accuracy_degrees = _manual_direction(request)
pending = PendingEvent.objects.create(
audio_file=uploaded_file,
original_filename="Manual fireworks report",
classifier_response={},
source_type=PendingEvent.SourceType.MANUAL,
occurrences=occurrences,
latitude=latitude,
longitude=longitude,
h3_cell=h3_cell or "",
direction_degrees=direction_degrees,
direction_accuracy_degrees=direction_accuracy_degrees,
direction_source="device_compass" if direction_degrees is not None else "",
)
return render(request, "fireworks_detect/manual_pending_submission.html", {"pending": pending})
@never_cache
def events_list(request):
return render(request, "fireworks_detect/events_list.html", {"events": Event.objects.all()[:30]})
@never_cache
def event_detail(request, event_id):
event = get_object_or_404(Event, pk=event_id)
context = _classification_context(event.classifier_response)
context["event"] = event
context["event_h3_cell"] = event.h3_cell or cell_from_coordinates(event.latitude, event.longitude)
return render(request, "fireworks_detect/event_detail.html", context)
def _audio_response(record):
if not record.audio_file:
raise Http404(_("No audio is attached to this record."))
content_type = mimetypes.guess_type(record.audio_file.name)[0] or "application/octet-stream"
return FileResponse(record.audio_file.open("rb"), content_type=content_type)
@staff_member_required
def pending_audio(request, event_id):
return _audio_response(get_object_or_404(PendingEvent, pk=event_id))
@staff_member_required
def event_audio(request, event_id):
return _audio_response(get_object_or_404(Event, pk=event_id))
def _sensor_from_request(request):
authorization = request.headers.get("Authorization", "")
token = authorization.removeprefix("Bearer ").strip() if authorization.startswith("Bearer ") else request.headers.get("X-Sensor-Token", "").strip()
if not token:
return None
sensor = Sensor.objects.filter(is_active=True, token_prefix=token[:15]).first()
return sensor if sensor and sensor.token_matches(token) else None
def _sensor_error(message, status=400):
return JsonResponse({"detail": message}, status=status)
@csrf_exempt
@require_http_methods(["POST"])
def classify_audio(request):
"""Token-authenticated device API. Browser uploads use classify_upload instead."""
sensor = _sensor_from_request(request)
if not sensor:
return _sensor_error("A valid sensor API token is required.", 401)
uploaded_file = request.FILES.get("file") or request.FILES.get("audio")
if not uploaded_file:
return _sensor_error("Attach audio as multipart field 'file'.")
extension = Path(uploaded_file.name).suffix.lower()
if not (uploaded_file.content_type or "").startswith("audio/") and extension not in AUDIO_EXTENSIONS:
return _sensor_error("Unsupported audio file.")
if uploaded_file.size > MAX_AUDIO_SIZE:
return _sensor_error("Audio exceeds the 25 MiB limit.")
body, content_type = _encode_audio_multipart(uploaded_file)
classifier_request = Request(CLASSIFIER_URL, data=body, headers={"Content-Type": content_type, "Accept": "application/json"}, method="POST")
try:
with urlopen(classifier_request, timeout=60) as response:
payload = json.loads(response.read().decode("utf-8"))
except HTTPError as error:
return _sensor_error(f"Classifier returned HTTP {error.code}.", 502)
except (URLError, TimeoutError, json.JSONDecodeError):
return _sensor_error("Classifier is unavailable or returned invalid JSON.", 502)
if not isinstance(payload, dict):
return _sensor_error("Classifier returned an unexpected response.", 502)
uploaded_file.seek(0)
pending = PendingEvent.objects.create(audio_file=uploaded_file, sensor=sensor, original_filename=uploaded_file.name, classifier_response=payload)
sensor.record_heartbeat(submission=True)
return JsonResponse({**payload, "pending_event_id": pending.pk, "sensor": sensor.name}, status=201)
@csrf_exempt
@require_http_methods(["POST"])
def sensor_heartbeat(request):
sensor = _sensor_from_request(request)
if not sensor:
return _sensor_error("A valid sensor API token is required.", 401)
sensor.record_heartbeat()
return JsonResponse({"ok": True, "sensor": sensor.name, "heartbeat_at": sensor.last_heartbeat.isoformat()})
@require_http_methods(["GET"])
def classification_whitelist(request):
classifications = ClassificationRule.objects.filter(
rule_type=ClassificationRule.RuleType.WHITELIST,
enabled=True,
).order_by("value").values_list("value", flat=True)
return JsonResponse({"classifications": list(classifications)})
@require_http_methods(["GET"])
def classification_blacklist(request):
classifications = ClassificationRule.objects.filter(
rule_type=ClassificationRule.RuleType.BLACKLIST,
enabled=True,
).order_by("value").values_list("value", flat=True)
return JsonResponse({"classifications": list(classifications)})