Spaces:
Runtime error
Runtime error
good app
Browse files
app.py
CHANGED
|
@@ -1,69 +1,80 @@
|
|
| 1 |
# pip install transformers gradio scipy ftfy "ipywidgets>=7,<8" datasets diffusers
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from torch import autocast
|
| 6 |
-
from diffusers import StableDiffusionPipeline
|
| 7 |
|
| 8 |
model_id = "hakurei/waifu-diffusion"
|
| 9 |
-
device = "cpu"
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# pip install transformers gradio scipy ftfy "ipywidgets>=7,<8" datasets diffusers
|
| 2 |
+
import random
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
from torch import autocast
|
| 7 |
+
from diffusers.pipelines.stable_diffusion import StableDiffusionPipeline
|
| 8 |
|
| 9 |
model_id = "hakurei/waifu-diffusion"
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def __def_helper():
|
| 14 |
+
StableDiffusionPipeline.__call__()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id,
|
| 18 |
+
resume_download=True, # 模型文件断点续传
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
+
revision='fp16')
|
| 21 |
+
pipe = pipe.to(device)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def infer(prompt, width, height, nums, steps, guidance_scale, seed):
|
| 25 |
+
print(prompt, width, height, nums, steps, guidance_scale, seed)
|
| 26 |
+
|
| 27 |
+
if prompt is not None and prompt != "":
|
| 28 |
+
|
| 29 |
+
if seed is None or seed == '' or seed == -1:
|
| 30 |
+
seed = int(random.randrange(4294967294))
|
| 31 |
+
|
| 32 |
+
with autocast("cuda"):
|
| 33 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 34 |
+
|
| 35 |
+
images = pipe([prompt] * nums,
|
| 36 |
+
height=height,
|
| 37 |
+
width=width,
|
| 38 |
+
num_inference_steps=steps,
|
| 39 |
+
generator=generator,
|
| 40 |
+
guidance_scale=guidance_scale
|
| 41 |
+
)["sample"]
|
| 42 |
+
return images
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
description = """
|
| 46 |
+
prompt 素材:[https://lexica.art](https://lexica.art) \n
|
| 47 |
+
seed:为空会使用随机seed
|
| 48 |
+
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# with block as demo:
|
| 53 |
+
def run():
|
| 54 |
+
_app = gr.Interface(
|
| 55 |
+
fn=infer,
|
| 56 |
+
title="Waifu Diffusion",
|
| 57 |
+
description=description,
|
| 58 |
+
inputs=[
|
| 59 |
+
gr.Textbox(label="输入 prompt"),
|
| 60 |
+
gr.Slider(512, 1024, 512, step=64, label="width"),
|
| 61 |
+
gr.Slider(512, 1024, 512, step=64, label="height"),
|
| 62 |
+
gr.Slider(1, 4, 1, step=1, label="Number of Images"),
|
| 63 |
+
gr.Slider(10, 150, step=1, value=50,
|
| 64 |
+
label="num_inference_steps"),
|
| 65 |
+
gr.Slider(0, 20, 7.5, step=0.5,
|
| 66 |
+
label="guidance_scale:\n" +
|
| 67 |
+
"较高的引导比例鼓励生成与文本“提示”密切相关的图像,通常以降低图像质量为代价"),
|
| 68 |
+
gr.Textbox(label="随机 send",
|
| 69 |
+
placeholder="Random Seed",
|
| 70 |
+
lines=1),
|
| 71 |
+
],
|
| 72 |
+
outputs=[
|
| 73 |
+
gr.Gallery(label="Generated images")
|
| 74 |
+
])
|
| 75 |
+
|
| 76 |
+
return _app
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
app = run()
|
| 80 |
+
app.launch(debug=True)
|
app2.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pip install transformers gradio scipy ftfy "ipywidgets>=7,<8" datasets diffusers
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from torch import autocast
|
| 6 |
+
from diffusers import StableDiffusionPipeline
|
| 7 |
+
|
| 8 |
+
model_id = "hakurei/waifu-diffusion"
|
| 9 |
+
device = "cpu"
|
| 10 |
+
|
| 11 |
+
# pipe = StableDiffusionPipeline.from_pretrained(model_id,
|
| 12 |
+
# resume_download=True, # 模型文件断点续传
|
| 13 |
+
# torch_dtype=torch.float16,
|
| 14 |
+
# revision='fp16')
|
| 15 |
+
# pipe = pipe.to(device)
|
| 16 |
+
|
| 17 |
+
block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
|
| 18 |
+
|
| 19 |
+
num_samples = 2
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def infer(prompt):
|
| 23 |
+
print(prompt)
|
| 24 |
+
return prompt
|
| 25 |
+
# with autocast("cuda"):
|
| 26 |
+
# images = pipe([prompt] * num_samples,
|
| 27 |
+
# hight=111,
|
| 28 |
+
# width=100,
|
| 29 |
+
#
|
| 30 |
+
# guidance_scale=7.5)["sample"]
|
| 31 |
+
|
| 32 |
+
# return images
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
with block as demo:
|
| 36 |
+
gr.Markdown("<h1><center>Waifu Diffusion</center></h1>")
|
| 37 |
+
gr.Markdown(
|
| 38 |
+
"waifu-diffusion is a latent text-to-image diffusion model that has been conditioned on high-quality anime images through fine-tuning."
|
| 39 |
+
)
|
| 40 |
+
with gr.Group():
|
| 41 |
+
with gr.Box():
|
| 42 |
+
with gr.Column().style(mobile_collapse=False, equal_height=True):
|
| 43 |
+
text = gr.Textbox(
|
| 44 |
+
label="Enter your prompt",
|
| 45 |
+
show_label=False,
|
| 46 |
+
max_lines=1
|
| 47 |
+
).style(
|
| 48 |
+
border=(True, False, True, True),
|
| 49 |
+
rounded=(True, False, False, True),
|
| 50 |
+
container=False,
|
| 51 |
+
)
|
| 52 |
+
slider = gr.Slider(0, 1000, 10)
|
| 53 |
+
|
| 54 |
+
btn = gr.Button("Run").style(
|
| 55 |
+
margin=False,
|
| 56 |
+
rounded=(False, True, True, False),
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
gallery = gr.Gallery(
|
| 60 |
+
label="Generated images",
|
| 61 |
+
show_label=False
|
| 62 |
+
).style(
|
| 63 |
+
grid=[2],
|
| 64 |
+
height="auto"
|
| 65 |
+
)
|
| 66 |
+
gr.Interface
|
| 67 |
+
text.submit(infer,
|
| 68 |
+
inputs=[text,slider] ,
|
| 69 |
+
outputs=gallery)
|
| 70 |
+
btn.click(infer, inputs=[text], outputs=gallery)
|
| 71 |
+
|
| 72 |
+
gr.Markdown(
|
| 73 |
+
"""___
|
| 74 |
+
<p style='text-align: center
|
| 75 |
+
'>
|
| 76 |
+
Created by https://huggingface.co/hakurei
|
| 77 |
+
<br/>
|
| 78 |
+
</p>"""
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
demo.launch(debug=True)
|