image-search / services /product_service.py
izuhasohail's picture
updated
2f9cab0
import random
from bson import ObjectId
# Constants for mock data generation
BRANDS = ["Nike", "Adidas", "H&M", "Zara", "Uniqlo", "Gap", "Levi's"]
COLORS = ["Black", "White", "Blue", "Red", "Green", "Yellow", "Purple", "Pink", "Orange", "Grey"]
GENDERS = ["Male", "Female"]
SIZE_OPTIONS = ["XS", "S", "M", "L", "XL"]
def generate_mock_product_data():
"""Generate realistic product data if missing."""
sample_products = [
{"product_name": "Stylish Shirt", "brand_name": "Zara", "color": "Blue", "price": "$29.99"},
{"product_name": "Casual Sneakers", "brand_name": "Nike", "color": "White", "price": "$79.99"},
{"product_name": "Leather Jacket", "brand_name": "H&M", "color": "Black", "price": "$99.99"},
]
selected_product = random.choice(sample_products) # Pick a random sample
mock_data = {
"product_name": selected_product["product_name"],
"brand_name": selected_product["brand_name"],
"price": selected_product["price"],
"description": f"A trendy {selected_product['color']} {selected_product['product_name']} perfect for all occasions.",
"gender": "Female",
"color": selected_product["color"],
"stock_level": 50,
"manufacturing_price": round(random.uniform(10.0, 50.0), 2),
"category": "T-shirts",
"size_options": SIZE_OPTIONS,
"features": [random.random() for _ in range(2048)], # Simulated embeddings
}
print("Generated mock product data:", mock_data) # Debugging print
return mock_data
def create_product(product_image_path, model_image_path, product_data, db, cloudinary_service, feature_service):
"""Create a new product with all required data"""
print("Received product data:", product_data) # Debugging print
# Generate mock data for missing fields
mock_data = generate_mock_product_data()
complete_product_data = {key: product_data.get(key, mock_data[key]) for key in mock_data}
print("Product data after merging with mock data:", complete_product_data) # Debugging print
# Upload images to Cloudinary
image_urls = cloudinary_service.upload_multiple_to_cloudinary([product_image_path, model_image_path])
# Extract features and generate auto description
analysis_result = feature_service.analyze_product_image(product_image_path)
# Merge product data with generated details
complete_product = {
**complete_product_data,
"image_urls": image_urls,
"features": analysis_result["features"],
"auto_description": analysis_result["auto_description"]
}
print("Final product data before saving:", complete_product) # Debugging print
# Save to database
product_id = db.save_product_data(complete_product)
return product_id, complete_product