File size: 5,184 Bytes
98ffc6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Deployment Guide for Hugging Face Spaces

## Step-by-Step Deployment

### 1. Create a Hugging Face Account
- Go to https://huggingface.co
- Sign up or log in

### 2. Create a New Space
1. Click on your profile β†’ "New Space"
2. Fill in the details:
   - **Space name**: `virtual-tryon-api` (or your preferred name)
   - **License**: Apache 2.0
   - **SDK**: Select **Docker**
   - **Hardware**: CPU (free) or upgrade to GPU for faster processing
3. Click "Create Space"

### 3. Upload Files to Your Space

You can upload files via:

#### Option A: Web Interface
1. In your Space, click "Files" β†’ "Add file" β†’ "Upload files"
2. Upload these files:
   - `app.py`
   - `requirements.txt`
   - `Dockerfile`
   - `README.md`
3. Commit the changes

#### Option B: Git (Recommended)
```bash

# Clone your Space repository

git clone https://huggingface.co/spaces/YOUR-USERNAME/virtual-tryon-api

cd virtual-tryon-api



# Copy files

cp /path/to/app.py .

cp /path/to/requirements.txt .

cp /path/to/Dockerfile .

cp /path/to/README.md .



# Commit and push

git add .

git commit -m "Initial commit: Virtual Try-On API"

git push

```

### 4. Wait for Build
- Your Space will automatically start building
- This may take 10-20 minutes for the first build
- You can view build logs in the "Logs" tab

### 5. Test Your Deployment
Once built, your API will be available at:
```

https://YOUR-USERNAME-virtual-tryon-api.hf.space

```

Test the health endpoint:
```bash

curl https://YOUR-USERNAME-virtual-tryon-api.hf.space/health

```

### 6. Update Your React Native App
In your React Native app, update the API URL:
```javascript

const API_URL = 'https://YOUR-USERNAME-virtual-tryon-api.hf.space';

```

## File Structure
```

your-space/

β”œβ”€β”€ app.py              # Main FastAPI application

β”œβ”€β”€ requirements.txt    # Python dependencies

β”œβ”€β”€ Dockerfile         # Docker configuration

└── README.md          # Space documentation

```

## Important Notes

### CPU vs GPU
- **CPU (Free)**: 
  - Free tier available
  - Slower processing (30-60 seconds per image)
  - Suitable for testing and low-volume use
  
- **GPU (Paid)**:
  - Faster processing (5-15 seconds per image)
  - Costs apply (check HF pricing)
  - Recommended for production

To enable GPU, modify your Space settings:
1. Go to Space Settings
2. Select GPU hardware
3. Rebuild

### Memory Considerations
The models are large (~10GB). Ensure your Space has enough memory:
- Minimum: 16GB RAM
- Recommended: 32GB RAM (especially for CPU)

### Model Download Time
On first run, models will download from Hugging Face:
- Stable Diffusion XL: ~7GB
- IP-Adapter: ~3GB
- Body Segmentation: ~100MB

This happens automatically but adds to initial startup time.

### Rate Limiting
For production use, consider adding rate limiting:

```python

from slowapi import Limiter

from slowapi.util import get_remote_address



limiter = Limiter(key_func=get_remote_address)

app.state.limiter = limiter



@app.post("/tryon")

@limiter.limit("5/minute")  # 5 requests per minute

async def virtual_tryon(...):

    # ... existing code

```

### Authentication
For secure API access, add authentication:

```python

from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials

from fastapi import Security



security = HTTPBearer()



def verify_token(credentials: HTTPAuthorizationCredentials = Security(security)):

    if credentials.credentials != "your-secret-token":

        raise HTTPException(status_code=401, detail="Invalid token")



@app.post("/tryon")

async def virtual_tryon(

    token: HTTPAuthorizationCredentials = Security(verify_token),

    ...

):

    # ... existing code

```

## Troubleshooting

### Build Fails
- Check build logs in the "Logs" tab
- Ensure all dependencies in requirements.txt are compatible
- Verify Dockerfile syntax

### Out of Memory
- Reduce model precision (use float16 instead of float32)
- Reduce batch size
- Upgrade to larger hardware tier

### Slow Performance
- Consider upgrading to GPU hardware
- Reduce `num_steps` parameter (trade quality for speed)
- Implement caching for repeated requests

### API Not Responding
- Check if Space is running (green status)
- Verify the URL is correct
- Check CORS settings if accessing from web

## Monitoring

Monitor your Space:
1. **Usage**: Check Space settings for API call statistics
2. **Logs**: View application logs in real-time
3. **Performance**: Monitor response times

## Updating Your Space

To update your deployed API:
```bash

# Make changes to your files

git add .

git commit -m "Update: description of changes"

git push

```

The Space will automatically rebuild with your changes.

## Support

- Hugging Face Docs: https://huggingface.co/docs/hub/spaces
- Community Forum: https://discuss.huggingface.co
- Discord: https://discord.gg/hugging-face

## Next Steps

1. Test your API thoroughly
2. Implement authentication for production
3. Add rate limiting
4. Monitor usage and optimize
5. Consider upgrading to GPU for better performance