Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,94 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
|
| 3 |
+
from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import spaces
|
| 9 |
|
| 10 |
+
# 🌟 Auto-detect device (CPU/GPU)
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
precision = torch.float16 if device == "cuda" else torch.float32
|
| 13 |
|
| 14 |
+
|
| 15 |
+
eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
|
| 16 |
+
|
| 17 |
+
# 🏗️ Load ControlNet model for Canny edge detection
|
| 18 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 19 |
+
"thepowerfuldeez/sd21-controlnet-canny",
|
| 20 |
+
torch_dtype=precision
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# when test with other base model, you need to change the vae also.
|
| 24 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 25 |
+
|
| 26 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 27 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 28 |
+
controlnet=controlnet,
|
| 29 |
+
vae=vae,
|
| 30 |
+
torch_dtype=torch.float16,
|
| 31 |
+
scheduler=eulera_scheduler,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
pipe.to(device)
|
| 35 |
+
|
| 36 |
+
# ✨ Enable attention slicing for CPU optimization
|
| 37 |
+
#pipe.enable_attention_slicing()
|
| 38 |
+
|
| 39 |
+
# 📸 Edge detection function using OpenCV (Canny)
|
| 40 |
+
@spaces.GPU
|
| 41 |
+
def apply_canny(image, low_threshold, high_threshold):
|
| 42 |
+
image = np.array(image.convert("RGB"))
|
| 43 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 44 |
+
edges = cv2.Canny(gray, low_threshold, high_threshold)
|
| 45 |
+
edges = np.stack([edges] * 3, axis=-1) # Convert to RGB format
|
| 46 |
+
return Image.fromarray(edges)
|
| 47 |
+
|
| 48 |
+
# 🎨 Image generation function
|
| 49 |
+
@spaces.GPU
|
| 50 |
+
def generate_image(prompt, input_image, low_threshold, high_threshold, strength, guidance):
|
| 51 |
+
# Apply edge detection
|
| 52 |
+
edge_detected = apply_canny(input_image, low_threshold, high_threshold)
|
| 53 |
+
|
| 54 |
+
# Generate styled image using ControlNet
|
| 55 |
+
result = pipe(
|
| 56 |
+
prompt=prompt,
|
| 57 |
+
image=edge_detected,
|
| 58 |
+
num_inference_steps=30,
|
| 59 |
+
guidance_scale=guidance,
|
| 60 |
+
strength=strength
|
| 61 |
+
).images[0]
|
| 62 |
+
|
| 63 |
+
return edge_detected, result
|
| 64 |
+
|
| 65 |
+
# 🖥️ Gradio UI
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("# 🏗️ 3D Screenshot to Styled Render with ControlNet")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column():
|
| 71 |
+
input_image = gr.Image(label="Upload 3D Screenshot", type="pil")
|
| 72 |
+
prompt = gr.Textbox(label="Style Prompt", placeholder="e.g., Futuristic building in sunset")
|
| 73 |
+
|
| 74 |
+
low_threshold = gr.Slider(50, 150, value=100, label="Canny Edge Low Threshold")
|
| 75 |
+
high_threshold = gr.Slider(100, 200, value=150, label="Canny Edge High Threshold")
|
| 76 |
+
|
| 77 |
+
strength = gr.Slider(0.1, 1.0, value=0.8, label="Denoising Strength")
|
| 78 |
+
guidance = gr.Slider(1, 20, value=7.5, label="Guidance Scale (Creativity)")
|
| 79 |
+
|
| 80 |
+
generate_button = gr.Button("Generate Styled Image")
|
| 81 |
+
|
| 82 |
+
with gr.Column():
|
| 83 |
+
edge_output = gr.Image(label="Edge Detected Image")
|
| 84 |
+
result_output = gr.Image(label="Generated Styled Image")
|
| 85 |
+
|
| 86 |
+
# 🔗 Button Action
|
| 87 |
+
generate_button.click(
|
| 88 |
+
fn=generate_image,
|
| 89 |
+
inputs=[prompt, input_image, low_threshold, high_threshold, strength, guidance],
|
| 90 |
+
outputs=[edge_output, result_output]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# 🚀 Launch the app
|
| 94 |
+
demo.launch()
|