Add README
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257# FLUX.2-dev Handler for HuggingFace Dedicated Endpoints
High-performance image generation using FLUX.2-dev with FP8 quantization for HuggingFace Dedicated Endpoints.
## Features
- **FP8 Quantization**: Via torchao for memory efficiency
- **Multi-Reference Images**: Up to 10 reference images for character/style consistency
- **torch.compile**: Optimized inference with reduce-overhead mode
- **1920x1080 Output**: Full HD resolution by default
## GPU Requirements
| GPU | VRAM | Mode | Recommended |
|-----|------|------|-------------|
| H200 | 141GB | Full GPU + FP8 + compile | Best |
| H100 | 80GB | Full GPU + FP8 + compile | Good |
| A100 | 80GB | Full GPU + FP8 | Minimum |
## Deployment Steps
### 1. Create HuggingFace Repository
```bash
# Install huggingface_hub CLI
pip install huggingface_hub
# Login
huggingface-cli login
# Create repository
huggingface-cli repo create flux2-dev-handler --type model
```
### 2. Push Handler Files
```bash
cd huggingface-endpoint/flux2-dev-handler
# Clone the repo
git clone https://huggingface.co/YOUR_USERNAME/flux2-dev-handler
cd flux2-dev-handler
# Copy handler files
cp ../handler.py .
cp ../requirements.txt .
# Commit and push
git add .
git commit -m "Add FLUX.2-dev handler"
git push
```
### 3. Create Dedicated Endpoint
1. Go to [HuggingFace Endpoints](https://ui.endpoints.huggingface.co/)
2. Click "New Endpoint"
3. Configure:
- **Repository**: `YOUR_USERNAME/flux2-dev-handler`
- **Cloud Provider**: AWS or Azure
- **Region**: Your preferred region
- **Instance Type**: `nvidia-h200-141gb` (recommended) or `nvidia-h100-80gb`
- **Scaling**: Min 0, Max 1 (scale-to-zero enabled)
- **Scale-to-Zero Timeout**: 15 minutes
4. Environment Variables:
- `HF_TOKEN`: Your HuggingFace token (for gated model access)
- `USE_FP8`: `true`
- `USE_COMPILE`: `true`
- `NUM_INFERENCE_STEPS`: `50`
5. Click "Create Endpoint"
### 4. Wait for Deployment
First deployment takes 10-15 minutes to:
- Download FLUX.2-dev model (~60GB)
- Apply FP8 quantization
- Run torch.compile warmup
### 5. Test the Endpoint
```bash
# Health check (via HuggingFace API)
curl "https://YOUR_ENDPOINT_URL" \
-H "Authorization: Bearer YOUR_HF_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"inputs": "test",
"parameters": {"mode": "generate", "num_inference_steps": 2, "width": 512, "height": 512}
}'
```
## API Reference
### Generate Image
**Request:**
```json
{
"inputs": "A beautiful sunset over mountains, photorealistic, 16:9",
"parameters": {
"mode": "generate",
"width": 1920,
"height": 1080,
"num_inference_steps": 50,
"guidance_scale": 4.0,
"seed": -1
}
}
```
**Response:**
```json
{
"image": "data:image/png;base64,...",
"generation_time_seconds": 12.5
}
```
### Generate with Reference Images
**Request:**
```json
{
"inputs": "the person from image 1 standing in a park, sunset lighting",
"parameters": {
"mode": "generate_with_refs",
"reference_images": [
"data:image/png;base64,..."
],
"width": 1920,
"height": 1080,
"num_inference_steps": 50,
"guidance_scale": 2.5,
"seed": -1
}
}
```
**Response:**
```json
{
"image": "data:image/png;base64,...",
"generation_time_seconds": 15.2,
"reference_count": 1
}
```
## Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `HF_TOKEN` | - | HuggingFace token (required) |
| `USE_FP8` | `true` | Enable FP8 quantization |
| `USE_COMPILE` | `true` | Enable torch.compile |
| `NUM_INFERENCE_STEPS` | `50` | Default inference steps |
## Endpoint Management
### Scale to Zero Manually
```python
from huggingface_hub import HfApi
api = HfApi()
api.scale_to_zero_inference_endpoint(
'flux2-dev-handler',
namespace='YOUR_USERNAME'
)
```
### Update Scale-to-Zero Timeout
```python
from huggingface_hub import HfApi
api = HfApi()
api.update_inference_endpoint(
'flux2-dev-handler',
namespace='YOUR_USERNAME',
scale_to_zero_timeout=15 # minutes
)
```
### Resume Endpoint
```python
from huggingface_hub import HfApi
api = HfApi()
api.resume_inference_endpoint(
'flux2-dev-handler',
namespace='YOUR_USERNAME'
)
```
## Frontend Integration
```typescript
// services/huggingfaceService.ts
const HF_TOKEN = import.meta.env.VITE_HF_TOKEN;
const ENDPOINT_URL = import.meta.env.VITE_HF_FLUX2_DEV_ENDPOINT_URL;
export const generateWithFlux2Dev = async (
prompt: string,
options?: {
referenceImages?: string[];
width?: number;
height?: number;
}
): Promise<string> => {
const hasRefs = options?.referenceImages && options.referenceImages.length > 0;
const response = await fetch(ENDPOINT_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${HF_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
inputs: prompt,
parameters: {
mode: hasRefs ? 'generate_with_refs' : 'generate',
reference_images: options?.referenceImages,
width: options?.width ?? 1920,
height: options?.height ?? 1080,
num_inference_steps: 50,
guidance_scale: hasRefs ? 2.5 : 4.0,
}
})
});
const result = await response.json();
return result.image;
};
```
## Troubleshooting
### Endpoint fails to start
- Check HF_TOKEN has access to FLUX.2-dev (gated model)
- Ensure GPU instance type has sufficient VRAM
### Slow cold start
- First inference after scale-up takes 2-3 minutes
- Model loading + torch.compile warmup is cached
### Out of memory
- Use H200 for best memory headroom
- Reduce image resolution if needed
## License
MITREADME.mdAdded+257