Spaces:
Running
Running
Download app/research.py from 2Btainment/Fuugi-API: direct link, hf CLI and curl.
- Browser
- Download file 6.52 kB
-
https://huggingface.co/spaces/2Btainment/Fuugi-API/resolve/main/app/research.py
- Command line
-
hf download hf://spaces/2Btainment/Fuugi-API/app/research.py
-
curl -L -o research.py https://huggingface.co/spaces/2Btainment/Fuugi-API/resolve/main/app/research.py
6.52 kB
| from __future__ import annotations | |
| import asyncio | |
| import os | |
| import re | |
| import sqlite3 | |
| from collections.abc import Callable | |
| from datetime import UTC, date, datetime, timedelta | |
| from enum import StrEnum | |
| from pathlib import Path | |
| class ResearchTopic(StrEnum): | |
| ARTISTS_RELEASES = "artists_releases" | |
| AUDIO_RADIO = "audio_radio" | |
| VIDEO = "video" | |
| SHOP_DOWNLOADS = "shop_downloads" | |
| STUDIO_SERVICES = "studio_services" | |
| CONTACT_BOOKING = "contact_booking" | |
| PRIVACY = "privacy" | |
| OTHER = "other" | |
| class ResearchIntent(StrEnum): | |
| INFORMATION = "information" | |
| NAVIGATION = "navigation" | |
| AVAILABILITY = "availability" | |
| CONSUME = "consume" | |
| DOWNLOAD_PURCHASE = "download_purchase" | |
| CONTACT_COLLABORATE = "contact_collaborate" | |
| SUPPORT = "support" | |
| OTHER = "other" | |
| TOPIC_RULES = ( | |
| (ResearchTopic.PRIVACY, ("datenschutz", "privacy", "speichern", "erinnerung", "memory")), | |
| ( | |
| ResearchTopic.CONTACT_BOOKING, | |
| ("kontakt", "contact", "booking", "buchen", "einreichen", "email", "zusammenarbeit"), | |
| ), | |
| ( | |
| ResearchTopic.STUDIO_SERVICES, | |
| ("2b studio", "studio", "service", "grafik", "produktion", "mixing", "mastering"), | |
| ), | |
| ( | |
| ResearchTopic.SHOP_DOWNLOADS, | |
| ("shop", "merch", "kaufen", "bestellen", "download", "wav", "mp3"), | |
| ), | |
| (ResearchTopic.VIDEO, ("2b tube", "video", "clip", "visual", "ansehen", "anschauen")), | |
| ( | |
| ResearchTopic.AUDIO_RADIO, | |
| ("2bfy", "radio", "musik", "song", "anhören", "hoeren", "stream", "playlist"), | |
| ), | |
| ( | |
| ResearchTopic.ARTISTS_RELEASES, | |
| ("artist", "roster", "release", "album", "ep", "track", "evil crow", "ktn", "zmureo"), | |
| ), | |
| ) | |
| INTENT_RULES = ( | |
| (ResearchIntent.SUPPORT, ("fehler", "funktioniert nicht", "problem", "hilfe", "support")), | |
| ( | |
| ResearchIntent.CONTACT_COLLABORATE, | |
| ("kontakt", "contact", "booking", "buchen", "einreichen", "mitmachen", "zusammenarbeiten"), | |
| ), | |
| ( | |
| ResearchIntent.DOWNLOAD_PURCHASE, | |
| ("download", "herunterladen", "kaufen", "bestellen", "preis", "kosten"), | |
| ), | |
| ( | |
| ResearchIntent.CONSUME, | |
| ("anhören", "hoeren", "abspielen", "ansehen", "anschauen", "streamen"), | |
| ), | |
| (ResearchIntent.NAVIGATION, ("wo finde", "link", "seite", "öffnen", "oeffnen", "hin")), | |
| ( | |
| ResearchIntent.AVAILABILITY, | |
| ("verfügbar", "verfuegbar", "wann", "neu", "aktuell", "online", "live"), | |
| ), | |
| (ResearchIntent.INFORMATION, ("was", "wer", "wie", "warum", "welche", "info", "erklär")), | |
| ) | |
| class ResearchWriteError(RuntimeError): | |
| pass | |
| def _normalized(text: str) -> str: | |
| return " " + re.sub(r"[^\wäöüß]+", " ", text.casefold()).strip() + " " | |
| def _matches(normalized: str, terms: tuple[str, ...]) -> bool: | |
| return any(f" {_normalized(term).strip()} " in normalized for term in terms) | |
| def classify_research(text: str) -> tuple[ResearchTopic, ResearchIntent]: | |
| normalized = _normalized(text) | |
| topic = next( | |
| (category for category, terms in TOPIC_RULES if _matches(normalized, terms)), | |
| ResearchTopic.OTHER, | |
| ) | |
| intent = next( | |
| (category for category, terms in INTENT_RULES if _matches(normalized, terms)), | |
| ResearchIntent.OTHER, | |
| ) | |
| return topic, intent | |
| def _utc_day() -> date: | |
| return datetime.now(UTC).date() | |
| SCHEMA = """ | |
| CREATE TABLE IF NOT EXISTS research_daily_counts ( | |
| day_utc TEXT NOT NULL, | |
| dimension TEXT NOT NULL CHECK (dimension IN ('topic', 'intent')), | |
| category TEXT NOT NULL, | |
| count INTEGER NOT NULL CHECK (typeof(count) = 'integer' AND count > 0), | |
| PRIMARY KEY (day_utc, dimension, category), | |
| CHECK (day_utc GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'), | |
| CHECK ( | |
| (dimension = 'topic' AND category IN ( | |
| 'artists_releases', 'audio_radio', 'video', 'shop_downloads', | |
| 'studio_services', 'contact_booking', 'privacy', 'other' | |
| )) OR | |
| (dimension = 'intent' AND category IN ( | |
| 'information', 'navigation', 'availability', 'consume', | |
| 'download_purchase', 'contact_collaborate', 'support', 'other' | |
| )) | |
| ) | |
| ) WITHOUT ROWID; | |
| """ | |
| class ResearchStore: | |
| def __init__( | |
| self, | |
| database_path: str, | |
| retention_days: int = 90, | |
| utc_day: Callable[[], date] = _utc_day, | |
| ) -> None: | |
| self._path = Path(database_path).expanduser().resolve() | |
| self._retention_days = retention_days | |
| self._utc_day = utc_day | |
| self._prepare() | |
| def _prepare(self) -> None: | |
| try: | |
| self._path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) | |
| os.chmod(self._path.parent, 0o700) | |
| descriptor = os.open(self._path, os.O_CREAT | os.O_WRONLY, 0o600) | |
| os.close(descriptor) | |
| os.chmod(self._path, 0o600) | |
| with sqlite3.connect(self._path) as connection: | |
| connection.execute("PRAGMA trusted_schema = OFF") | |
| connection.executescript(SCHEMA) | |
| os.chmod(self._path, 0o600) | |
| except (OSError, sqlite3.Error) as error: | |
| raise ResearchWriteError("Could not initialize anonymous research counters.") from error | |
| async def increment(self, topic: ResearchTopic, intent: ResearchIntent) -> None: | |
| try: | |
| await asyncio.to_thread(self._increment_sync, topic, intent) | |
| except (OSError, sqlite3.Error) as error: | |
| raise ResearchWriteError("Could not update anonymous research counters.") from error | |
| def _increment_sync(self, topic: ResearchTopic, intent: ResearchIntent) -> None: | |
| current_day = self._utc_day() | |
| cutoff = current_day - timedelta(days=self._retention_days - 1) | |
| rows = (("topic", topic.value), ("intent", intent.value)) | |
| with sqlite3.connect(self._path, timeout=5) as connection: | |
| connection.execute("BEGIN IMMEDIATE") | |
| connection.execute( | |
| "DELETE FROM research_daily_counts WHERE day_utc < ?", | |
| (cutoff.isoformat(),), | |
| ) | |
| connection.executemany( | |
| """ | |
| INSERT INTO research_daily_counts (day_utc, dimension, category, count) | |
| VALUES (?, ?, ?, 1) | |
| ON CONFLICT(day_utc, dimension, category) | |
| DO UPDATE SET count = count + 1 | |
| """, | |
| ((current_day.isoformat(), dimension, category) for dimension, category in rows), | |
| ) | |
| connection.commit() | |