Spaces:
Sleeping
Sleeping
File size: 1,007 Bytes
545a4e2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import threading
from enum import Enum
from collections import defaultdict
class UsageFeature(str, Enum):
TEXT = "text"
IMAGE = "image"
VIDEO = "video"
EMR = "emr"
SPEECH = "speech"
class UsageTracker:
"""
Simple in-memory usage tracker.
- Not durable: resets on process restart.
- Thread-safe for single-process deployments.
"""
TEXT_FREE_LIMIT = 10
def __init__(self) -> None:
self._lock = threading.Lock()
self._counts: dict[str, dict[UsageFeature, int]] = defaultdict(
lambda: defaultdict(int)
)
def record(self, user_id: str, feature: UsageFeature) -> int:
with self._lock:
self._counts[user_id][feature] += 1
return self._counts[user_id][feature]
def get_count(self, user_id: str, feature: UsageFeature) -> int:
with self._lock:
return self._counts[user_id][feature]
# Singleton instance shared across dependencies
usage_tracker = UsageTracker()
|