bsure-api / scripts /seed_data.py
“mohansree14”
Deploy Bsure API with correct structure
af1d8cd
Raw History Blame Contribute Delete
3.61 kB
import os
import requests
import shutil
import uuid
from sqlmodel import Session, select
from models import User, WardrobeItem
from database import engine
from datetime import datetime
# Sample data mimicking a subset of a fashion dataset
SAMPLE_ITEMS = [
{
"category": "top",
"url": "https://images.unsplash.com/photo-1521572163474-6864f9cf17ab?w=600&q=80",
"description": "White Cotton T-Shirt"
},
{
"category": "top",
"url": "https://images.unsplash.com/photo-1618354691373-d851c5c3a990?w=600&q=80",
"description": "Black Graphic Tee"
},
{
"category": "top",
"url": "https://images.unsplash.com/photo-1551028919-ac66e6a46121?w=600&q=80",
"description": "Blue Denim Jacket"
},
{
"category": "bottom",
"url": "https://images.unsplash.com/photo-1542272617-08f086303294?w=600&q=80",
"description": "Patterned Summer Skirt"
},
{
"category": "bottom",
"url": "https://images.unsplash.com/photo-1584370848010-d7cc637703ef?w=600&q=80",
"description": "Grey Trousers"
},
{
"category": "bottom",
"url": "https://images.unsplash.com/photo-1541099649105-f69ad21f3246?w=600&q=80",
"description": "Blue Jeans"
},
{
"category": "shoes",
"url": "https://images.unsplash.com/photo-1549298916-b41d501d3772?w=600&q=80",
"description": "Running Shoes"
},
{
"category": "shoes",
"url": "https://images.unsplash.com/photo-1543163521-1bf539c55dd2?w=600&q=80",
"description": "High Heels"
}
]
UPLOAD_DIR = "static/uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
def download_image(url):
try:
response = requests.get(url, stream=True)
if response.status_code == 200:
file_ext = "jpg" # Assuming jpg for unsplash
filename = f"{uuid.uuid4()}.{file_ext}"
file_path = os.path.join(UPLOAD_DIR, filename)
with open(file_path, 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
return f"/static/uploads/{filename}"
except Exception as e:
print(f"Failed to download {url}: {e}")
return None
def seed():
print("Starting data seeding...")
with Session(engine) as session:
# Get or Create Demo User
user = session.exec(select(User).where(User.email == "demo@bsure.app")).first()
if not user:
print("Creating demo user...")
# Using dummy hash since we fixed the hasher to pbkdf2_sha256
user = User(
email="demo@bsure.app",
password_hash="$pbkdf2-sha256$29000$dummyhash$dummy",
full_name="Demo User"
)
session.add(user)
session.commit()
session.refresh(user)
print(f"Seeding wardrobe for user: {user.email}")
count = 0
for item_data in SAMPLE_ITEMS:
print(f"Downloading {item_data['description']}...")
local_url = download_image(item_data['url'])
if local_url:
db_item = WardrobeItem(
owner_id=user.id,
image_url=local_url,
category=item_data['category'],
tags="demo-data"
)
session.add(db_item)
count += 1
session.commit()
print(f"Successfully seeded {count} items!")
if __name__ == "__main__":
seed()