File size: 2,098 Bytes
d74863e
 
 
 
 
 
 
 
 
 
 
 
 
 
147d620
d74863e
 
8b21ecf
d74863e
 
 
 
 
 
 
 
 
1ab78e2
 
 
d74863e
 
 
 
 
 
 
 
 
 
1ab78e2
d74863e
1ab78e2
 
 
 
 
 
 
 
 
 
 
d74863e
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from google import genai
from typing import Optional
from src.utils.logger import setup_logger
from src.utils.config import settings

logger = setup_logger(__name__)


class CategorizationService:
    """
    Service for automatically categorizing notes based on their content using Gemini AI.
    """

    def __init__(self, api_key: Optional[str] = None):
        self.api_key = api_key or settings.gemini_api_key_1
        # Use the newer google-genai client
        self.client = genai.Client(api_key=self.api_key)
        self.model_id = "gemini-1.5-flash-latest"

    async def categorize_text(self, text: str) -> str:
        """
        Categorize a piece of text into a single word or short phrase category.
        """
        if not text or len(text) < 10:
            return "Uncategorized"

        prompt = (
            "Analyze the following text and categorize it into exactly ONE of the following: "
            "['Technology & AI', 'Business & Finance', 'Education & Science', 'Productivity & Self-Growth', 'News & Politics', 'Entertainment & Lifestyle', 'Health & Sports']. "
            "Return ONLY the category string with no additional text or punctuation.\n\n"
            f"Text: {text[:2000]}\n\n"
            "Category:"
        )

        try:
            # Using the newer google-genai syntax
            response = self.client.models.generate_content(
                model=self.model_id, contents=prompt
            )

            category = response.text.strip()
            # Basic validation/cleanup
            valid_categories = [
                "Technology & AI", 
                "Business & Finance", 
                "Education & Science", 
                "Productivity & Self-Growth", 
                "News & Politics", 
                "Entertainment & Lifestyle", 
                "Health & Sports"
            ]
            if category not in valid_categories:
                return "Uncategorized"
            return category
        except Exception as e:
            logger.error(f"Categorization failed: {e}")
            return "Uncategorized"