Files
FireworksApp/fireworks_detect/browser_policy.py
T

68 lines
2.4 KiB
Python

"""Public, advisory configuration for the optional browser processing beta."""
import json
from django.urls import reverse
from .models import BrowserProcessingSettings, ClassificationRule
POLICY_VERSION = 1
SPEECH_LABEL_TERMS = (
"speech", "conversation", "narration", "monologue", "babbling", "whisper",
"speaking", "speech synthesizer",
)
GATE_RESULTS = {"whitelist_match", "no_match", "unavailable", "error"}
def browser_config():
"""Keep template configuration in one small, non-secret object."""
return {
"browserProcessingBeta": BrowserProcessingSettings.enabled(),
"policyUrl": reverse("fireworks_detect:browser_policy"),
"policyVersion": POLICY_VERSION,
}
def browser_policy():
enabled = BrowserProcessingSettings.enabled()
policy = {
"version": POLICY_VERSION,
"browser_processing_beta_enabled": enabled,
"whitelist": [],
"privacy_sensitive_speech": [],
"thresholds": {},
}
if not enabled:
return policy
rules = ClassificationRule.objects.filter(enabled=True).order_by("value")
policy["whitelist"] = list(rules.filter(rule_type=ClassificationRule.RuleType.WHITELIST).values_list("value", flat=True))
policy["privacy_sensitive_speech"] = [
value for value in rules.filter(rule_type=ClassificationRule.RuleType.BLACKLIST).values_list("value", flat=True)
if any(term in value.lower() for term in SPEECH_LABEL_TERMS)
]
return policy
def advisory_browser_metadata(post_data):
"""Parse bounded diagnostics; never use this to authorize or classify audio."""
raw = post_data.get("browser_processing_metadata", "")
if not raw or len(raw) > 1024:
return {}
try:
submitted = json.loads(raw)
except (TypeError, ValueError):
return {}
if not isinstance(submitted, dict):
return {}
metadata = {}
for key in ("browser_processing_used", "browser_override"):
if type(submitted.get(key)) is bool:
metadata[key] = submitted[key]
version = submitted.get("browser_processing_version")
if type(version) is int and 0 < version <= POLICY_VERSION:
metadata["browser_processing_version"] = version
gate_result = submitted.get("browser_gate_result")
if isinstance(gate_result, str) and gate_result in GATE_RESULTS:
metadata["browser_gate_result"] = gate_result
return metadata