File size: 6,892 Bytes
8bcf79a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash

# Dressify - Deploy to Hugging Face Space
# This script prepares and deploys the outfit recommendation system to HF Spaces

set -e  # Exit on any error

# Configuration
SPACE_NAME="${SPACE_NAME:-dressify-outfit-recommendation}"
SPACE_SDK="${SPACE_SDK:-gradio}"
SPACE_HARDWARE="${SPACE_HARDWARE:-cpu-basic}"
SPACE_PRIVATE="${SPACE_PRIVATE:-false}"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}πŸš€ Deploying Dressify to Hugging Face Space${NC}"
echo "=================================================="

# Check if HF CLI is installed
if ! command -v huggingface-cli &> /dev/null; then
    echo -e "${YELLOW}⚠️  Hugging Face CLI not found${NC}"
    echo "Installing huggingface_hub..."
    pip install --upgrade huggingface_hub
fi

# Check if logged in to HF
if ! huggingface-cli whoami &> /dev/null; then
    echo -e "${RED}❌ Not logged in to Hugging Face${NC}"
    echo "Please login first:"
    echo "  huggingface-cli login"
    exit 1
fi

# Get username
USERNAME=$(huggingface-cli whoami)
echo -e "${GREEN}βœ… Logged in as: $USERNAME${NC}"

# Check if models are trained
EXPORT_DIR="models/exports"
if [ ! -f "$EXPORT_DIR/resnet_item_embedder_best.pth" ] || [ ! -f "$EXPORT_DIR/vit_outfit_model_best.pth" ]; then
    echo -e "${YELLOW}⚠️  Models not fully trained${NC}"
    echo "Training models first..."
    
    if [ ! -f "$EXPORT_DIR/resnet_item_embedder_best.pth" ]; then
        echo "Training ResNet..."
        ./scripts/train_item.sh
    fi
    
    if [ ! -f "$EXPORT_DIR/vit_outfit_model_best.pth" ]; then
        echo "Training ViT..."
        ./scripts/train_outfit.sh
    fi
fi

echo -e "${GREEN}βœ… All models are ready${NC}"

# Create Space configuration
echo -e "${BLUE}πŸ“ Creating Space configuration...${NC}"

# Update README.md with Space metadata
cat > README.md << EOF
---
title: Dressify - Production-Ready Outfit Recommendation
emoji: πŸ†
colorFrom: purple
colorTo: green
sdk: $SPACE_SDK
sdk_version: "5.44.1"
app_file: app.py
pinned: false
---

# Dressify - Production-Ready Outfit Recommendation System

A **research-grade, self-contained** outfit recommendation service that automatically downloads the Polyvore dataset, trains state-of-the-art models, and provides a sophisticated Gradio interface for wardrobe uploads and outfit generation.

## πŸš€ Features

- **Self-Contained**: No external dependencies or environment variables needed
- **Auto-Dataset Preparation**: Downloads and processes Stylique/Polyvore dataset automatically
- **Research-Grade Models**: ResNet50 item embedder + ViT outfit compatibility encoder
- **Advanced Training**: Triplet loss with semi-hard negative mining, mixed precision
- **Production UI**: Gradio interface with wardrobe upload, outfit preview, and JSON export
- **REST API**: FastAPI endpoints for embedding and composition
- **Auto-Bootstrap**: Background training and model reloading

## 🎯 Quick Start

1. **Upload Wardrobe**: Drag & drop multiple clothing images
2. **Set Context**: Choose occasion, weather, and style preferences
3. **Generate Outfits**: Get top-N outfit recommendations with compatibility scores
4. **View Results**: See stitched outfit previews and download JSON data

## πŸ”¬ Research Features

- **Triplet Loss**: Semi-hard negative mining for better embeddings
- **Mixed Precision**: CUDA-optimized training with autocast
- **Transformer Architecture**: ViT encoder for outfit-level compatibility
- **Slot Awareness**: Category-aware outfit composition

