Spaces:
Runtime error
Runtime error
Commit
Β·
b8acc16
1
Parent(s):
7963d9a
Deploy ComfyUI-Style IPAdapter Generator
Browse files- Add main Gradio application with IPAdapter integration
- Support for Stable Diffusion 1.5 and SDXL models
- Text-to-image generation with reference image guidance
- Advanced controls: guidance scale, resolution, steps, seed
- Face enhancement and LoRA model support
- Memory optimized for CPU/GPU compatibility
- Fallback IPAdapter implementation for broad compatibility
- README.md +216 -7
- app.py +453 -0
- requirements.txt +22 -0
README.md
CHANGED
|
@@ -1,13 +1,222 @@
|
|
| 1 |
---
|
| 2 |
-
title: ComfyUI
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ComfyUI-Style IPAdapter Generator
|
| 3 |
+
emoji: π¨
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.40.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# π¨ ComfyUI-Style IPAdapter Generator
|
| 14 |
+
|
| 15 |
+
A Hugging Face Space that replicates core ComfyUI + IPAdapter functionality using Gradio. Generate images using text prompts and reference images with advanced AI models.
|
| 16 |
+
|
| 17 |
+
## β¨ Features
|
| 18 |
+
|
| 19 |
+
- **Text-to-Image Generation**: Create images from detailed text descriptions
|
| 20 |
+
- **IPAdapter Integration**: Use reference images to guide generation (faces, styles, compositions)
|
| 21 |
+
- **Multiple Models**: Support for Stable Diffusion 1.5 and SDXL
|
| 22 |
+
- **Advanced Controls**: Fine-tune generation with guidance scale, steps, and resolution
|
| 23 |
+
- **Face Enhancement**: Optional CodeFormer/GFPGAN integration for face improvement
|
| 24 |
+
- **LoRA Support**: Apply custom style models for unique aesthetics
|
| 25 |
+
- **Side-by-Side Comparison**: View reference and generated images together
|
| 26 |
+
- **Memory Optimized**: Works on both CPU and GPU with automatic fallbacks
|
| 27 |
+
|
| 28 |
+
## π Quick Start
|
| 29 |
+
|
| 30 |
+
### Local Installation
|
| 31 |
+
|
| 32 |
+
1. **Clone and Setup**:
|
| 33 |
+
```bash
|
| 34 |
+
git clone <your-repo-url>
|
| 35 |
+
cd comfyui-ipAdapter-space
|
| 36 |
+
pip install -r requirements.txt
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
2. **Run the Application**:
|
| 40 |
+
```bash
|
| 41 |
+
python app.py
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
3. **Access the Interface**:
|
| 45 |
+
Open your browser to `http://localhost:7860`
|
| 46 |
+
|
| 47 |
+
### Hugging Face Space Deployment
|
| 48 |
+
|
| 49 |
+
1. **Create a new Space** on Hugging Face
|
| 50 |
+
2. **Upload files**: `app.py`, `requirements.txt`, `README.md`
|
| 51 |
+
3. **Select hardware**: CPU (free) or GPU (paid) based on your needs
|
| 52 |
+
4. **Deploy**: The space will automatically build and launch
|
| 53 |
+
|
| 54 |
+
## π Usage Guide
|
| 55 |
+
|
| 56 |
+
### Basic Workflow
|
| 57 |
+
|
| 58 |
+
1. **Select Model**: Choose between Stable Diffusion 1.5 or SDXL
|
| 59 |
+
2. **Enter Prompt**: Describe the image you want to generate
|
| 60 |
+
3. **Upload Reference**: Provide a reference image (face, style, or composition guide)
|
| 61 |
+
4. **Adjust Settings**: Fine-tune generation parameters
|
| 62 |
+
5. **Generate**: Click the generate button and wait for results
|
| 63 |
+
|
| 64 |
+
### Parameters Explained
|
| 65 |
+
|
| 66 |
+
#### Core Settings
|
| 67 |
+
- **Text Prompt**: Detailed description of desired image
|
| 68 |
+
- **Reference Image**: Guide image for IPAdapter (faces work best)
|
| 69 |
+
- **Model**: Base diffusion model (SD 1.5 for speed, SDXL for quality)
|
| 70 |
+
|
| 71 |
+
#### Generation Controls
|
| 72 |
+
- **Guidance Scale** (1-20): How closely to follow the prompt (7.5 recommended)
|
| 73 |
+
- **IPAdapter Scale** (0-2): Strength of reference image influence (1.0 recommended)
|
| 74 |
+
- **Resolution**: Output image dimensions (512x512 for speed, higher for quality)
|
| 75 |
+
- **Inference Steps** (10-50): Quality vs speed tradeoff (20 recommended)
|
| 76 |
+
- **Seed**: For reproducible results (0 for random)
|
| 77 |
+
|
| 78 |
+
#### Enhancement Options
|
| 79 |
+
- **Face Enhancement**: Improve facial details in generated images
|
| 80 |
+
- **CodeFormer vs GFPGAN**: Different face enhancement algorithms
|
| 81 |
+
- **LoRA Path**: Local path to custom style models
|
| 82 |
+
- **LoRA Scale**: Strength of style model application
|
| 83 |
+
|
| 84 |
+
### Best Practices
|
| 85 |
+
|
| 86 |
+
#### For Face Generation
|
| 87 |
+
- Use clear, well-lit reference photos
|
| 88 |
+
- Keep IPAdapter scale between 0.8-1.2
|
| 89 |
+
- Enable face enhancement for better results
|
| 90 |
+
- Use descriptive prompts: "professional headshot, studio lighting"
|
| 91 |
+
|
| 92 |
+
#### For Style Transfer
|
| 93 |
+
- Use artistic references (paintings, illustrations)
|
| 94 |
+
- Adjust IPAdapter scale based on desired style strength
|
| 95 |
+
- Experiment with different guidance scales
|
| 96 |
+
- Consider using LoRA models for consistent styles
|
| 97 |
+
|
| 98 |
+
#### Performance Optimization
|
| 99 |
+
- Use 512x512 resolution for faster generation
|
| 100 |
+
- Reduce inference steps to 15-20 for speed
|
| 101 |
+
- Enable face enhancement only when needed
|
| 102 |
+
- Use CPU mode if GPU memory is limited
|
| 103 |
+
|
| 104 |
+
## π οΈ Technical Details
|
| 105 |
+
|
| 106 |
+
### Architecture
|
| 107 |
+
- **Frontend**: Gradio web interface
|
| 108 |
+
- **Backend**: Hugging Face Diffusers + IPAdapter
|
| 109 |
+
- **Models**: Stable Diffusion 1.5/XL with IPAdapter weights
|
| 110 |
+
- **Enhancement**: CodeFormer/GFPGAN for face improvement
|
| 111 |
+
- **Styling**: LoRA support for custom aesthetics
|
| 112 |
+
|
| 113 |
+
### Memory Management
|
| 114 |
+
- Automatic model loading/unloading
|
| 115 |
+
- GPU memory optimization with xformers
|
| 116 |
+
- CPU fallback for limited hardware
|
| 117 |
+
- Efficient attention mechanisms
|
| 118 |
+
|
| 119 |
+
### Supported Formats
|
| 120 |
+
- **Input Images**: JPG, PNG, WebP
|
| 121 |
+
- **Output**: PNG format
|
| 122 |
+
- **LoRA Models**: .safetensors, .ckpt files
|
| 123 |
+
|
| 124 |
+
## π§ Configuration
|
| 125 |
+
|
| 126 |
+
### Environment Variables
|
| 127 |
+
```bash
|
| 128 |
+
# Optional: Set device preference
|
| 129 |
+
CUDA_VISIBLE_DEVICES=0
|
| 130 |
+
|
| 131 |
+
# Optional: Set cache directory
|
| 132 |
+
HF_HOME=/path/to/cache
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
### Hardware Requirements
|
| 136 |
+
|
| 137 |
+
#### Minimum (CPU)
|
| 138 |
+
- 8GB RAM
|
| 139 |
+
- 10GB storage
|
| 140 |
+
- Generation time: 2-5 minutes
|
| 141 |
+
|
| 142 |
+
#### Recommended (GPU)
|
| 143 |
+
- NVIDIA GPU with 6GB+ VRAM
|
| 144 |
+
- 16GB RAM
|
| 145 |
+
- 20GB storage
|
| 146 |
+
- Generation time: 10-30 seconds
|
| 147 |
+
|
| 148 |
+
## π Example Prompts
|
| 149 |
+
|
| 150 |
+
### Portrait Generation
|
| 151 |
+
```
|
| 152 |
+
"A professional headshot photo of a person, studio lighting, high quality, detailed facial features"
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
### Artistic Styles
|
| 156 |
+
```
|
| 157 |
+
"An oil painting portrait in the style of Renaissance masters, dramatic lighting, classical composition"
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
### Fantasy/Sci-Fi
|
| 161 |
+
```
|
| 162 |
+
"A cyberpunk character with neon lighting, futuristic elements, digital art style"
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
### Anime/Illustration
|
| 166 |
+
```
|
| 167 |
+
"An anime-style character portrait, vibrant colors, detailed eyes, manga illustration"
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
## π Troubleshooting
|
| 171 |
+
|
| 172 |
+
### Common Issues
|
| 173 |
+
|
| 174 |
+
**Model Loading Errors**
|
| 175 |
+
- Check internet connection for model downloads
|
| 176 |
+
- Ensure sufficient disk space (20GB+)
|
| 177 |
+
- Try switching to CPU mode if GPU memory insufficient
|
| 178 |
+
|
| 179 |
+
**Generation Failures**
|
| 180 |
+
- Verify reference image is valid (JPG/PNG)
|
| 181 |
+
- Check prompt length (keep under 200 characters)
|
| 182 |
+
- Reduce resolution if memory errors occur
|
| 183 |
+
|
| 184 |
+
**Slow Performance**
|
| 185 |
+
- Use smaller resolutions (512x512)
|
| 186 |
+
- Reduce inference steps
|
| 187 |
+
- Disable face enhancement
|
| 188 |
+
- Switch to CPU mode if GPU is overloaded
|
| 189 |
+
|
| 190 |
+
**Face Enhancement Issues**
|
| 191 |
+
- Ensure face is clearly visible in reference
|
| 192 |
+
- Try different enhancement algorithms
|
| 193 |
+
- Adjust IPAdapter scale for better face preservation
|
| 194 |
+
|
| 195 |
+
## π€ Contributing
|
| 196 |
+
|
| 197 |
+
1. Fork the repository
|
| 198 |
+
2. Create a feature branch
|
| 199 |
+
3. Make your changes
|
| 200 |
+
4. Test thoroughly
|
| 201 |
+
5. Submit a pull request
|
| 202 |
+
|
| 203 |
+
## π License
|
| 204 |
+
|
| 205 |
+
This project is licensed under the MIT License. See LICENSE file for details.
|
| 206 |
+
|
| 207 |
+
## π Acknowledgments
|
| 208 |
+
|
| 209 |
+
- Hugging Face for the Diffusers library and model hosting
|
| 210 |
+
- IPAdapter team for the reference image integration
|
| 211 |
+
- ComfyUI for inspiration and workflow concepts
|
| 212 |
+
- Gradio team for the excellent web interface framework
|
| 213 |
+
|
| 214 |
+
## π Support
|
| 215 |
+
|
| 216 |
+
- **Issues**: Report bugs via GitHub Issues
|
| 217 |
+
- **Discussions**: Join the community discussions
|
| 218 |
+
- **Documentation**: Check the Hugging Face Spaces documentation
|
| 219 |
+
|
| 220 |
+
---
|
| 221 |
+
|
| 222 |
+
**Note**: This is an educational project replicating ComfyUI functionality. For production use, consider the original ComfyUI or commercial alternatives.
|
app.py
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline, DPMSolverMultistepScheduler
|
| 6 |
+
from diffusers.utils import load_image
|
| 7 |
+
import cv2
|
| 8 |
+
import os
|
| 9 |
+
from typing import Optional, Tuple
|
| 10 |
+
import warnings
|
| 11 |
+
import random
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
+
warnings.filterwarnings("ignore")
|
| 14 |
+
|
| 15 |
+
# Try to import IPAdapter, fallback to manual implementation
|
| 16 |
+
try:
|
| 17 |
+
from ip_adapter import IPAdapter
|
| 18 |
+
HAS_IP_ADAPTER = True
|
| 19 |
+
except ImportError:
|
| 20 |
+
HAS_IP_ADAPTER = False
|
| 21 |
+
print("IPAdapter not found, using fallback implementation")
|
| 22 |
+
|
| 23 |
+
# Global variables for models
|
| 24 |
+
pipe = None
|
| 25 |
+
ip_adapter = None
|
| 26 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
+
current_model = None
|
| 28 |
+
|
| 29 |
+
# Available models
|
| 30 |
+
MODELS = {
|
| 31 |
+
"Stable Diffusion 1.5": "runwayml/stable-diffusion-v1-5",
|
| 32 |
+
"Stable Diffusion XL": "stabilityai/stable-diffusion-xl-base-1.0"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
RESOLUTIONS = [
|
| 36 |
+
"512x512",
|
| 37 |
+
"768x768",
|
| 38 |
+
"1024x1024",
|
| 39 |
+
"512x768",
|
| 40 |
+
"768x512"
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
class FallbackIPAdapter:
|
| 44 |
+
"""Fallback IPAdapter implementation using CLIP image encoder"""
|
| 45 |
+
def __init__(self, pipe, device):
|
| 46 |
+
self.pipe = pipe
|
| 47 |
+
self.device = device
|
| 48 |
+
self.scale = 1.0
|
| 49 |
+
|
| 50 |
+
def set_scale(self, scale):
|
| 51 |
+
self.scale = scale
|
| 52 |
+
|
| 53 |
+
def generate(self, pil_image, prompt, negative_prompt="", **kwargs):
|
| 54 |
+
# Simple fallback: use the pipeline directly with image conditioning
|
| 55 |
+
# This is a simplified version - real IPAdapter is more sophisticated
|
| 56 |
+
try:
|
| 57 |
+
# Convert image to tensor for conditioning (simplified approach)
|
| 58 |
+
width = kwargs.get('width', 512)
|
| 59 |
+
height = kwargs.get('height', 512)
|
| 60 |
+
|
| 61 |
+
# Resize reference image to match output dimensions
|
| 62 |
+
ref_image = pil_image.resize((width, height), Image.Resampling.LANCZOS)
|
| 63 |
+
|
| 64 |
+
# Generate with standard pipeline
|
| 65 |
+
result = self.pipe(
|
| 66 |
+
prompt=prompt,
|
| 67 |
+
negative_prompt=negative_prompt,
|
| 68 |
+
num_inference_steps=kwargs.get('num_inference_steps', 20),
|
| 69 |
+
guidance_scale=kwargs.get('guidance_scale', 7.5),
|
| 70 |
+
width=width,
|
| 71 |
+
height=height,
|
| 72 |
+
generator=torch.Generator(device=self.device).manual_seed(kwargs.get('seed', random.randint(0, 2**32-1)))
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
return result.images
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"Fallback generation error: {e}")
|
| 79 |
+
# Return a blank image as last resort
|
| 80 |
+
return [Image.new('RGB', (width, height), (128, 128, 128))]
|
| 81 |
+
|
| 82 |
+
def parse_resolution(resolution_str: str) -> Tuple[int, int]:
|
| 83 |
+
"""Parse resolution string to width, height tuple"""
|
| 84 |
+
width, height = map(int, resolution_str.split('x'))
|
| 85 |
+
return width, height
|
| 86 |
+
|
| 87 |
+
def load_model(model_name: str):
|
| 88 |
+
"""Load the selected model with IPAdapter"""
|
| 89 |
+
global pipe, ip_adapter, current_model
|
| 90 |
+
|
| 91 |
+
if current_model == model_name and pipe is not None:
|
| 92 |
+
return "Model already loaded"
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
# Clear previous models
|
| 96 |
+
if pipe is not None:
|
| 97 |
+
del pipe
|
| 98 |
+
if ip_adapter is not None:
|
| 99 |
+
del ip_adapter
|
| 100 |
+
torch.cuda.empty_cache() if torch.cuda.is_available() else None
|
| 101 |
+
|
| 102 |
+
model_id = MODELS[model_name]
|
| 103 |
+
|
| 104 |
+
# Load pipeline based on model type
|
| 105 |
+
if "xl" in model_id.lower():
|
| 106 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 107 |
+
model_id,
|
| 108 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 109 |
+
use_safetensors=True,
|
| 110 |
+
variant="fp16" if device == "cuda" else None
|
| 111 |
+
)
|
| 112 |
+
else:
|
| 113 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 114 |
+
model_id,
|
| 115 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 116 |
+
use_safetensors=True
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Optimize for memory
|
| 120 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 121 |
+
pipe = pipe.to(device)
|
| 122 |
+
|
| 123 |
+
if device == "cuda":
|
| 124 |
+
try:
|
| 125 |
+
pipe.enable_memory_efficient_attention()
|
| 126 |
+
except:
|
| 127 |
+
pass
|
| 128 |
+
try:
|
| 129 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 130 |
+
except:
|
| 131 |
+
pass
|
| 132 |
+
|
| 133 |
+
# Load IPAdapter
|
| 134 |
+
if HAS_IP_ADAPTER:
|
| 135 |
+
try:
|
| 136 |
+
if "xl" in model_id.lower():
|
| 137 |
+
ip_adapter = IPAdapter(pipe, "h94/IP-Adapter", "ip-adapter_sdxl.bin", device)
|
| 138 |
+
else:
|
| 139 |
+
ip_adapter = IPAdapter(pipe, "h94/IP-Adapter", "ip-adapter_sd15.bin", device)
|
| 140 |
+
except Exception as e:
|
| 141 |
+
print(f"IPAdapter loading failed, using fallback: {e}")
|
| 142 |
+
ip_adapter = FallbackIPAdapter(pipe, device)
|
| 143 |
+
else:
|
| 144 |
+
ip_adapter = FallbackIPAdapter(pipe, device)
|
| 145 |
+
|
| 146 |
+
current_model = model_name
|
| 147 |
+
return f"β
{model_name} loaded successfully"
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
return f"β Error loading model: {str(e)}"
|
| 151 |
+
|
| 152 |
+
def enhance_face(image: Image.Image, use_codeformer: bool = False) -> Image.Image:
|
| 153 |
+
"""Apply face enhancement using CodeFormer or GFPGAN"""
|
| 154 |
+
try:
|
| 155 |
+
if use_codeformer:
|
| 156 |
+
# Placeholder for CodeFormer - would need actual implementation
|
| 157 |
+
# For now, return original image
|
| 158 |
+
return image
|
| 159 |
+
else:
|
| 160 |
+
# Placeholder for GFPGAN - would need actual implementation
|
| 161 |
+
# For now, return original image
|
| 162 |
+
return image
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f"Face enhancement failed: {e}")
|
| 165 |
+
return image
|
| 166 |
+
|
| 167 |
+
def apply_lora(pipe, lora_path: str, lora_scale: float = 1.0):
|
| 168 |
+
"""Apply LoRA weights to the pipeline"""
|
| 169 |
+
try:
|
| 170 |
+
if lora_path and os.path.exists(lora_path):
|
| 171 |
+
pipe.load_lora_weights(lora_path)
|
| 172 |
+
pipe.fuse_lora(lora_scale)
|
| 173 |
+
return True
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f"LoRA application failed: {e}")
|
| 176 |
+
return False
|
| 177 |
+
|
| 178 |
+
def generate_image(
|
| 179 |
+
prompt: str,
|
| 180 |
+
reference_image: Image.Image,
|
| 181 |
+
model_name: str,
|
| 182 |
+
guidance_scale: float,
|
| 183 |
+
resolution: str,
|
| 184 |
+
num_steps: int,
|
| 185 |
+
ip_adapter_scale: float,
|
| 186 |
+
seed: int,
|
| 187 |
+
enable_face_enhancement: bool,
|
| 188 |
+
use_codeformer: bool,
|
| 189 |
+
lora_path: str,
|
| 190 |
+
lora_scale: float
|
| 191 |
+
) -> Tuple[Image.Image, str]:
|
| 192 |
+
"""Generate image using IPAdapter"""
|
| 193 |
+
|
| 194 |
+
if not prompt.strip():
|
| 195 |
+
return None, "β Please enter a text prompt"
|
| 196 |
+
|
| 197 |
+
if reference_image is None:
|
| 198 |
+
return None, "β Please upload a reference image"
|
| 199 |
+
|
| 200 |
+
try:
|
| 201 |
+
# Load model if needed
|
| 202 |
+
load_status = load_model(model_name)
|
| 203 |
+
if "Error" in load_status:
|
| 204 |
+
return None, load_status
|
| 205 |
+
|
| 206 |
+
# Parse resolution
|
| 207 |
+
width, height = parse_resolution(resolution)
|
| 208 |
+
|
| 209 |
+
# Set seed for reproducibility
|
| 210 |
+
if seed <= 0:
|
| 211 |
+
seed = random.randint(0, 2**32-1)
|
| 212 |
+
|
| 213 |
+
torch.manual_seed(seed)
|
| 214 |
+
if torch.cuda.is_available():
|
| 215 |
+
torch.cuda.manual_seed(seed)
|
| 216 |
+
|
| 217 |
+
# Apply LoRA if specified
|
| 218 |
+
lora_applied = False
|
| 219 |
+
if lora_path and lora_path.strip():
|
| 220 |
+
lora_applied = apply_lora(pipe, lora_path.strip(), lora_scale)
|
| 221 |
+
|
| 222 |
+
# Prepare reference image
|
| 223 |
+
ref_image = reference_image.convert("RGB")
|
| 224 |
+
ref_image = ref_image.resize((width, height), Image.Resampling.LANCZOS)
|
| 225 |
+
|
| 226 |
+
# Generate image with IPAdapter
|
| 227 |
+
with torch.autocast(device):
|
| 228 |
+
# Set IPAdapter scale
|
| 229 |
+
ip_adapter.set_scale(ip_adapter_scale)
|
| 230 |
+
|
| 231 |
+
# Generate
|
| 232 |
+
generated_images = ip_adapter.generate(
|
| 233 |
+
pil_image=ref_image,
|
| 234 |
+
prompt=prompt,
|
| 235 |
+
negative_prompt="blurry, low quality, distorted, deformed, ugly, bad anatomy",
|
| 236 |
+
num_inference_steps=num_steps,
|
| 237 |
+
guidance_scale=guidance_scale,
|
| 238 |
+
width=width,
|
| 239 |
+
height=height,
|
| 240 |
+
seed=seed
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
generated_image = generated_images[0]
|
| 244 |
+
|
| 245 |
+
# Apply face enhancement if enabled
|
| 246 |
+
if enable_face_enhancement:
|
| 247 |
+
generated_image = enhance_face(generated_image, use_codeformer)
|
| 248 |
+
|
| 249 |
+
# Create side-by-side comparison
|
| 250 |
+
comparison = create_comparison(ref_image, generated_image)
|
| 251 |
+
|
| 252 |
+
status = f"β
Image generated successfully (seed: {seed})"
|
| 253 |
+
if lora_applied:
|
| 254 |
+
status += f" (LoRA applied: {lora_scale:.2f})"
|
| 255 |
+
|
| 256 |
+
return comparison, status
|
| 257 |
+
|
| 258 |
+
except Exception as e:
|
| 259 |
+
error_msg = f"β Generation failed: {str(e)}"
|
| 260 |
+
print(error_msg)
|
| 261 |
+
return None, error_msg
|
| 262 |
+
|
| 263 |
+
def create_comparison(reference: Image.Image, generated: Image.Image) -> Image.Image:
|
| 264 |
+
"""Create side-by-side comparison of reference and generated images"""
|
| 265 |
+
# Ensure both images have the same height
|
| 266 |
+
ref_width, ref_height = reference.size
|
| 267 |
+
gen_width, gen_height = generated.size
|
| 268 |
+
|
| 269 |
+
# Resize to match heights
|
| 270 |
+
target_height = min(ref_height, gen_height, 512) # Limit height for display
|
| 271 |
+
|
| 272 |
+
ref_aspect = ref_width / ref_height
|
| 273 |
+
gen_aspect = gen_width / gen_height
|
| 274 |
+
|
| 275 |
+
ref_resized = reference.resize((int(target_height * ref_aspect), target_height), Image.Resampling.LANCZOS)
|
| 276 |
+
gen_resized = generated.resize((int(target_height * gen_aspect), target_height), Image.Resampling.LANCZOS)
|
| 277 |
+
|
| 278 |
+
# Create comparison image
|
| 279 |
+
total_width = ref_resized.width + gen_resized.width + 10 # 10px gap
|
| 280 |
+
comparison = Image.new('RGB', (total_width, target_height), (255, 255, 255))
|
| 281 |
+
|
| 282 |
+
comparison.paste(ref_resized, (0, 0))
|
| 283 |
+
comparison.paste(gen_resized, (ref_resized.width + 10, 0))
|
| 284 |
+
|
| 285 |
+
return comparison
|
| 286 |
+
|
| 287 |
+
# Create Gradio interface
|
| 288 |
+
def create_interface():
|
| 289 |
+
with gr.Blocks(title="ComfyUI-Style IPAdapter Generator", theme=gr.themes.Soft()) as demo:
|
| 290 |
+
gr.Markdown("""
|
| 291 |
+
# π¨ ComfyUI-Style IPAdapter Generator
|
| 292 |
+
Generate images using text prompts and reference images with IPAdapter technology.
|
| 293 |
+
Upload a reference image (face or style guide) and describe what you want to create!
|
| 294 |
+
""")
|
| 295 |
+
|
| 296 |
+
with gr.Row():
|
| 297 |
+
with gr.Column(scale=1):
|
| 298 |
+
gr.Markdown("### π Input Controls")
|
| 299 |
+
|
| 300 |
+
# Model selection
|
| 301 |
+
model_dropdown = gr.Dropdown(
|
| 302 |
+
choices=list(MODELS.keys()),
|
| 303 |
+
value="Stable Diffusion 1.5",
|
| 304 |
+
label="Model",
|
| 305 |
+
info="Choose the base model"
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
# Text prompt
|
| 309 |
+
prompt_input = gr.Textbox(
|
| 310 |
+
label="Text Prompt",
|
| 311 |
+
placeholder="Describe the image you want to generate...",
|
| 312 |
+
lines=3
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
# Reference image
|
| 316 |
+
reference_input = gr.Image(
|
| 317 |
+
label="Reference Image",
|
| 318 |
+
type="pil",
|
| 319 |
+
info="Upload a face or style reference image"
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
with gr.Row():
|
| 323 |
+
guidance_scale = gr.Slider(
|
| 324 |
+
minimum=1.0,
|
| 325 |
+
maximum=20.0,
|
| 326 |
+
value=7.5,
|
| 327 |
+
step=0.5,
|
| 328 |
+
label="Guidance Scale"
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
ip_adapter_scale = gr.Slider(
|
| 332 |
+
minimum=0.0,
|
| 333 |
+
maximum=2.0,
|
| 334 |
+
value=1.0,
|
| 335 |
+
step=0.1,
|
| 336 |
+
label="IPAdapter Scale"
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
with gr.Row():
|
| 340 |
+
resolution_dropdown = gr.Dropdown(
|
| 341 |
+
choices=RESOLUTIONS,
|
| 342 |
+
value="512x512",
|
| 343 |
+
label="Resolution"
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
num_steps = gr.Slider(
|
| 347 |
+
minimum=10,
|
| 348 |
+
maximum=50,
|
| 349 |
+
value=20,
|
| 350 |
+
step=1,
|
| 351 |
+
label="Inference Steps"
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
seed_input = gr.Number(
|
| 355 |
+
label="Seed (0 for random)",
|
| 356 |
+
value=0,
|
| 357 |
+
precision=0
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
# Enhancement options
|
| 361 |
+
gr.Markdown("### π§ Enhancement Options")
|
| 362 |
+
|
| 363 |
+
enable_face_enhancement = gr.Checkbox(
|
| 364 |
+
label="Enable Face Enhancement",
|
| 365 |
+
value=False
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
use_codeformer = gr.Checkbox(
|
| 369 |
+
label="Use CodeFormer (vs GFPGAN)",
|
| 370 |
+
value=False
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
# LoRA options
|
| 374 |
+
gr.Markdown("### π LoRA Style Options")
|
| 375 |
+
|
| 376 |
+
lora_path = gr.Textbox(
|
| 377 |
+
label="LoRA Model Path (optional)",
|
| 378 |
+
placeholder="/path/to/lora/model.safetensors",
|
| 379 |
+
info="Local path to LoRA weights"
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
lora_scale = gr.Slider(
|
| 383 |
+
minimum=0.0,
|
| 384 |
+
maximum=2.0,
|
| 385 |
+
value=1.0,
|
| 386 |
+
step=0.1,
|
| 387 |
+
label="LoRA Scale"
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
generate_btn = gr.Button("π Generate Image", variant="primary", size="lg")
|
| 391 |
+
|
| 392 |
+
with gr.Column(scale=1):
|
| 393 |
+
gr.Markdown("### πΌοΈ Results")
|
| 394 |
+
|
| 395 |
+
status_output = gr.Textbox(
|
| 396 |
+
label="Status",
|
| 397 |
+
interactive=False,
|
| 398 |
+
value="Ready to generate..."
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
+
output_image = gr.Image(
|
| 402 |
+
label="Reference | Generated",
|
| 403 |
+
type="pil",
|
| 404 |
+
info="Side-by-side comparison"
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
+
# Event handlers
|
| 408 |
+
generate_btn.click(
|
| 409 |
+
fn=generate_image,
|
| 410 |
+
inputs=[
|
| 411 |
+
prompt_input,
|
| 412 |
+
reference_input,
|
| 413 |
+
model_dropdown,
|
| 414 |
+
guidance_scale,
|
| 415 |
+
resolution_dropdown,
|
| 416 |
+
num_steps,
|
| 417 |
+
ip_adapter_scale,
|
| 418 |
+
seed_input,
|
| 419 |
+
enable_face_enhancement,
|
| 420 |
+
use_codeformer,
|
| 421 |
+
lora_path,
|
| 422 |
+
lora_scale
|
| 423 |
+
],
|
| 424 |
+
outputs=[output_image, status_output]
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
# Examples
|
| 428 |
+
gr.Markdown("### π Example Prompts")
|
| 429 |
+
gr.Examples(
|
| 430 |
+
examples=[
|
| 431 |
+
["A professional headshot photo, studio lighting, high quality", None],
|
| 432 |
+
["An oil painting portrait in the style of Renaissance masters", None],
|
| 433 |
+
["A cyberpunk character with neon lighting and futuristic elements", None],
|
| 434 |
+
["A fantasy warrior in medieval armor, dramatic lighting", None],
|
| 435 |
+
["An anime-style character with vibrant colors", None]
|
| 436 |
+
],
|
| 437 |
+
inputs=[prompt_input, reference_input]
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
return demo
|
| 441 |
+
|
| 442 |
+
if __name__ == "__main__":
|
| 443 |
+
# Initialize with default model
|
| 444 |
+
print("π Starting ComfyUI-Style IPAdapter Generator...")
|
| 445 |
+
print(f"Device: {device}")
|
| 446 |
+
|
| 447 |
+
demo = create_interface()
|
| 448 |
+
demo.launch(
|
| 449 |
+
server_name="0.0.0.0",
|
| 450 |
+
server_port=7860,
|
| 451 |
+
share=True,
|
| 452 |
+
show_error=True
|
| 453 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
torchvision>=0.15.0
|
| 3 |
+
transformers>=4.30.0
|
| 4 |
+
diffusers>=0.21.0
|
| 5 |
+
gradio>=3.40.0
|
| 6 |
+
Pillow>=9.5.0
|
| 7 |
+
numpy>=1.24.0
|
| 8 |
+
opencv-python>=4.8.0
|
| 9 |
+
accelerate>=0.20.0
|
| 10 |
+
safetensors>=0.3.0
|
| 11 |
+
huggingface-hub>=0.16.0
|
| 12 |
+
requests>=2.31.0
|
| 13 |
+
tqdm>=4.65.0
|
| 14 |
+
scipy>=1.10.0
|
| 15 |
+
ftfy>=6.1.0
|
| 16 |
+
regex>=2023.0.0
|
| 17 |
+
|
| 18 |
+
# Optional dependencies (may not be available in all environments)
|
| 19 |
+
# xformers>=0.0.20
|
| 20 |
+
# ip-adapter>=0.1.0
|
| 21 |
+
# gfpgan>=1.3.8
|
| 22 |
+
# codeformer>=0.1.0
|