CoolFace
Apppublic

ashnaali22/phase-3-h2

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
priority_classifier.py116 linesDownload Raw Back to services
1"""2Priority Classification Service for Task Management3 4This module implements automatic priority classification based on task urgency5keywords and due date proximity, following the Skills & Subagents Architecture.6 7Classification Rules:8- VERY_IMPORTANT: Urgency keyword in title OR due date within 6 hours9- HIGH: Due date within 24 hours10- MEDIUM: Due date within 7 days11- LOW: Due date beyond 7 days OR no due date12"""13 14from datetime import datetime, timedelta, timezone15from typing import Optional16 17 18# Urgency keywords (case-insensitive matching)19URGENCY_KEYWORDS = ['urgent', 'asap', 'critical', 'important', 'emergency']20 21 22def classify_priority(title: str, due_date: Optional[datetime]) -> str:23    """24    Classify task priority based on title urgency keywords and due date proximity.25 26    Args:27        title: Task title to check for urgency keywords28        due_date: Optional task due date (timezone-aware datetime)29 30    Returns:31        Priority level: 'VERY_IMPORTANT' | 'HIGH' | 'MEDIUM' | 'LOW'32 33    Examples:34        >>> classify_priority("Urgent: Fix production bug", None)35        'VERY_IMPORTANT'36 37        >>> from datetime import datetime, timezone, timedelta38        >>> now = datetime.now(timezone.utc)39        >>> classify_priority("Normal task", now + timedelta(hours=3))40        'VERY_IMPORTANT'41 42        >>> classify_priority("Normal task", now + timedelta(hours=20))43        'HIGH'44 45        >>> classify_priority("Normal task", now + timedelta(days=5))46        'MEDIUM'47 48        >>> classify_priority("Normal task", now + timedelta(days=10))49        'LOW'50 51        >>> classify_priority("Normal task", None)52        'LOW'53    """54    # Check for urgency keywords in title (case-insensitive)55    title_lower = title.lower()56    has_urgency_keyword = any(keyword in title_lower for keyword in URGENCY_KEYWORDS)57 58    # If no due date, only urgency keyword matters59    if due_date is None:60        return 'VERY_IMPORTANT' if has_urgency_keyword else 'LOW'61 62    # Calculate time until due date63    now = datetime.now(timezone.utc).replace(tzinfo=None)64 65    # Ensure due_date is timezone-aware for comparison66    if due_date.tzinfo is None:67        # Treat naive datetime as UTC68        due_date = due_date.replace(tzinfo=timezone.utc)69 70    # Convert now to timezone-aware71    now = now.replace(tzinfo=timezone.utc)72 73    time_until_due = due_date - now74 75    # Classification logic based on due date proximity76    if time_until_due <= timedelta(hours=6):77        # Within 6 hours OR has urgency keyword78        return 'VERY_IMPORTANT'79    elif time_until_due <= timedelta(hours=24):80        # Within 24 hours81        return 'HIGH'82    elif time_until_due <= timedelta(days=7):83        # Within 1 week84        return 'MEDIUM'85    else:86        # Beyond 1 week87        return 'LOW'88 89 90def reclassify_priority_on_update(91    title: Optional[str],92    due_date: Optional[datetime],93    current_title: str,94    current_due_date: Optional[datetime]95) -> str:96    """97    Re-classify priority when task is updated.98 99    Args:100        title: New title (None if not being updated)101        due_date: New due date (None if not being updated, could be explicitly set to None to clear)102        current_title: Current task title103        current_due_date: Current task due date104 105    Returns:106        Priority level: 'VERY_IMPORTANT' | 'HIGH' | 'MEDIUM' | 'LOW'107    """108    # Use updated title if provided, otherwise keep current109    effective_title = title if title is not None else current_title110 111    # Use updated due_date if provided in the update, otherwise keep current112    # Note: If due_date is explicitly being cleared, it will be passed as None113    effective_due_date = due_date if due_date is not None else current_due_date114 115    return classify_priority(effective_title, effective_due_date)116