## πŸ“Š Model Performance

- **Item Embedder**: ResNet50 + projection head β†’ 512D embeddings
- **Outfit Encoder**: 6-layer transformer with 8 attention heads
- **Training Time**: ~2-4 hours on L4 GPU (full dataset)
- **Inference**: <100ms per outfit on GPU

## πŸš€ Deployment

This Space automatically:
1. Downloads the Stylique/Polyvore dataset
2. Prepares training splits and triplets
3. Trains models if no checkpoints exist
4. Launches the Gradio UI + FastAPI

## πŸ“š References

- **Dataset**: [Stylique/Polyvore](https://huggingface.co/datasets/Stylique/Polyvore)
- **Research**: Triplet loss, transformer encoders, outfit compatibility

---

**Built with ❀️ for the fashion AI community**
EOF

echo -e "${GREEN}βœ… Space configuration created${NC}"

# Check if Space already exists
SPACE_ID="$USERNAME/$SPACE_NAME"
if huggingface-cli repo info "$SPACE_ID" &> /dev/null; then
    echo -e "${YELLOW}⚠️  Space $SPACE_ID already exists${NC}"
    read -p "Do you want to update it? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Deployment cancelled"
        exit 0
    fi
fi

# Create or update Space
echo -e "${BLUE}πŸš€ Creating/updating Space: $SPACE_ID${NC}"

if [ "$SPACE_PRIVATE" = "true" ]; then
    PRIVATE_FLAG="--private"
else
    PRIVATE_FLAG=""
fi

# Create Space
huggingface-cli repo create "$SPACE_NAME" \
    --type space \
    --space-sdk "$SPACE_SDK" \
    --space-hardware "$SPACE_HARDWARE" \
    $PRIVATE_FLAG

# Push code to Space
echo -e "${BLUE}πŸ“€ Pushing code to Space...${NC}"

# Initialize git if not already done
if [ ! -d ".git" ]; then
    git init
    git add .
    git commit -m "Initial commit: Dressify outfit recommendation system"
fi

# Add HF Space as remote
git remote remove origin 2>/dev/null || true
git remote add origin "https://huggingface.co/spaces/$SPACE_ID"

# Push to Space
git push -u origin main --force

echo -e "${GREEN}βœ… Code pushed to Space successfully!${NC}"

# Push models to HF Hub (optional)
read -p "Do you want to push trained models to HF Hub? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${BLUE}πŸ“€ Pushing models to HF Hub...${NC}"
    
    # Push ResNet model
    python utils/hf_utils.py \
        --action push \
        --checkpoint "$EXPORT_DIR/resnet_item_embedder_best.pth" \
        --model-name "dressify-resnet-embedder"
    
    # Push ViT model
    python utils/hf_utils.py \
        --action push \
        --checkpoint "$EXPORT_DIR/vit_outfit_model_best.pth" \
        --model-name "dressify-vit-outfit-encoder"
    
    echo -e "${GREEN}βœ… Models pushed to HF Hub${NC}"
fi

echo ""
echo -e "${GREEN}πŸŽ‰ Deployment completed successfully!${NC}"
echo ""
echo -e "${BLUE}🌐 Your Space is available at:${NC}"
echo -e "  https://huggingface.co/spaces/$SPACE_ID"
echo ""
echo -e "${BLUE}πŸ“‹ Next steps:${NC}"
echo "1. Wait for Space to build (usually 5-10 minutes)"
echo "2. Test the outfit recommendation interface"
echo "3. Monitor training progress in the Status tab"
echo "4. Download trained models from the Downloads tab"
echo ""
echo -e "${BLUE}πŸ”§ Space Management:${NC}"
echo "  View Space: https://huggingface.co/spaces/$SPACE_ID"
echo "  Settings: https://huggingface.co/spaces/$SPACE_ID/settings"
echo "  Logs: https://huggingface.co/spaces/$SPACE_ID/logs"