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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Integration Guide
2
+
3
+ ## Overview
4
+
5
+ Byte Dream now includes full Hugging Face Hub integration, allowing you to:
6
+ - Upload trained models to HF Hub with `push_to_hub()`
7
+ - Download and use models from HF Hub with `from_pretrained()`
8
+ - Load models directly in the generator using `hf_repo_id` parameter
9
+ - Deploy to Hugging Face Spaces easily
10
+
11
+ ## Quick Start
12
+
13
+ ### 1. Get Your Hugging Face Token
14
+
15
+ 1. Go to https://huggingface.co/settings/tokens
16
+ 2. Click "New token"
17
+ 3. Give it a name (e.g., "Byte Dream")
18
+ 4. Select "Write" permissions
19
+ 5. Copy the token (starts with `hf_...`)
20
+
21
+ ### 2. Upload Your Model to Hugging Face
22
+
23
+ After training your model:
24
+
25
+ ```bash
26
+ # Method 1: Interactive (recommended)
27
+ python publish_to_hf.py
28
+
29
+ # You'll be prompted for:
30
+ # - Your HF token
31
+ # - Repository ID (e.g., Enzo8930302/ByteDream)
32
+ ```
33
+
34
+ Or programmatically:
35
+
36
+ ```python
37
+ from bytedream import ByteDreamGenerator
38
+
39
+ # Load your trained model
40
+ generator = ByteDreamGenerator(model_path="./models/bytedream")
41
+
42
+ # Upload to Hugging Face
43
+ generator.push_to_hub(
44
+ repo_id="your_username/ByteDream",
45
+ token="hf_xxxxxxxxxxxxx", # Your HF token
46
+ private=False, # Set True for private model
47
+ )
48
+ ```
49
+
50
+ ### 3. Use Model from Hugging Face
51
+
52
+ #### Python API
53
+
54
+ ```python
55
+ from bytedream import ByteDreamGenerator
56
+
57
+ # Load directly from HF Hub
58
+ generator = ByteDreamGenerator(hf_repo_id="your_username/ByteDream")
59
+
60
+ # Generate image
61
+ image = generator.generate(
62
+ prompt="A beautiful sunset over mountains",
63
+ num_inference_steps=50,
64
+ guidance_scale=7.5
65
+ )
66
+
67
+ image.save("output.png")
68
+ ```
69
+
70
+ #### Command Line
71
+
72
+ ```bash
73
+ # Generate using model from HF
74
+ python infer.py \
75
+ --prompt "A dragon flying over castle" \
76
+ --hf_repo "your_username/ByteDream" \
77
+ --output dragon.png
78
+ ```
79
+
80
+ #### Gradio Web Interface
81
+
82
+ ```bash
83
+ # Set environment variable
84
+ export HF_REPO_ID=your_username/ByteDream
85
+
86
+ # Run app (will load from HF)
87
+ python app.py
88
+ ```
89
+
90
+ ## Detailed Usage
91
+
92
+ ### Upload Methods
93
+
94
+ #### Method 1: publish_to_hf.py (Recommended)
95
+
96
+ ```bash
97
+ python publish_to_hf.py [token] [repo_id]
98
+
99
+ # Examples:
100
+ python publish_to_hf.py
101
+ python publish_to_hf.py hf_xxxx Enzo8930302/ByteDream
102
+ ```
103
+
104
+ #### Method 2: upload_to_hf.py
105
+
106
+ ```bash
107
+ python upload_to_hf.py \
108
+ --model_path ./models/bytedream \
109
+ --repo_id your_username/ByteDream \
110
+ --token hf_xxxx \
111
+ --private # Optional: make repository private
112
+ ```
113
+
114
+ #### Method 3: Python API
115
+
116
+ ```python
117
+ from bytedream import ByteDreamGenerator
118
+
119
+ generator = ByteDreamGenerator(model_path="./models/bytedream")
120
+
121
+ generator.push_to_hub(
122
+ repo_id="your_username/ByteDream",
123
+ token="hf_xxxx",
124
+ private=False,
125
+ commit_message="Upload Byte Dream model v1.0"
126
+ )
127
+ ```
128
+
129
+ ### Download/Load Methods
130
+
131
+ #### Method 1: Generator with hf_repo_id
132
+
133
+ ```python
134
+ from bytedream import ByteDreamGenerator
135
+
136
+ # Automatically downloads from HF
137
+ generator = ByteDreamGenerator(
138
+ hf_repo_id="your_username/ByteDream",
139
+ config_path="config.yaml",
140
+ device="cpu"
141
+ )
142
+ ```
143
+
144
+ #### Method 2: Pipeline from_pretrained
145
+
146
+ ```python
147
+ from bytedream.pipeline import ByteDreamPipeline
148
+ import torch
149
+
150
+ # Load pipeline directly from HF
151
+ pipeline = ByteDreamPipeline.from_pretrained(
152
+ "your_username/ByteDream",
153
+ device="cpu",
154
+ dtype=torch.float32
155
+ )
156
+
157
+ # Generate
158
+ result = pipeline(
159
+ prompt="Your prompt here",
160
+ num_inference_steps=50,
161
+ guidance_scale=7.5
162
+ )
163
+
164
+ result[0].save("output.png")
165
+ ```
166
+
167
+ #### Method 3: Local Directory
168
+
169
+ ```python
170
+ from bytedream.pipeline import ByteDreamPipeline
171
+
172
+ # First download manually or save locally
173
+ pipeline = ByteDreamPipeline.from_pretrained(
174
+ "./models/bytedream", # Local path
175
+ device="cpu"
176
+ )
177
+ ```
178
+
179
+ ## Deploy to Hugging Face Spaces
180
+
181
+ ### Option 1: Manual Deployment
182
+
183
+ 1. **Create Space**
184
+ - Go to https://huggingface.co/spaces
185
+ - Click "Create new Space"
186
+ - Choose Gradio SDK
187
+ - Select CPU hardware (Basic tier is free)
188
+
189
+ 2. **Upload Files**
190
+ ```bash
191
+ cd your_space_directory
192
+ git lfs install
193
+ git clone https://huggingface.co/spaces/your_username/your_space
194
+ cp -r ../Byte Dream/* your_space/
195
+ git add .
196
+ git commit -m "Initial commit"
197
+ git push
198
+ ```
199
+
200
+ 3. **Set Environment Variable**
201
+ - In your Space settings
202
+ - Add `HF_REPO_ID` variable with value `your_username/ByteDream`
203
+
204
+ 4. **Deploy**
205
+ - The app will automatically deploy
206
+ - Available at: `https://huggingface.co/spaces/your_username/your_space`
207
+
208
+ ### Option 2: Using Spaces SDK
209
+
210
+ ```python
211
+ # In your Byte Dream directory
212
+ from huggingface_hub import HfApi
213
+
214
+ api = HfApi()
215
+
216
+ # Create and push space
217
+ api.create_repo(
218
+ repo_id="your_username/ByteDream-Space",
219
+ repo_type="space",
220
+ space_sdk="gradio",
221
+ token="hf_xxxx"
222
+ )
223
+
224
+ api.upload_folder(
225
+ folder_path=".",
226
+ repo_id="your_username/ByteDream-Space",
227
+ repo_type="space",
228
+ token="hf_xxxx"
229
+ )
230
+ ```
231
+
232
+ ## Configuration
233
+
234
+ ### Environment Variables
235
+
236
+ ```bash
237
+ # Load model from HF in app.py
238
+ export HF_REPO_ID=your_username/ByteDream
239
+
240
+ # Custom model path
241
+ export MODEL_PATH=./models/bytedream
242
+ ```
243
+
244
+ ### Model Files Structure
245
+
246
+ When uploaded to HF, your model will have this structure:
247
+
248
+ ```
249
+ your_username/ByteDream/
250
+ β”œβ”€β”€ unet/
251
+ β”‚ └── pytorch_model.bin # UNet weights
252
+ β”œβ”€β”€ vae/
253
+ β”‚ └── pytorch_model.bin # VAE weights
254
+ β”œβ”€β”€ scheduler/
255
+ β”‚ └── scheduler_config.json # Scheduler config
256
+ β”œβ”€β”€ model_index.json # Pipeline config
257
+ β”œβ”€β”€ config.yaml # Full configuration
258
+ └── README.md # Model card
259
+ ```
260
+
261
+ ## Examples
262
+
263
+ ### Example 1: Complete Workflow
264
+
265
+ ```python
266
+ from bytedream import ByteDreamGenerator
267
+
268
+ # 1. Train model
269
+ # python train.py
270
+
271
+ # 2. Load trained model
272
+ generator = ByteDreamGenerator(model_path="./models/bytedream")
273
+
274
+ # 3. Test generation
275
+ image = generator.generate("Test prompt")
276
+ image.save("test.png")
277
+
278
+ # 4. Upload to HF
279
+ generator.push_to_hub(
280
+ repo_id="Enzo8930302/ByteDream",
281
+ token="hf_xxxx"
282
+ )
283
+
284
+ print("βœ“ Model uploaded!")
285
+ ```
286
+
287
+ ### Example 2: Use Community Models
288
+
289
+ ```python
290
+ from bytedream import ByteDreamGenerator
291
+
292
+ # Load community model
293
+ generator = ByteDreamGenerator(
294
+ hf_repo_id="community-member/fantasy-model"
295
+ )
296
+
297
+ # Generate fantasy art
298
+ image = generator.generate(
299
+ prompt="Majestic dragon, fantasy landscape, dramatic lighting",
300
+ num_inference_steps=75,
301
+ guidance_scale=9.0
302
+ )
303
+
304
+ image.save("dragon.png")
305
+ ```
306
+
307
+ ### Example 3: Batch Processing
308
+
309
+ ```python
310
+ from bytedream import ByteDreamGenerator
311
+
312
+ generator = ByteDreamGenerator(hf_repo_id="your_username/ByteDream")
313
+
314
+ prompts = [
315
+ "Sunset over mountains",
316
+ "Cyberpunk city at night",
317
+ "Fantasy castle in clouds",
318
+ "Underwater coral reef",
319
+ ]
320
+
321
+ images = generator.generate_batch(
322
+ prompts=prompts,
323
+ width=512,
324
+ height=512,
325
+ num_inference_steps=50,
326
+ )
327
+
328
+ for i, img in enumerate(images):
329
+ img.save(f"image_{i}.png")
330
+ ```
331
+
332
+ ## Troubleshooting
333
+
334
+ ### Error: "Repository not found"
335
+
336
+ **Solution**: Make sure the repository exists and is public, or you have proper authentication.
337
+
338
+ ```python
339
+ # For private repos, provide token
340
+ generator = ByteDreamGenerator(
341
+ hf_repo_id="your_username/private-model",
342
+ config_path="config.yaml"
343
+ )
344
+ # Token should be configured in ~/.cache/huggingface/token
345
+ ```
346
+
347
+ ### Error: "Model not trained"
348
+
349
+ **Solution**: Train the model first or download pretrained weights.
350
+
351
+ ```bash
352
+ # Train model
353
+ python train.py
354
+
355
+ # Or download from HF
356
+ python infer.py --hf_repo username/model --prompt "test"
357
+ ```
358
+
359
+ ### Error: "Out of memory"
360
+
361
+ **Solution**: Reduce image size or enable memory efficient mode.
362
+
363
+ ```python
364
+ generator = ByteDreamGenerator(hf_repo_id="username/model")
365
+ generator.pipeline.enable_memory_efficient_mode()
366
+
367
+ image = generator.generate(
368
+ prompt="...",
369
+ width=256, # Smaller size
370
+ height=256,
371
+ )
372
+ ```
373
+
374
+ ## Best Practices
375
+
376
+ 1. **Token Security**: Never commit your HF token to git
377
+ - Use environment variables
378
+ - Store in `~/.cache/huggingface/token`
379
+
380
+ 2. **Model Versioning**: Use meaningful commit messages
381
+ ```python
382
+ generator.push_to_hub(
383
+ repo_id="username/ByteDream",
384
+ commit_message="Add v2.0 with improved quality"
385
+ )
386
+ ```
387
+
388
+ 3. **Private Models**: For proprietary models
389
+ ```python
390
+ generator.push_to_hub(
391
+ repo_id="username/private-model",
392
+ private=True
393
+ )
394
+ ```
395
+
396
+ 4. **Model Cards**: Include good README
397
+ - Describe training data
398
+ - Show example prompts
399
+ - List known limitations
400
+
401
+ ## API Reference
402
+
403
+ ### ByteDreamGenerator
404
+
405
+ ```python
406
+ class ByteDreamGenerator:
407
+ def __init__(
408
+ self,
409
+ model_path: Optional[str] = None,
410
+ config_path: str = "config.yaml",
411
+ device: str = "cpu",
412
+ hf_repo_id: Optional[str] = None, # NEW!
413
+ )
414
+
415
+ def push_to_hub(
416
+ self,
417
+ repo_id: str,
418
+ token: Optional[str] = None,
419
+ private: bool = False,
420
+ commit_message: str = "Upload Byte Dream model",
421
+ )
422
+
423
+ def save_pretrained(self, save_directory: str)
424
+ ```
425
+
426
+ ### ByteDreamPipeline
427
+
428
+ ```python
429
+ class ByteDreamPipeline:
430
+ @classmethod
431
+ def from_pretrained(
432
+ cls,
433
+ model_path: Union[str, Path], # Can be HF repo ID!
434
+ device: str = "cpu",
435
+ dtype: torch.dtype = torch.float32,
436
+ ) -> "ByteDreamPipeline"
437
+
438
+ def save_pretrained(self, save_directory: Union[str, Path])
439
+ ```
440
+
441
+ ## Resources
442
+
443
+ - Hugging Face Hub: https://huggingface.co
444
+ - Documentation: https://huggingface.co/docs/hub
445
+ - Spaces: https://huggingface.co/spaces
446
+ - Token settings: https://huggingface.co/settings/tokens
447
+
448
+ ## Support
449
+
450
+ For issues or questions:
451
+ 1. Check this guide first
452
+ 2. Review error messages carefully
453
+ 3. Check Hugging Face documentation
454
+ 4. Open GitHub issue
455
+
456
+ ---
457
+
458
+ **Happy Generating! 🎨**