Spaces:
Sleeping
Sleeping
Download sdk/python/image_search_sdk/models.py from fmbishu/image-search-api: direct link, hf CLI and curl.
- Browser
- Download file 5.1 kB
-
https://huggingface.co/spaces/fmbishu/image-search-api/resolve/main/sdk/python/image_search_sdk/models.py
- Command line
-
hf download hf://spaces/fmbishu/image-search-api/sdk/python/image_search_sdk/models.py
-
curl -L -o models.py https://huggingface.co/spaces/fmbishu/image-search-api/resolve/main/sdk/python/image_search_sdk/models.py
5.1 kB
| """ | |
| Data models for the Image Search SDK. | |
| """ | |
| from dataclasses import dataclass, field | |
| from typing import List, Optional, Dict, Any, Union | |
| from datetime import datetime | |
| class Product: | |
| """Product model for indexing and search results.""" | |
| id: str | |
| title: str | |
| image_url: Optional[str] = None | |
| image_path: Optional[str] = None | |
| category: Optional[str] = None | |
| brand: Optional[str] = None | |
| price: Optional[float] = None | |
| rating: Optional[float] = None | |
| description: Optional[str] = None | |
| metadata: Dict[str, Any] = field(default_factory=dict) | |
| def to_dict(self) -> Dict[str, Any]: | |
| """Convert to dictionary for API requests.""" | |
| return { | |
| 'id': self.id, | |
| 'title': self.title, | |
| 'image_url': self.image_url, | |
| 'image_path': self.image_path, | |
| 'category': self.category, | |
| 'brand': self.brand, | |
| 'price': self.price, | |
| 'rating': self.rating, | |
| 'description': self.description, | |
| 'metadata': self.metadata | |
| } | |
| def from_dict(cls, data: Dict[str, Any]) -> 'Product': | |
| """Create Product from dictionary.""" | |
| return cls(**data) | |
| class SearchResult: | |
| """Search result model.""" | |
| product: Product | |
| similarity_score: float | |
| rank: int | |
| search_time_ms: int | |
| def to_dict(self) -> Dict[str, Any]: | |
| """Convert to dictionary.""" | |
| return { | |
| 'product': self.product.to_dict(), | |
| 'similarity_score': self.similarity_score, | |
| 'rank': self.rank, | |
| 'search_time_ms': self.search_time_ms | |
| } | |
| class SearchOptions: | |
| """Search options and filters.""" | |
| top_k: int = 10 | |
| min_price: Optional[float] = None | |
| max_price: Optional[float] = None | |
| category: Optional[str] = None | |
| brand: Optional[str] = None | |
| title_substring: Optional[str] = None | |
| min_similarity: float = 0.0 | |
| def to_dict(self) -> Dict[str, Any]: | |
| """Convert to dictionary for API requests.""" | |
| return { | |
| 'top_k': self.top_k, | |
| 'min_price': self.min_price, | |
| 'max_price': self.max_price, | |
| 'category': self.category, | |
| 'brand': self.brand, | |
| 'title_substring': self.title_substring, | |
| 'min_similarity': self.min_similarity | |
| } | |
| class IndexingStatus: | |
| """Indexing operation status.""" | |
| total_products: int | |
| indexed_products: int | |
| failed_products: int | |
| is_complete: bool | |
| start_time: datetime | |
| end_time: Optional[datetime] = None | |
| errors: List[str] = field(default_factory=list) | |
| def progress_percentage(self) -> float: | |
| """Get indexing progress as percentage.""" | |
| if self.total_products == 0: | |
| return 0.0 | |
| return (self.indexed_products / self.total_products) * 100 | |
| def success_rate(self) -> float: | |
| """Get success rate as percentage.""" | |
| if self.total_products == 0: | |
| return 0.0 | |
| return (self.indexed_products / self.total_products) * 100 | |
| class SystemStatus: | |
| """System health and status information.""" | |
| is_healthy: bool | |
| total_products: int | |
| indexed_products: int | |
| last_indexing_time: Optional[datetime] | |
| search_latency_ms: float | |
| memory_usage_mb: float | |
| version: str | |
| uptime_seconds: int | |
| def to_dict(self) -> Dict[str, Any]: | |
| """Convert to dictionary.""" | |
| return { | |
| 'is_healthy': self.is_healthy, | |
| 'total_products': self.total_products, | |
| 'indexed_products': self.indexed_products, | |
| 'last_indexing_time': self.last_indexing_time.isoformat() if self.last_indexing_time else None, | |
| 'search_latency_ms': self.search_latency_ms, | |
| 'memory_usage_mb': self.memory_usage_mb, | |
| 'version': self.version, | |
| 'uptime_seconds': self.uptime_seconds | |
| } | |
| class DatabaseConfig: | |
| """Database connection configuration.""" | |
| connection_string: str | |
| table_name: str = "products" | |
| id_column: str = "id" | |
| title_column: str = "title" | |
| image_url_column: str = "image_url" | |
| category_column: Optional[str] = None | |
| brand_column: Optional[str] = None | |
| price_column: Optional[str] = None | |
| rating_column: Optional[str] = None | |
| description_column: Optional[str] = None | |
| def to_dict(self) -> Dict[str, Any]: | |
| """Convert to dictionary.""" | |
| return { | |
| 'connection_string': self.connection_string, | |
| 'table_name': self.table_name, | |
| 'id_column': self.id_column, | |
| 'title_column': self.title_column, | |
| 'image_url_column': self.image_url_column, | |
| 'category_column': self.category_column, | |
| 'brand_column': self.brand_column, | |
| 'price_column': self.price_column, | |
| 'rating_column': self.rating_column, | |
| 'description_column': self.description_column | |
| } | |