File size: 2,257 Bytes
a98a9f8
02e8a9e
a98a9f8
 
 
 
 
 
 
02e8a9e
a98a9f8
 
02e8a9e
a98a9f8
 
 
 
 
 
 
 
 
 
 
 
 
02e8a9e
 
 
 
 
 
 
a98a9f8
 
 
 
 
 
 
 
 
 
02e8a9e
a98a9f8
 
 
 
 
 
 
 
 
 
 
02e8a9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
import gradio as gr
import torch
import numpy as np
import random
from diffusers import DiffusionPipeline

device = "cuda" if torch.cuda.is_available() else "cpu"

if torch.cuda.is_available():
    pipe = DiffusionPipeline.from_pretrained("civit-ai/wanostyle_2_offset", use_safetensors=True)
    pipe = pipe.to(device)
else: 
    pipe = DiffusionPipeline.from_pretrained("civit-ai/wanostyle_2_offset", use_safetensors=True)
    pipe = pipe.to(device)

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024

def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):

    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
        
    generator = torch.Generator().manual_seed(seed)
    
    image = pipe(
        prompt=prompt, 
        negative_prompt=negative_prompt,
        guidance_scale=guidance_scale, 
        num_inference_steps=num_inference_steps, 
        width=width, 
        height=height,
        generator=generator
    ).images[0] 
    
    return image

examples = [
    "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
    "An astronaut riding a green horse",
    "A delicious ceviche cheesecake slice",
]

css = """
#col-container {
    margin: 0 auto;
    max-width: 520px;
}
"""

if torch.cuda.is_available():
    power_device = "GPU"
else:
    power_device = "CPU"

gr.Interface(
    fn=infer, 
    inputs=[
        gr.inputs.Text(label="Prompt", placeholder="Enter your prompt"),
        gr.inputs.Text(label="Negative Prompt", visible=False),
        gr.inputs.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, default=0),
        gr.inputs.Checkbox(label="Randomize Seed", default=True),
        gr.inputs.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, default=512),
        gr.inputs.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, default=512),
        gr.inputs.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, default=0.0),
        gr.inputs.Slider(label="Number of Inference Steps", minimum=1, maximum=12, step=1, default=2)
    ],
    outputs=gr.outputs.Image(label="Result"),
    title="Text-to-Image Gradio Template",
    css=css,
    examples=examples
).launch()