File size: 2,981 Bytes
8776e89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0c86d7
8776e89
 
e7ba981
8776e89
 
 
 
 
 
 
 
 
 
 
 
d0c86d7
 
8776e89
d0c86d7
8776e89
 
 
 
 
ee4fae6
8776e89
d0c86d7
 
 
 
 
 
 
 
 
 
ee4fae6
d0c86d7
 
 
ee4fae6
d0c86d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94ed80b
 
 
 
 
7a65eac
d0c86d7
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import jax
import numpy as np
import jax.numpy as jnp
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from PIL import Image
from diffusers import FlaxStableDiffusionPipeline

def create_key(seed=0):
    return jax.random.PRNGKey(seed)


pipe, params = FlaxStableDiffusionPipeline.from_pretrained(
    "MuhammadHanif/stable-diffusion-v1-5-high-res", 
    dtype=jnp.bfloat16, 
    use_memory_efficient_attention=True
)

def infer(prompts, negative_prompts, width=1088, height=1088, inference_steps=30, seed=0):
    
    num_samples = 1 #jax.device_count()
    rng = create_key(int(seed))
    rng = jax.random.split(rng, jax.device_count())
    
    prompt_ids = pipe.prepare_inputs([prompts] * num_samples)
    negative_prompt_ids = pipe.prepare_inputs([negative_prompts] * num_samples)
    
    p_params = replicate(params)
    prompt_ids = shard(prompt_ids)
    negative_prompt_ids = shard(negative_prompt_ids)
    
    output = pipe(
        prompt_ids=prompt_ids,
        params=p_params,
        height=height,
        width=width,
        prng_seed=rng,
        num_inference_steps=inference_steps,
        neg_prompt_ids=negative_prompt_ids,
        jit=True,
    ).images
    
    output_images = pipe.numpy_to_pil(np.asarray(output.reshape((num_samples,) + output.shape[-3:])))
    return output_images[0]

prompt_input = gr.inputs.Textbox(
    label="Prompt", 
    placeholder="a highly detailed mansion in the autumn by studio ghibli, makoto shinkai"
)
neg_prompt_input = gr.inputs.Textbox(
    label="Negative Prompt", 
    placeholder=""
)

width_slider = gr.inputs.Slider(
    minimum=512, maximum=2048, default=1088, step=64, label="width"
)

height_slider = gr.inputs.Slider(
    minimum=512, maximum=2048, default=1088, step=64, label="height"
)

inf_steps_input = gr.inputs.Slider(
    minimum=1, maximum=100, default=30, step=1, label="Inference Steps"
)


seed_input = gr.inputs.Number(default=0, label="Seed")

app = gr.Interface(
    fn=infer,
    inputs=[prompt_input, neg_prompt_input, width_slider, height_slider, inf_steps_input, seed_input],
    outputs="image",
    title="Stable Diffusion High Resolution",
    description=(
        "Based on stable diffusion 1.5 and fine-tuned on 576x576 up to 1088x1088 images, "
        "Stable Diffusion High Resolution is compartible with another SD1.5 model and mergeable with other SD1.5 model, "
        "giving other model to generate high resolution images without using upscaler."
    ),
    examples=[
        ["a highly detailed mansion in the autumn by studio ghibli, makoto shinkai","", 1088, 1088, 30, 0],
        ["best high quality landscape, in the morning light, Overlooking TOKYO beautiful city with Fujiyama, from a tall house, by greg rutkowski and thomas kinkade, Trending on artstation makoto shinkai style","", 1088, 576, 30, 0],
        [" assassin's creed black flag, hd, 4k, dlsr ","", 960, 960, 30, 4154731],
    ],
    
)

app.launch()