File size: 1,384 Bytes
da9047d
 
57acc5b
 
8cceab7
 
b26b1fd
 
 
 
8cceab7
b26b1fd
 
 
8cceab7
 
 
 
 
 
 
 
 
 
 
 
57acc5b
da9047d
 
 
 
 
 
 
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
from __future__ import annotations

from typing import Optional

from bson import ObjectId
from pydantic import BaseModel, Field, field_validator


class CategorizeRequest(BaseModel):
    notes: str = Field(..., min_length=1, description="Full transaction note.")
    user_id: str = Field(..., description="User identifier associated with the request.")


class CategoryPrediction(BaseModel):
    headcategory_id: str = Field(..., description="High-level category ObjectId.")
    headcategory_title: str = Field(..., description="High-level category title.")
    category_id: str = Field(..., description="Specific subcategory ObjectId chosen by the model.")
    category_title: str = Field(..., description="Specific subcategory title chosen by the model.")
    
    @field_validator('headcategory_id', 'category_id')
    @classmethod
    def validate_object_id(cls, v: str) -> str:
        """Validate that the string is a valid ObjectId."""
        if not ObjectId.is_valid(v):
            raise ValueError(f"Invalid ObjectId: {v}")
        return v


class CategorizeResponse(BaseModel):
    status: str = Field(..., description="Outcome of the request (success/fail).")
    data: Optional[CategoryPrediction] = Field(
        None, description="Predicted category payload when successful."
    )
    message: Optional[str] = Field(None, description="Error message when failed.")