File size: 1,891 Bytes
907171e
9c99cd9
72d181d
 
 
 
 
 
 
 
 
 
 
 
 
2467efd
 
72d181d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383cc6f
 
 
72d181d
 
 
 
 
 
aa165fb
 
72d181d
 
 
 
 
 
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

import torch
from PIL import Image
from diffusers import StableDiffusionImg2ImgPipeline
import gradio as gr

# Load the model
model_id_or_path = "stablediffusionapi/realistic-vision-v20-2047"
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float32).to(device)

def generate_image(content_img, style_img, prompt, guidance_scale, strength):
    if content_img is None or style_img is None:
        return None

    content_image = Image.open(content_img).convert("RGB").resize((450, 450))
    style_image = Image.open(style_img).convert("RGB").resize((450, 450))

    seed_value = torch.randint(0, 4294967295, size=(1,)).item()

    result = pipe(
        image=content_image,
        guidance=style_image,
        guidance_scale=guidance_scale,
        strength=strength,
        num_inference_steps=100,
        prompt=prompt,
        seed=seed_value
    ).images[0]

    return result

with gr.Blocks() as demo:
    gr.Markdown("# Advanced Neural Style Transfer with Stable Diffusion")

    with gr.Row():
        content_img = gr.Image(type="filepath", label="Upload Content Image")
        style_img = gr.Image(type="filepath", label="Upload Style Image")


    with gr.Row():
        prompt = gr.Textbox(label="Enter Stable Diffusion Prompt", placeholder="e.g., 'Van Gogh painting'")
        generate_btn = gr.Button("Generate")

    with gr.Row():
        guidance_scale = gr.Slider(5.0, 20.0, value=16.0, label="Guidance Scale: #light (content image emphasized) 5.0 - 10.0 #haevy (style image emphasized) 15.0 - 20.0")
        strength = gr.Slider(0.3, 1.0, value=0.45, label="Strength")

    output_image = gr.Image(label="Generated Image")

    generate_btn.click(generate_image, inputs=[content_img, style_img, prompt, guidance_scale, strength], outputs=output_image)

demo.launch()