Aguilar Elizondo commited on
Commit
ee66c89
·
1 Parent(s): 467eea4

Add LoRA activation support and usage instructions

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py CHANGED
@@ -29,6 +29,17 @@ def initialize_models():
29
  if pipeline_manager is None:
30
  logger.info("Loading Stable Diffusion pipeline...")
31
  pipeline_manager = get_pipeline_manager()
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def enhance_image_simple(
34
  input_image: Image.Image,
@@ -123,6 +134,29 @@ with gr.Blocks(title="🏛️ Architecture AI Enhancer") as demo:
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"):
@@ -248,6 +282,84 @@ with gr.Blocks(title="🏛️ Architecture AI Enhancer") as demo:
248
  - **Alignment**: Input and target should show the same scene
249
  - **Target Quality**: Ensure targets represent your desired style accurately
250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  ---
252
  """)
253
 
 
29
  if pipeline_manager is None:
30
  logger.info("Loading Stable Diffusion pipeline...")
31
  pipeline_manager = get_pipeline_manager()
32
+
33
+ # Check if there's a LoRA model to load
34
+ from backend.config import get_lora_path
35
+ lora_path = get_lora_path()
36
+ if lora_path:
37
+ logger.info(f"LoRA model found: {lora_path}")
38
+ try:
39
+ pipeline_manager.load_lora(lora_path)
40
+ logger.info("✅ Custom LoRA loaded successfully!")
41
+ except Exception as e:
42
+ logger.warning(f"Failed to load LoRA: {e}")
43
 
44
  def enhance_image_simple(
45
  input_image: Image.Image,
 
134
  Transform your architectural renders with AI-powered enhancement using Stable Diffusion 1.5
135
  """)
136
 
137
+ # Check for custom LoRA
138
+ from backend.config import get_lora_path
139
+ lora_path = get_lora_path()
140
+ if lora_path and lora_path.exists():
141
+ gr.Markdown(f"""
142
+ <div style="background: linear-gradient(90deg, #4CAF50 0%, #45a049 100%); padding: 10px; border-radius: 8px; margin: 10px 0;">
143
+ <p style="color: white; margin: 0; font-weight: bold;">
144
+ ✅ Custom LoRA Active: {lora_path.name}
145
+ </p>
146
+ <p style="color: rgba(255,255,255,0.9); margin: 5px 0 0 0; font-size: 0.9em;">
147
+ All enhancements will use your custom trained style
148
+ </p>
149
+ </div>
150
+ """)
151
+ else:
152
+ gr.Markdown("""
153
+ <div style="background: #f0f0f0; padding: 10px; border-radius: 8px; margin: 10px 0;">
154
+ <p style="color: #666; margin: 0;">
155
+ ℹ️ Using base Stable Diffusion 1.5 model. <a href="#" style="color: #2196F3;">Train a custom LoRA</a> for personalized style.
156
+ </p>
157
+ </div>
158
+ """)
159
+
160
  with gr.Tabs():
161
  # Tab 1: Enhancement
162
  with gr.Tab("✨ Enhance Image"):
 
282
  - **Alignment**: Input and target should show the same scene
283
  - **Target Quality**: Ensure targets represent your desired style accurately
284
 
285
+ ---
286
+
287
+ ## 🎯 Using a Custom LoRA Model
288
+
289
+ Once you've trained a LoRA model locally, here's how to use it:
290
+
291
+ ### Option 1: Deploy to This Space (Recommended)
292
+
293
+ 1. **Upload your LoRA to Hugging Face Hub**:
294
+ ```bash
295
+ # Install huggingface_hub
296
+ pip install huggingface_hub
297
+
298
+ # Upload your LoRA
299
+ from huggingface_hub import upload_file
300
+ upload_file(
301
+ path_or_fileobj="models/lora/your_lora.safetensors",
302
+ path_in_repo="your_lora.safetensors",
303
+ repo_id="your-username/your-lora-repo",
304
+ repo_type="model"
305
+ )
306
+ ```
307
+
308
+ 2. **Modify this Space to load your LoRA**:
309
+ - Fork this Space or create a duplicate
310
+ - Edit `backend/config.py`:
311
+ ```python
312
+ LORA_MODEL_NAME = "your_lora.safetensors"
313
+ ```
314
+ - Add code in `app.py` to download from HF Hub:
315
+ ```python
316
+ from huggingface_hub import hf_hub_download
317
+
318
+ lora_path = hf_hub_download(
319
+ repo_id="your-username/your-lora-repo",
320
+ filename="your_lora.safetensors",
321
+ local_dir="models/lora"
322
+ )
323
+ ```
324
+
325
+ 3. **Restart the Space** - Your custom LoRA will be loaded automatically!
326
+
327
+ ### Option 2: Use Locally
328
+
329
+ 1. **Place your LoRA file** in `models/lora/` directory
330
+ 2. **Update config**: Set `LORA_MODEL_NAME` in `backend/config.py`
331
+ 3. **Run backend**: `uvicorn main:app --reload`
332
+ 4. The LoRA is automatically detected and loaded!
333
+
334
+ ### How It Works
335
+
336
+ When a LoRA is present:
337
+ - ✅ Pipeline automatically loads it on startup
338
+ - ✅ All enhancements use your custom style
339
+ - ✅ No additional configuration needed
340
+ - ✅ Can be combined with custom prompts for fine control
341
+
342
+ ### Verify LoRA is Loaded
343
+
344
+ Check the logs on startup:
345
+ ```
346
+ INFO: Loading Stable Diffusion pipeline...
347
+ INFO: LoRA model found: models/lora/your_lora.safetensors
348
+ INFO: ✅ Custom LoRA loaded successfully!
349
+ ```
350
+
351
+ ### Example: Architecture Firm Custom Style
352
+
353
+ ```python
354
+ # After training with your firm's rendering style
355
+ # Place: models/lora/firm_style.safetensors
356
+
357
+ # In config.py:
358
+ LORA_MODEL_NAME = "firm_style.safetensors"
359
+
360
+ # Now all enhancements will match your style!
361
+ ```
362
+
363
  ---
364
  """)
365