Spaces:
Sleeping
Sleeping
Download sdk/python/examples/quick_start.py from fmbishu/image-search-api: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/fmbishu/image-search-api/resolve/main/sdk/python/examples/quick_start.py
- Command line
-
hf download hf://spaces/fmbishu/image-search-api/sdk/python/examples/quick_start.py
-
curl -L -o quick_start.py https://huggingface.co/spaces/fmbishu/image-search-api/resolve/main/sdk/python/examples/quick_start.py
2.67 kB
| """ | |
| Quick Start Example - Minimal setup to get started with the SDK. | |
| This example shows the fastest way to integrate the Image Search SDK | |
| into your application with minimal configuration. | |
| """ | |
| import sys | |
| from pathlib import Path | |
| # Add SDK to path | |
| sdk_path = Path(__file__).parent.parent | |
| sys.path.insert(0, str(sdk_path)) | |
| from image_search_sdk import ImageSearchClient, Product, SearchOptions | |
| def main(): | |
| print("Quick Start Example - Image Search SDK") | |
| print("=" * 60) | |
| # Step 1: Initialize the SDK client | |
| print("\n1. Initializing SDK...") | |
| client = ImageSearchClient( | |
| data_dir="./quick_start_data", | |
| model_name="clip-ViT-L-14", # High-quality model | |
| vector_store_type="chromadb", # Simple local storage | |
| vector_store_config={ | |
| "mode": "persistent", | |
| "path": "./quick_start_data/chroma" | |
| } | |
| ) | |
| print(" ✓ SDK initialized") | |
| # Step 2: Connect to your product database | |
| print("\n2. Connecting to database...") | |
| client.connect_database( | |
| connection_string="sqlite:///your_products.db", # Replace with your DB | |
| table_name="products", | |
| id_column="id", | |
| title_column="name", # Map your column names | |
| image_url_column="image" | |
| ) | |
| print(" ✓ Database connected") | |
| # Step 3: First-time initialization (generates embeddings) | |
| print("\n3. Initializing embeddings...") | |
| stats = client.initialize(batch_size=50) | |
| print(f" ✓ Indexed {stats['indexed_products']} products in {stats['duration_seconds']:.1f}s") | |
| # Step 4: Enable automatic embedding generation for new products | |
| print("\n4. Enabling auto-monitor...") | |
| client.enable_auto_monitor(poll_interval=60, enable=True) | |
| print(" ✓ Auto-monitor enabled (checks every 60s)") | |
| # Step 5: Search! | |
| print("\n5. Testing search...") | |
| # Search by text | |
| results = client.search_by_text("blue shirt", options=SearchOptions(top_k=5)) | |
| print(f"\n Found {len(results)} results for 'blue shirt':") | |
| for i, result in enumerate(results, 1): | |
| print(f" {i}. {result.product.title} (score: {result.similarity_score:.3f})") | |
| # Search by image (if you have a local image) | |
| # results = client.search_by_image("./sample_image.jpg", options=SearchOptions(top_k=5)) | |
| print("\n" + "=" * 60) | |
| print("Setup complete! Your image search is now running.") | |
| print("=" * 60) | |
| # Keep running (auto-monitor is active in background) | |
| try: | |
| input("\nPress Enter to exit...") | |
| finally: | |
| client.close() | |
| print("Shutdown complete.") | |
| if __name__ == "__main__": | |
| main() | |