goatom/Image_Edits
๐จ Zero-Shot Image Editing Assistant
Upload an image โ Describe your edit โ Get AI-generated results Powered by Stable Diffusion img2img + LoRA Transfer Learning
๐ Table of Contents
- How It Works
- Architecture
- Transfer Learning Explained
- Setup Instructions
- GPU Configuration
- Model Download
- Running the App
- Example Prompts
- Docker Deployment
- HuggingFace Spaces Deployment
- Troubleshooting
- Performance Optimization
- Folder Structure
๐ง How It Works
- User uploads a source image (PNG/JPG/WebP)
- User writes a natural-language editing prompt
- Prompt Parser extracts style keywords, intensity modifiers & object edits
- Stable Diffusion img2img encodes the image into latent space, adds noise proportional to
strength, and denoises guided by the text prompt - LoRA adapter (transfer learning) modulates the model's style behaviour
- Result is displayed side-by-side with the original and can be downloaded
๐ Architecture
Browser (HTML/CSS/JS)
โ
โโโ POST /upload โ Save image to static/uploads/
โโโ POST /generate โ Prompt Parser โ Diffusion Pipeline โ Output
โโโ GET /download โ Serve output file
โ
Flask Server (app.py)
โ
โโโ utils/prompt_parser.py โ NLP-style prompt analysis
โโโ utils/diffusion_pipeline.py โ SD img2img + LoRA
โ
HuggingFace Diffusers
โ
runwayml/stable-diffusion-v1-5 (pre-trained)๐ฌ Transfer Learning Explained
This project uses two levels of transfer learning โ neither trains a model from scratch:
Level 1: Pre-Trained Stable Diffusion
The base model (runwayml/stable-diffusion-v1-5) was trained on LAION-5B (~5 billion image-text pairs). We load these pre-trained weights directly โ this is the simplest form of transfer learning: feature reuse.
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16 # Half-precision for GPU efficiency
)
pipe = pipe.to("cuda") # GPU accelerationLevel 2: LoRA (Low-Rank Adaptation)
LoRA is a parameter-efficient fine-tuning technique:
- The base model's attention weight matrices W are frozen (not changed)
- Two small matrices A (down-projection) and B (up-projection) are added:
W' = W + scale ร (B @ A) - LoRA weights are only a few MB vs. the full model's ~4 GB
- They encode domain-specific knowledge (e.g., anime style, oil painting style)
- We load pre-trained LoRA weights from HuggingFace Hub:
pipe.load_lora_weights("lora-library/some-style")
Why this is transfer learning:
- The base model's knowledge is transferred to new tasks
- LoRA adapts the model to a new domain without retraining the full model
- Inference is fast because only the small adapter weights change the model's behaviour
๐ Setup Instructions
Prerequisites
- Python 3.10+
- 8 GB+ RAM (for CPU mode)
- NVIDIA GPU with 4+ GB VRAM (recommended, not required)
- Git installed
Step 1: Clone / Navigate to the project
cd c:\Users\tomge\Desktop\ai_imgStep 2: Create a virtual environment (recommended)
python -m venv venv
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activateStep 3: Install dependencies
pip install -r requirements.txtStep 4: Install PyTorch with CUDA (if you have an NVIDIA GPU)
# Check your CUDA version first:
nvidia-smi
# Install matching PyTorch:
# CUDA 11.8:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
# CUDA 12.1:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
# CPU only (no GPU):
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpuQ
๐ฅ GPU Configuration
Check GPU availability
import torch
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"GPU: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'None'}")
print(f"VRAM: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f} GB" if torch.cuda.is_available() else "")Memory optimization (already enabled in the code)
These are all enabled by default to keep your laptop cool.
๐ฅ Model Download
The model downloads automatically on the first generation request. No manual download needed.
Models are cached in the models/ directory. Subsequent runs use the cache.
Manual download (optional)
from diffusers import StableDiffusionImg2ImgPipeline
StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", cache_dir="./models")โถ๏ธ Running the App
python app.pyOpen your browser at http://localhost:5000
๐ก Example Prompts
๐ณ Docker Deployment
Build and run
docker build -t zero-shot-editor .
docker run -p 5000:5000 zero-shot-editorWith GPU (NVIDIA Container Toolkit required)
docker run --gpus all -p 5000:5000 zero-shot-editor๐ค HuggingFace Spaces Deployment
- Create a new Space at huggingface.co/spaces
- Select Gradio or Docker SDK
- Upload all project files
- Set Space hardware to T4 GPU (free tier available)
- The app will auto-build and deploy
For HuggingFace Spaces, you may need to change app.py's host:
app.run(host="0.0.0.0", port=7860) # Spaces uses port 7860๐ง Troubleshooting
โก Performance Optimization
Speed improvements
- Use GPU โ 10-20x faster than CPU
- Reduce steps โ 15-20 steps gives decent quality much faster
- Reduce image size โ 384px instead of 512px
- Use float16 โ already enabled for GPU
Memory savings
- Attention slicing โ already enabled
- VAE slicing โ already enabled
- CPU offload โ set
ENABLE_CPU_OFFLOAD = Trueinconfig.pyfor very low VRAM GPUs - Reduce batch size โ this app processes one image at a time (optimal)
Quality tips
- Increase steps to 40-50 for higher quality
- Guidance scale of 7-9 is usually optimal
- Strength of 0.4-0.6 preserves structure well
- Negative prompts are auto-generated by the prompt parser
๐ Folder Structure
zero_shot_editor/
โ
โโโ app.py # Flask server (routes, API)
โโโ config.py # All configuration in one place
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Container deployment
โโโ .dockerignore # Docker build exclusions
โโโ README.md # This file
โ
โโโ models/ # Cached model weights (auto-created)
โ
โโโ static/
โ โโโ css/
โ โ โโโ style.css # Dark glassmorphism UI
โ โโโ js/
โ โ โโโ app.js # Frontend logic
โ โโโ uploads/ # User-uploaded images
โ โโโ outputs/ # AI-generated outputs
โ
โโโ templates/
โ โโโ index.html # Main web interface
โ
โโโ utils/
โโโ __init__.py
โโโ diffusion_pipeline.py # SD img2img + LoRA pipeline
โโโ prompt_parser.py # NLP-style prompt analysis๐ License
MIT License โ Use freely for academic and personal projects.
