File size: 2,318 Bytes
17ba84f
5c5be76
17ba84f
 
 
 
 
 
 
 
 
 
 
 
af5c0a1
17ba84f
 
5c5be76
17ba84f
 
 
 
5c5be76
 
17ba84f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d178095
17ba84f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import gradio as gr
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline, UniPCMultistepScheduler
import torch
import numpy as np
import cv2
from PIL import Image
import spaces


# 🌟 Auto-detect device (CPU/GPU)
device = "cuda"
precision = torch.float16

# πŸ—οΈ Load ControlNet model for ADE20k
controlnet = ControlNetModel.from_pretrained("thibaud/controlnet-sd21-ade20k-diffusers", torch_dtype=precision)

# Load the model from Hugging Face
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2-1",
    torch_dtype=precision,
    controlnet=controlnet,
)

pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.to("cuda")  # Use GPU for faster inference


# 🎨 Image generation function
@spaces.GPU
def generate_image(prompt, input_image, strength, guidance, controlnet_conditioning_scale):
    
    # Generate styled image using ControlNet
    result = pipe(
        prompt=prompt,
        image=input_image,
        num_inference_steps=30,
        guidance_scale=guidance,
        controlnet_conditioning_scale=float(controlnet_conditioning_scale),
        strength=strength
    ).images[0]
    
    return result


# πŸ–₯️ Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("# πŸ—οΈ 3D Screenshot to Styled Render with ADE20k Segmentation ControlNet")

    with gr.Row():
        with gr.Column():
            input_image = gr.Image(label="Upload a segmented image", type="pil")
            prompt = gr.Textbox(label="Style Prompt", placeholder="e.g., Futuristic building in sunset")
            
            strength = gr.Slider(0.1, 1.0, value=0.7, label="Denoising Strength")
            guidance = gr.Slider(1, 20, value=7.5, label="Guidance Scale (Creativity)")
            controlnet_conditioning_scale = gr.Slider(0, 2, value=0.5, step=0.01, label="ControlNet Conditioning Scale")
            
            generate_button = gr.Button("Generate Styled Image")

        with gr.Column():
            result_output = gr.Image(label="Generated Styled Image")

    # πŸ”— Generate Button Action
    generate_button.click(
        fn=generate_image,
        inputs=[prompt, input_image, strength, guidance, controlnet_conditioning_scale],
        outputs=[result_output]
    )

# πŸš€ Launch the app
demo.launch()