Gonzalo Vergara commited on
Commit Β·
906f08a
1
Parent(s): ebefbcf
vibe or bust
Browse files- .gitignore +62 -0
- app.py +55 -0
- requirements.txt +7 -0
.gitignore
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python virtual environments
|
| 2 |
+
venv/
|
| 3 |
+
env/
|
| 4 |
+
.virtualenv/
|
| 5 |
+
.venv/
|
| 6 |
+
|
| 7 |
+
# Python bytecode and caches
|
| 8 |
+
__pycache__/
|
| 9 |
+
*.py[cod]
|
| 10 |
+
*$py.class
|
| 11 |
+
*.so
|
| 12 |
+
|
| 13 |
+
# Virtual environment activation files
|
| 14 |
+
*.egg-info/
|
| 15 |
+
dist/
|
| 16 |
+
build/
|
| 17 |
+
*.egg
|
| 18 |
+
|
| 19 |
+
# Dependency lock files (optional, include if you donβt want them tracked)
|
| 20 |
+
# requirements.lock
|
| 21 |
+
# Pipfile.lock
|
| 22 |
+
|
| 23 |
+
# IDE and editor files
|
| 24 |
+
.idea/
|
| 25 |
+
.vscode/
|
| 26 |
+
*.sublime-project
|
| 27 |
+
*.sublime-workspace
|
| 28 |
+
|
| 29 |
+
# OS-generated files
|
| 30 |
+
.DS_Store
|
| 31 |
+
Thumbs.db
|
| 32 |
+
|
| 33 |
+
# Logs and temporary files
|
| 34 |
+
*.log
|
| 35 |
+
*.tmp
|
| 36 |
+
temp/
|
| 37 |
+
|
| 38 |
+
# Model weights and caches (if downloaded locally)
|
| 39 |
+
*.ckpt
|
| 40 |
+
*.safetensors
|
| 41 |
+
*.bin
|
| 42 |
+
*.pth
|
| 43 |
+
model_cache/
|
| 44 |
+
cache/
|
| 45 |
+
diffusers_cache/
|
| 46 |
+
|
| 47 |
+
# Output files from generation
|
| 48 |
+
output/
|
| 49 |
+
*.png
|
| 50 |
+
*.jpg
|
| 51 |
+
*.jpeg
|
| 52 |
+
*.gif
|
| 53 |
+
|
| 54 |
+
# Hugging Face Spaces-specific ignores (optional)
|
| 55 |
+
# Uncomment if you donβt want these tracked
|
| 56 |
+
# .hf_cache/
|
| 57 |
+
# .gradio/
|
| 58 |
+
|
| 59 |
+
# Miscellaneous
|
| 60 |
+
*.bak
|
| 61 |
+
*.swp
|
| 62 |
+
*~
|
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Load ControlNet and Stable Diffusion models
|
| 9 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 10 |
+
"lllyasviel/sd-controlnet-canny",
|
| 11 |
+
torch_dtype=torch.float16
|
| 12 |
+
)
|
| 13 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 14 |
+
"runwayml/stable-diffusion-v1-5",
|
| 15 |
+
controlnet=controlnet,
|
| 16 |
+
torch_dtype=torch.float16
|
| 17 |
+
)
|
| 18 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 19 |
+
pipe.to("cuda") # Use GPU in Spaces
|
| 20 |
+
pipe.enable_model_cpu_offload() # Optimize memory usage
|
| 21 |
+
|
| 22 |
+
# Function to process image with ControlNet
|
| 23 |
+
def generate_image(input_image, prompt):
|
| 24 |
+
# Convert input image to numpy array and extract Canny edges
|
| 25 |
+
image = np.array(input_image)
|
| 26 |
+
low_threshold = 100
|
| 27 |
+
high_threshold = 200
|
| 28 |
+
canny_image = cv2.Canny(image, low_threshold, high_threshold)
|
| 29 |
+
canny_image = canny_image[:, :, None]
|
| 30 |
+
canny_image = np.concatenate([canny_image, canny_image, canny_image], axis=2)
|
| 31 |
+
canny_image = Image.fromarray(canny_image)
|
| 32 |
+
|
| 33 |
+
# Generate image with ControlNet
|
| 34 |
+
output = pipe(
|
| 35 |
+
prompt,
|
| 36 |
+
image=canny_image,
|
| 37 |
+
num_inference_steps=20,
|
| 38 |
+
controlnet_conditioning_scale=0.8
|
| 39 |
+
).images[0]
|
| 40 |
+
return output
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=generate_image,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Image(type="pil", label="Upload an Image"),
|
| 47 |
+
gr.Textbox(label="Prompt", placeholder="e.g., 'a futuristic city'")
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 50 |
+
title="Stable Diffusion with ControlNet (Canny)",
|
| 51 |
+
description="Upload an image and enter a prompt to generate a new image guided by Canny edges."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Launch the app
|
| 55 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
diffusers>=0.27.0
|
| 3 |
+
transformers>=4.30.0
|
| 4 |
+
accelerate>=0.20.0
|
| 5 |
+
gradio>=4.0.0
|
| 6 |
+
numpy>=1.23.0
|
| 7 |
+
opencv-python>=4.8.0
|