File size: 10,025 Bytes
0b38b66 | 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | # Hugging Face Integration Guide
## Overview
Byte Dream now includes full Hugging Face Hub integration, allowing you to:
- Upload trained models to HF Hub with `push_to_hub()`
- Download and use models from HF Hub with `from_pretrained()`
- Load models directly in the generator using `hf_repo_id` parameter
- Deploy to Hugging Face Spaces easily
## Quick Start
### 1. Get Your Hugging Face Token
1. Go to https://huggingface.co/settings/tokens
2. Click "New token"
3. Give it a name (e.g., "Byte Dream")
4. Select "Write" permissions
5. Copy the token (starts with `hf_...`)
### 2. Upload Your Model to Hugging Face
After training your model:
```bash
# Method 1: Interactive (recommended)
python publish_to_hf.py
# You'll be prompted for:
# - Your HF token
# - Repository ID (e.g., Enzo8930302/ByteDream)
```
Or programmatically:
```python
from bytedream import ByteDreamGenerator
# Load your trained model
generator = ByteDreamGenerator(model_path="./models/bytedream")
# Upload to Hugging Face
generator.push_to_hub(
repo_id="your_username/ByteDream",
token="hf_xxxxxxxxxxxxx", # Your HF token
private=False, # Set True for private model
)
```
### 3. Use Model from Hugging Face
#### Python API
```python
from bytedream import ByteDreamGenerator
# Load directly from HF Hub
generator = ByteDreamGenerator(hf_repo_id="your_username/ByteDream")
# Generate image
image = generator.generate(
prompt="A beautiful sunset over mountains",
num_inference_steps=50,
guidance_scale=7.5
)
image.save("output.png")
```
#### Command Line
```bash
# Generate using model from HF
python infer.py \
--prompt "A dragon flying over castle" \
--hf_repo "your_username/ByteDream" \
--output dragon.png
```
#### Gradio Web Interface
```bash
# Set environment variable
export HF_REPO_ID=your_username/ByteDream
# Run app (will load from HF)
python app.py
```
## Detailed Usage
### Upload Methods
#### Method 1: publish_to_hf.py (Recommended)
```bash
python publish_to_hf.py [token] [repo_id]
# Examples:
python publish_to_hf.py
python publish_to_hf.py hf_xxxx Enzo8930302/ByteDream
```
#### Method 2: upload_to_hf.py
```bash
python upload_to_hf.py \
--model_path ./models/bytedream \
--repo_id your_username/ByteDream \
--token hf_xxxx \
--private # Optional: make repository private
```
#### Method 3: Python API
```python
from bytedream import ByteDreamGenerator
generator = ByteDreamGenerator(model_path="./models/bytedream")
generator.push_to_hub(
repo_id="your_username/ByteDream",
token="hf_xxxx",
private=False,
commit_message="Upload Byte Dream model v1.0"
)
```
### Download/Load Methods
#### Method 1: Generator with hf_repo_id
```python
from bytedream import ByteDreamGenerator
# Automatically downloads from HF
generator = ByteDreamGenerator(
hf_repo_id="your_username/ByteDream",
config_path="config.yaml",
device="cpu"
)
```
#### Method 2: Pipeline from_pretrained
```python
from bytedream.pipeline import ByteDreamPipeline
import torch
# Load pipeline directly from HF
pipeline = ByteDreamPipeline.from_pretrained(
"your_username/ByteDream",
device="cpu",
dtype=torch.float32
)
# Generate
result = pipeline(
prompt="Your prompt here",
num_inference_steps=50,
guidance_scale=7.5
)
result[0].save("output.png")
```
#### Method 3: Local Directory
```python
from bytedream.pipeline import ByteDreamPipeline
# First download manually or save locally
pipeline = ByteDreamPipeline.from_pretrained(
"./models/bytedream", # Local path
device="cpu"
)
```
## Deploy to Hugging Face Spaces
### Option 1: Manual Deployment
1. **Create Space**
- Go to https://huggingface.co/spaces
- Click "Create new Space"
- Choose Gradio SDK
- Select CPU hardware (Basic tier is free)
2. **Upload Files**
```bash
cd your_space_directory
git lfs install
git clone https://huggingface.co/spaces/your_username/your_space
cp -r ../Byte Dream/* your_space/
git add .
git commit -m "Initial commit"
git push
```
3. **Set Environment Variable**
- In your Space settings
- Add `HF_REPO_ID` variable with value `your_username/ByteDream`
4. **Deploy**
- The app will automatically deploy
- Available at: `https://huggingface.co/spaces/your_username/your_space`
### Option 2: Using Spaces SDK
```python
# In your Byte Dream directory
from huggingface_hub import HfApi
api = HfApi()
# Create and push space
api.create_repo(
repo_id="your_username/ByteDream-Space",
repo_type="space",
space_sdk="gradio",
token="hf_xxxx"
)
api.upload_folder(
folder_path=".",
repo_id="your_username/ByteDream-Space",
repo_type="space",
token="hf_xxxx"
)
```
## Configuration
### Environment Variables
```bash
# Load model from HF in app.py
export HF_REPO_ID=your_username/ByteDream
# Custom model path
export MODEL_PATH=./models/bytedream
```
### Model Files Structure
When uploaded to HF, your model will have this structure:
```
your_username/ByteDream/
βββ unet/
β βββ pytorch_model.bin # UNet weights
βββ vae/
β βββ pytorch_model.bin # VAE weights
βββ scheduler/
β βββ scheduler_config.json # Scheduler config
βββ model_index.json # Pipeline config
βββ config.yaml # Full configuration
βββ README.md # Model card
```
## Examples
### Example 1: Complete Workflow
```python
from bytedream import ByteDreamGenerator
# 1. Train model
# python train.py
# 2. Load trained model
generator = ByteDreamGenerator(model_path="./models/bytedream")
# 3. Test generation
image = generator.generate("Test prompt")
image.save("test.png")
# 4. Upload to HF
generator.push_to_hub(
repo_id="Enzo8930302/ByteDream",
token="hf_xxxx"
)
print("β Model uploaded!")
```
### Example 2: Use Community Models
```python
from bytedream import ByteDreamGenerator
# Load community model
generator = ByteDreamGenerator(
hf_repo_id="community-member/fantasy-model"
)
# Generate fantasy art
image = generator.generate(
prompt="Majestic dragon, fantasy landscape, dramatic lighting",
num_inference_steps=75,
guidance_scale=9.0
)
image.save("dragon.png")
```
### Example 3: Batch Processing
```python
from bytedream import ByteDreamGenerator
generator = ByteDreamGenerator(hf_repo_id="your_username/ByteDream")
prompts = [
"Sunset over mountains",
"Cyberpunk city at night",
"Fantasy castle in clouds",
"Underwater coral reef",
]
images = generator.generate_batch(
prompts=prompts,
width=512,
height=512,
num_inference_steps=50,
)
for i, img in enumerate(images):
img.save(f"image_{i}.png")
```
## Troubleshooting
### Error: "Repository not found"
**Solution**: Make sure the repository exists and is public, or you have proper authentication.
```python
# For private repos, provide token
generator = ByteDreamGenerator(
hf_repo_id="your_username/private-model",
config_path="config.yaml"
)
# Token should be configured in ~/.cache/huggingface/token
```
### Error: "Model not trained"
**Solution**: Train the model first or download pretrained weights.
```bash
# Train model
python train.py
# Or download from HF
python infer.py --hf_repo username/model --prompt "test"
```
### Error: "Out of memory"
**Solution**: Reduce image size or enable memory efficient mode.
```python
generator = ByteDreamGenerator(hf_repo_id="username/model")
generator.pipeline.enable_memory_efficient_mode()
image = generator.generate(
prompt="...",
width=256, # Smaller size
height=256,
)
```
## Best Practices
1. **Token Security**: Never commit your HF token to git
- Use environment variables
- Store in `~/.cache/huggingface/token`
2. **Model Versioning**: Use meaningful commit messages
```python
generator.push_to_hub(
repo_id="username/ByteDream",
commit_message="Add v2.0 with improved quality"
)
```
3. **Private Models**: For proprietary models
```python
generator.push_to_hub(
repo_id="username/private-model",
private=True
)
```
4. **Model Cards**: Include good README
- Describe training data
- Show example prompts
- List known limitations
## API Reference
### ByteDreamGenerator
```python
class ByteDreamGenerator:
def __init__(
self,
model_path: Optional[str] = None,
config_path: str = "config.yaml",
device: str = "cpu",
hf_repo_id: Optional[str] = None, # NEW!
)
def push_to_hub(
self,
repo_id: str,
token: Optional[str] = None,
private: bool = False,
commit_message: str = "Upload Byte Dream model",
)
def save_pretrained(self, save_directory: str)
```
### ByteDreamPipeline
```python
class ByteDreamPipeline:
@classmethod
def from_pretrained(
cls,
model_path: Union[str, Path], # Can be HF repo ID!
device: str = "cpu",
dtype: torch.dtype = torch.float32,
) -> "ByteDreamPipeline"
def save_pretrained(self, save_directory: Union[str, Path])
```
## Resources
- Hugging Face Hub: https://huggingface.co
- Documentation: https://huggingface.co/docs/hub
- Spaces: https://huggingface.co/spaces
- Token settings: https://huggingface.co/settings/tokens
## Support
For issues or questions:
1. Check this guide first
2. Review error messages carefully
3. Check Hugging Face documentation
4. Open GitHub issue
---
**Happy Generating! π¨**
|