File size: 2,171 Bytes
8ad30ba
 
98e3f08
8ad30ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98e3f08
8ad30ba
 
 
 
 
 
 
 
98e3f08
8ad30ba
 
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
# pip install transformers gradio scipy ftfy "ipywidgets>=7,<8" datasets diffusers

import gradio as gr
import torch
from torch import autocast
from diffusers import StableDiffusionPipeline

model_id = "hakurei/waifu-diffusion"
device = "cpu"

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

block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")

num_samples = 2


def infer(prompt):
    with autocast("cuda"):
        images = pipe([prompt] * num_samples,
                      hight=111,
                      width=100,

                      guidance_scale=7.5)["sample"]

    return images


with block as demo:
    gr.Markdown("<h1><center>Waifu Diffusion</center></h1>")
    gr.Markdown(
        "waifu-diffusion is a latent text-to-image diffusion model that has been conditioned on high-quality anime images through fine-tuning."
    )
    with gr.Group():
        with gr.Box():
            with gr.Row().style(mobile_collapse=False, equal_height=True):
                text = gr.Textbox(
                    label="Enter your prompt", show_label=False, max_lines=1
                ).style(
                    border=(True, False, True, True),
                    rounded=(True, False, False, True),
                    container=False,
                )
                btn = gr.Button("Run").style(
                    margin=False,
                    rounded=(False, True, True, False),
                )

        gallery = gr.Gallery(label="Generated images", show_label=False).style(
            grid=[2], height="auto"
        )
        text.submit(infer, inputs=[text], outputs=gallery)
        btn.click(infer, inputs=[text], outputs=gallery)

    gr.Markdown(
        """___
      <p style='text-align: center
      '>
      Created by https://huggingface.co/hakurei
      <br/>
      </p>"""
    )

demo.launch(debug=True,
            server_port=7860)