testinp commited on
Commit
9ec10d6
1 Parent(s): b5e7e94

add waifu diffusion code

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -1,7 +1,61 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from torch import autocast
4
+ from diffusers import StableDiffusionPipeline
5
 
6
+ model_id = "hakurei/waifu-diffusion"
7
+ device = "cuda"
8
 
9
+
10
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision='fp16')
11
+ pipe = pipe.to(device)
12
+
13
+
14
+ block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
15
+
16
+ num_samples = 2
17
+
18
+ def infer(prompt):
19
+ with autocast("cuda"):
20
+ images = pipe([prompt] * num_samples, guidance_scale=7.5)["sample"]
21
+
22
+ return images
23
+
24
+
25
+ with block as demo:
26
+ gr.Markdown("<h1><center>Waifu Diffusion</center></h1>")
27
+ gr.Markdown(
28
+ "waifu-diffusion is a latent text-to-image diffusion model that has been conditioned on high-quality anime images through fine-tuning."
29
+ )
30
+ with gr.Group():
31
+ with gr.Box():
32
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
33
+
34
+ text = gr.Textbox(
35
+ label="Enter your prompt", show_label=False, max_lines=1
36
+ ).style(
37
+ border=(True, False, True, True),
38
+ rounded=(True, False, False, True),
39
+ container=False,
40
+ )
41
+ btn = gr.Button("Run").style(
42
+ margin=False,
43
+ rounded=(False, True, True, False),
44
+ )
45
+
46
+ gallery = gr.Gallery(label="Generated images", show_label=False).style(
47
+ grid=[2], height="auto"
48
+ )
49
+ text.submit(infer, inputs=[text], outputs=gallery)
50
+ btn.click(infer, inputs=[text], outputs=gallery)
51
+
52
+ gr.Markdown(
53
+ """___
54
+ <p style='text-align: center'>
55
+ Created by https://huggingface.co/hakurei
56
+ <br/>
57
+ </p>"""
58
+ )
59
+
60
+
61
+ demo.launch(debug=True)