Spaces:
Runtime error
Runtime error
Aguilar Elizondo commited on
Commit Β·
e902d68
1
Parent(s): f6c0ca4
Add tabbed interface with visible Training Guide and About sections
Browse files
app.py
CHANGED
|
@@ -116,28 +116,288 @@ def enhance_image_simple(
|
|
| 116 |
logger.error(f"Enhancement failed: {e}", exc_info=True)
|
| 117 |
raise gr.Error(f"Enhancement failed: {str(e)}")
|
| 118 |
|
| 119 |
-
# Create
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 minutes on CPU
|
|
|
|
| 116 |
logger.error(f"Enhancement failed: {e}", exc_info=True)
|
| 117 |
raise gr.Error(f"Enhancement failed: {str(e)}")
|
| 118 |
|
| 119 |
+
# Create interface with tabs
|
| 120 |
+
with gr.Blocks(title="ποΈ Architecture AI Enhancer") as demo:
|
| 121 |
+
gr.Markdown("""
|
| 122 |
+
# ποΈ Architecture AI Enhancer
|
| 123 |
+
Transform your architectural renders with AI-powered enhancement using Stable Diffusion 1.5
|
| 124 |
+
""")
|
| 125 |
+
|
| 126 |
+
with gr.Tabs():
|
| 127 |
+
# Tab 1: Enhancement
|
| 128 |
+
with gr.Tab("β¨ Enhance Image"):
|
| 129 |
+
with gr.Row():
|
| 130 |
+
with gr.Column():
|
| 131 |
+
input_image = gr.Image(label="π€ Input Image", type="pil")
|
| 132 |
+
|
| 133 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 134 |
+
strength = gr.Slider(
|
| 135 |
+
0.1, 0.8, value=0.3, step=0.05,
|
| 136 |
+
label="Denoising Strength",
|
| 137 |
+
info="Lower = more faithful to input, Higher = more creative"
|
| 138 |
+
)
|
| 139 |
+
guidance_scale = gr.Slider(
|
| 140 |
+
1.0, 15.0, value=5.5, step=0.5,
|
| 141 |
+
label="Guidance Scale",
|
| 142 |
+
info="How closely to follow the prompt"
|
| 143 |
+
)
|
| 144 |
+
custom_prompt = gr.Textbox(
|
| 145 |
+
label="Additional Prompt (Optional)",
|
| 146 |
+
placeholder="e.g., modern minimalist, glass facade, sunset lighting...",
|
| 147 |
+
lines=2,
|
| 148 |
+
info="Add custom details to enhance specific aspects"
|
| 149 |
+
)
|
| 150 |
+
use_upscaler = gr.Checkbox(label="Enable Upscaling (2x)", value=True)
|
| 151 |
+
use_postprocess = gr.Checkbox(label="Enable Post-Processing", value=True)
|
| 152 |
+
|
| 153 |
+
enhance_btn = gr.Button("β¨ Enhance Image", variant="primary", size="lg")
|
| 154 |
+
|
| 155 |
+
with gr.Column():
|
| 156 |
+
output_image = gr.Image(label="β
Enhanced Result", type="pil")
|
| 157 |
+
|
| 158 |
+
gr.Markdown("""
|
| 159 |
+
### π Tips for Best Results:
|
| 160 |
+
- Use high-quality architectural renders as input
|
| 161 |
+
- Start with default settings and adjust if needed
|
| 162 |
+
- Lower strength for subtle enhancements
|
| 163 |
+
- Higher strength for more dramatic changes
|
| 164 |
+
- Processing takes 2-5 minutes on CPU, ~30 seconds on GPU
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
# Connect the button
|
| 168 |
+
enhance_btn.click(
|
| 169 |
+
fn=enhance_image_simple,
|
| 170 |
+
inputs=[input_image, strength, guidance_scale, custom_prompt, use_upscaler, use_postprocess],
|
| 171 |
+
outputs=output_image
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# Tab 2: Training Guide
|
| 175 |
+
with gr.Tab("π Custom Training"):
|
| 176 |
+
gr.Markdown("""
|
| 177 |
+
# π Train Your Own Custom LoRA Model
|
| 178 |
+
|
| 179 |
+
Want to teach the AI your specific architectural style? You can train a custom LoRA model!
|
| 180 |
+
|
| 181 |
+
## β οΈ Important Note
|
| 182 |
+
|
| 183 |
+
**Training is not available on this HF Space** due to computational requirements. However, you can train locally and deploy your custom model!
|
| 184 |
+
|
| 185 |
+
## π What is LoRA Training?
|
| 186 |
+
|
| 187 |
+
LoRA (Low-Rank Adaptation) allows you to fine-tune the AI with just 10-50 image pairs to learn:
|
| 188 |
+
- Your specific architectural rendering style
|
| 189 |
+
- Preferred lighting and atmosphere
|
| 190 |
+
- Consistent material treatments
|
| 191 |
+
- Unique design aesthetics
|
| 192 |
+
|
| 193 |
+
### Benefits:
|
| 194 |
+
- β
**Fast Training**: Only 1000 steps needed (~20-30 min on GPU)
|
| 195 |
+
- β
**Small Models**: LoRA weights are only ~10-50 MB
|
| 196 |
+
- οΏ½οΏ½οΏ½ **Style Consistency**: Perfect for architectural firms with specific styles
|
| 197 |
+
- β
**Efficient**: Works on consumer GPUs
|
| 198 |
+
|
| 199 |
+
---
|
| 200 |
+
|
| 201 |
+
## π Training Process Overview
|
| 202 |
+
|
| 203 |
+
### Step 1: Prepare Training Data
|
| 204 |
+
|
| 205 |
+
Create **image pairs**:
|
| 206 |
+
- **Input**: Your base architectural render (before)
|
| 207 |
+
- **Target**: Your ideal enhanced result (after)
|
| 208 |
+
|
| 209 |
+
**Requirements:**
|
| 210 |
+
- Minimum: 10 pairs (recommended: 20-50)
|
| 211 |
+
- Format: PNG or JPG
|
| 212 |
+
- Resolution: 512x512 to 1024x1024
|
| 213 |
+
|
| 214 |
+
**Example structure:**
|
| 215 |
+
```
|
| 216 |
+
training_data/
|
| 217 |
+
inputs/
|
| 218 |
+
building_001_input.png
|
| 219 |
+
building_002_input.png
|
| 220 |
+
targets/
|
| 221 |
+
building_001_target.png
|
| 222 |
+
building_002_target.png
|
| 223 |
+
```
|
| 224 |
+
|
| 225 |
+
### Step 2: Setup Local Backend
|
| 226 |
+
|
| 227 |
+
```bash
|
| 228 |
+
cd architecture-ai-enhancer/backend
|
| 229 |
+
pip install -r requirements.txt
|
| 230 |
+
uvicorn main:app --host 0.0.0.0 --port 8000
|
| 231 |
+
```
|
| 232 |
+
|
| 233 |
+
### Step 3: Upload Training Pairs
|
| 234 |
+
|
| 235 |
+
Use the API at `http://localhost:8000/docs` to upload your image pairs:
|
| 236 |
+
|
| 237 |
+
```python
|
| 238 |
+
import requests
|
| 239 |
+
|
| 240 |
+
files = {
|
| 241 |
+
'input_image': open('building_001_input.png', 'rb'),
|
| 242 |
+
'target_image': open('building_001_target.png', 'rb')
|
| 243 |
+
}
|
| 244 |
+
response = requests.post(
|
| 245 |
+
'http://localhost:8000/training/upload_pair',
|
| 246 |
+
files=files
|
| 247 |
+
)
|
| 248 |
+
```
|
| 249 |
+
|
| 250 |
+
### Step 4: Start Training
|
| 251 |
+
|
| 252 |
+
```python
|
| 253 |
+
config = {
|
| 254 |
+
"train_steps": 1000,
|
| 255 |
+
"learning_rate": 1e-4,
|
| 256 |
+
"lora_rank": 8,
|
| 257 |
+
"batch_size": 1
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
response = requests.post(
|
| 261 |
+
'http://localhost:8000/training/start',
|
| 262 |
+
json=config
|
| 263 |
+
)
|
| 264 |
+
```
|
| 265 |
+
|
| 266 |
+
### Step 5: Use Your Custom Model
|
| 267 |
+
|
| 268 |
+
Once trained, your custom LoRA is automatically used for all enhancements!
|
| 269 |
+
|
| 270 |
+
---
|
| 271 |
+
|
| 272 |
+
## π¨ Training Tips
|
| 273 |
+
|
| 274 |
+
**For Subtle Enhancements:**
|
| 275 |
+
- train_steps: 500
|
| 276 |
+
- learning_rate: 5e-5
|
| 277 |
+
- lora_rank: 4
|
| 278 |
+
|
| 279 |
+
**For Dramatic Style Changes:**
|
| 280 |
+
- train_steps: 1500
|
| 281 |
+
- learning_rate: 1e-4
|
| 282 |
+
- lora_rank: 12
|
| 283 |
+
|
| 284 |
+
**Balanced (Recommended):**
|
| 285 |
+
- train_steps: 1000
|
| 286 |
+
- learning_rate: 1e-4
|
| 287 |
+
- lora_rank: 8
|
| 288 |
+
|
| 289 |
+
---
|
| 290 |
+
|
| 291 |
+
## π Complete Documentation
|
| 292 |
+
|
| 293 |
+
For detailed step-by-step instructions, troubleshooting, and advanced techniques, see:
|
| 294 |
+
|
| 295 |
+
**[π Complete LoRA Training Guide](https://huggingface.co/spaces/TransformacionDigitalAA/ArchEnhancer/blob/main/TRAINING_GUIDE.md)**
|
| 296 |
+
|
| 297 |
+
This includes:
|
| 298 |
+
- Detailed API usage examples
|
| 299 |
+
- Complete Python training script
|
| 300 |
+
- Troubleshooting common issues
|
| 301 |
+
- Best practices for creating training data
|
| 302 |
+
- How to deploy your custom LoRA
|
| 303 |
+
|
| 304 |
+
---
|
| 305 |
+
|
| 306 |
+
## π‘ Use Cases
|
| 307 |
+
|
| 308 |
+
- **Architecture Firms**: Train on your signature rendering style
|
| 309 |
+
- **Game Studios**: Consistent environmental concept art
|
| 310 |
+
- **VFX Artists**: Specific lighting and atmosphere
|
| 311 |
+
- **Real Estate**: Standardized visualization style
|
| 312 |
+
|
| 313 |
+
---
|
| 314 |
+
|
| 315 |
+
## π Resources
|
| 316 |
+
|
| 317 |
+
- [Backend GitHub Repository](https://github.com/yourusername/architecture-ai-enhancer)
|
| 318 |
+
- [LoRA Paper](https://arxiv.org/abs/2106.09685)
|
| 319 |
+
- [Diffusers Documentation](https://huggingface.co/docs/diffusers/)
|
| 320 |
+
""")
|
| 321 |
+
|
| 322 |
+
# Tab 3: About
|
| 323 |
+
with gr.Tab("βΉοΈ About"):
|
| 324 |
+
gr.Markdown("""
|
| 325 |
+
# About Architecture AI Enhancer
|
| 326 |
+
|
| 327 |
+
## π§ Technical Details
|
| 328 |
+
|
| 329 |
+
- **Model**: Stable Diffusion 1.5 (runwayml/stable-diffusion-v1-5)
|
| 330 |
+
- **Upscaler**: ESRGAN with fallback to Lanczos
|
| 331 |
+
- **Framework**: PyTorch + Diffusers
|
| 332 |
+
- **Interface**: Gradio 4.20.0
|
| 333 |
+
- **Version**: 1.0.0
|
| 334 |
+
|
| 335 |
+
## βοΈ Settings Guide
|
| 336 |
+
|
| 337 |
+
### Denoising Strength (0.1-0.8)
|
| 338 |
+
Controls how much the AI modifies your input image:
|
| 339 |
+
- **0.2-0.3**: Subtle enhancements (recommended for most cases)
|
| 340 |
+
- **0.4-0.5**: Moderate changes
|
| 341 |
+
- **0.6-0.8**: Dramatic transformations
|
| 342 |
+
|
| 343 |
+
### Guidance Scale (1-15)
|
| 344 |
+
How closely the AI follows the prompt:
|
| 345 |
+
- **4-6**: Natural, balanced results (recommended)
|
| 346 |
+
- **7-10**: More stylized output
|
| 347 |
+
- **11-15**: Very strong prompt adherence
|
| 348 |
+
|
| 349 |
+
### Custom Prompt
|
| 350 |
+
Add specific details to guide the enhancement:
|
| 351 |
+
- Lighting: "sunset lighting", "dramatic shadows"
|
| 352 |
+
- Style: "modern minimalist", "brutalist concrete"
|
| 353 |
+
- Materials: "glass facade", "wooden accents"
|
| 354 |
+
- Atmosphere: "foggy morning", "golden hour"
|
| 355 |
+
|
| 356 |
+
## π Features
|
| 357 |
+
|
| 358 |
+
- β
AI-powered enhancement with Stable Diffusion
|
| 359 |
+
- β
2x image upscaling
|
| 360 |
+
- β
Professional post-processing
|
| 361 |
+
- β
Custom prompt support
|
| 362 |
+
- β
Adjustable parameters
|
| 363 |
+
- β
Custom LoRA training (local)
|
| 364 |
+
|
| 365 |
+
## π Performance
|
| 366 |
+
|
| 367 |
+
- **CPU (HF Spaces Free Tier)**: 2-5 minutes per image
|
| 368 |
+
- **GPU (Local/Paid)**: ~30 seconds per image
|
| 369 |
+
|
| 370 |
+
## π Credits
|
| 371 |
+
|
| 372 |
+
Built with:
|
| 373 |
+
- [Stable Diffusion](https://github.com/CompVis/stable-diffusion)
|
| 374 |
+
- [Diffusers](https://github.com/huggingface/diffusers)
|
| 375 |
+
- [Gradio](https://gradio.app)
|
| 376 |
+
- [PyTorch](https://pytorch.org)
|
| 377 |
+
|
| 378 |
+
## π License
|
| 379 |
+
|
| 380 |
+
MIT License - Free for commercial and personal use
|
| 381 |
+
|
| 382 |
+
---
|
| 383 |
+
|
| 384 |
+
**Made with β€οΈ for the architecture community**
|
| 385 |
+
""")
|
| 386 |
+
|
| 387 |
+
if __name__ == "__main__":
|
| 388 |
+
# Check if running on HF Spaces
|
| 389 |
+
is_spaces = os.getenv("SPACE_ID") is not None
|
| 390 |
+
|
| 391 |
+
if is_spaces:
|
| 392 |
+
# HF Spaces specific configuration
|
| 393 |
+
demo.launch(
|
| 394 |
+
server_name="0.0.0.0",
|
| 395 |
+
server_port=7860,
|
| 396 |
+
share=False
|
| 397 |
+
)
|
| 398 |
+
else:
|
| 399 |
+
# Local development
|
| 400 |
+
demo.launch(share=True)
|
| 401 |
- Lower strength = more faithful to input
|
| 402 |
- Higher strength = more creative output
|
| 403 |
- Processing takes 2-5 minutes on CPU
|