File size: 2,712 Bytes
8ad30ba
783bf66
8ad30ba
98e3f08
8ad30ba
 
783bf66
8ad30ba
 
783bf66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
831a077
 
783bf66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# pip install transformers gradio scipy ftfy "ipywidgets>=7,<8" datasets diffusers
import random

import gradio as gr
import torch
from torch import autocast
from diffusers.pipelines.stable_diffusion import StableDiffusionPipeline

model_id = "hakurei/waifu-diffusion"
device = "cuda" if torch.cuda.is_available() else "cpu"


def __def_helper():
    StableDiffusionPipeline.__call__()


pipe = StableDiffusionPipeline.from_pretrained(model_id,
                                               resume_download=True,  # 模型文件断点续传
                                               torch_dtype=torch.float16,
                                               revision='fp16')
pipe = pipe.to(device)


def infer(prompt, width, height, nums, steps, guidance_scale, seed):
    print(prompt, width, height, nums, steps, guidance_scale, seed)

    if prompt is not None and prompt != "":

        if seed is None or seed == '' or seed == -1:
            seed = int(random.randrange(4294967294))

        with autocast("cuda"):
            generator = torch.Generator("cuda").manual_seed(seed)

            images = pipe([prompt] * nums,
                          height=height,
                          width=width,
                          num_inference_steps=steps,
                          generator=generator,
                          guidance_scale=guidance_scale
                          )["sample"]
            return images


description = """
prompt 素材:[https://lexica.art](https://lexica.art) \n
seed:为空会使用随机seed

"""


# with block as demo:
def run():
    _app = gr.Interface(
        fn=infer,
        title="Waifu Diffusion",
        description=description,
        inputs=[
            gr.Textbox(label="输入 prompt"),
            gr.Slider(512, 1024, 512, step=64, label="width"),
            gr.Slider(512, 1024, 512, step=64, label="height"),
            gr.Slider(1, 4, 1, step=1, label="Number of Images"),
            gr.Slider(10, 150, step=1, value=50,
                      label="num_inference_steps:\n"
                            "去噪步骤的数量。更多的去噪步骤通常会导致更高质量的图像,但会降低推理速度。"),
            gr.Slider(0, 20, 7.5, step=0.5,
                      label="guidance_scale:\n" +
                            "较高的引导比例鼓励生成与文本“提示”密切相关的图像,通常以降低图像质量为代价"),
            gr.Textbox(label="随机 send",
                       placeholder="Random Seed",
                       lines=1),
        ],
        outputs=[
            gr.Gallery(label="Generated images")
        ])

    return _app


app = run()
app.launch(debug=True)