Skip to content

Instantly share code, notes, and snippets.

@AK391
Created September 8, 2022 23:09
Show Gist options
  • Save AK391/954eaf3f0e767c57ef4c6aa400dc416a to your computer and use it in GitHub Desktop.
Save AK391/954eaf3f0e767c57ef4c6aa400dc416a to your computer and use it in GitHub Desktop.
colab for waifu diffusion
!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 = "cuda"
pipe = StableDiffusionPipeline.from_pretrained(model_id, 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, 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment