Download models.py from Atomio/facerecog: direct link, hf CLI and curl.
- Browser
- Download file 3.66 kB
-
https://huggingface.co/spaces/Atomio/facerecog/resolve/main/models.py
- Command line
-
hf download hf://spaces/Atomio/facerecog/models.py
-
curl -L -o models.py https://huggingface.co/spaces/Atomio/facerecog/resolve/main/models.py
3.66 kB
| """Global database models and base classes.""" | |
| from datetime import datetime | |
| from typing import Any | |
| from uuid import UUID | |
| from pydantic import BaseModel, ConfigDict, Field | |
| class CustomBaseModel(BaseModel): | |
| """Custom base model with common configurations.""" | |
| model_config = ConfigDict( | |
| # Use enum values instead of enum objects | |
| use_enum_values=True, | |
| # Validate assignment | |
| validate_assignment=True, | |
| # Allow population by field name or alias | |
| populate_by_name=True, | |
| # Serialize datetime as ISO format | |
| json_encoders={datetime: lambda v: v.isoformat()}, | |
| # Extra fields are forbidden | |
| extra="forbid", | |
| ) | |
| def model_dump_json_safe(self, **kwargs: Any) -> dict[str, Any]: | |
| """Return a dict that is safe for JSON serialization.""" | |
| return self.model_dump(mode="json", **kwargs) | |
| class TimestampMixin(BaseModel): | |
| """Mixin for models with timestamp fields.""" | |
| created_at: datetime = Field(description="Creation timestamp") | |
| updated_at: datetime | None = Field(default=None, description="Last update timestamp") | |
| class UUIDMixin(BaseModel): | |
| """Mixin for models with UUID primary key.""" | |
| id: UUID = Field(description="Unique identifier") | |
| class BaseDBModel(CustomBaseModel, UUIDMixin, TimestampMixin): | |
| """Base model for database entities.""" | |
| pass | |
| class PaginationParams(CustomBaseModel): | |
| """Pagination parameters.""" | |
| page: int = Field(default=1, ge=1, description="Page number (1-based)") | |
| size: int = Field(default=20, ge=1, le=100, description="Page size") | |
| def offset(self) -> int: | |
| """Calculate offset for database queries.""" | |
| return (self.page - 1) * self.size | |
| def limit(self) -> int: | |
| """Get limit for database queries.""" | |
| return self.size | |
| class PaginatedResponse(CustomBaseModel): | |
| """Generic paginated response.""" | |
| items: list[Any] = Field(description="List of items") | |
| total: int = Field(description="Total number of items") | |
| page: int = Field(description="Current page number") | |
| size: int = Field(description="Page size") | |
| pages: int = Field(description="Total number of pages") | |
| def create( | |
| cls, | |
| items: list[Any], | |
| total: int, | |
| page: int, | |
| size: int, | |
| ) -> "PaginatedResponse": | |
| """Create a paginated response.""" | |
| pages = (total + size - 1) // size # Ceiling division | |
| return cls( | |
| items=items, | |
| total=total, | |
| page=page, | |
| size=size, | |
| pages=pages, | |
| ) | |
| class HealthCheck(CustomBaseModel): | |
| """Health check response model.""" | |
| status: str = Field(description="Service status") | |
| timestamp: datetime = Field(description="Check timestamp") | |
| version: str = Field(description="Application version") | |
| environment: str = Field(description="Environment name") | |
| class ErrorResponse(CustomBaseModel): | |
| """Standard error response model.""" | |
| error: str = Field(description="Error type") | |
| message: str = Field(description="Error message") | |
| details: dict[str, Any] | None = Field(default=None, description="Error details") | |
| timestamp: datetime = Field(description="Error timestamp") | |
| path: str | None = Field(default=None, description="Request path") | |
| class SuccessResponse(CustomBaseModel): | |
| """Standard success response model.""" | |
| success: bool = Field(default=True, description="Success flag") | |
| message: str = Field(description="Success message") | |
| data: dict[str, Any] | None = Field(default=None, description="Response data") | |