File size: 3,606 Bytes
af1d8cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 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()
|