Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
from diffusers import StableDiffusionPipeline, AutoencoderKL
|
| 10 |
+
|
| 11 |
+
def gen_seed():
|
| 12 |
+
random_data = os.urandom(3)
|
| 13 |
+
seed = int.from_bytes(random_data, byteorder="big")
|
| 14 |
+
return seed
|
| 15 |
+
|
| 16 |
+
repo = "IDKiro/sdxs-512-0.9"
|
| 17 |
+
weight_type = torch.float32 # or float16
|
| 18 |
+
|
| 19 |
+
# Load model.
|
| 20 |
+
pipe = StableDiffusionPipeline.from_pretrained(repo, torch_dtype=weight_type)
|
| 21 |
+
|
| 22 |
+
# use original VAE
|
| 23 |
+
# pipe.vae = AutoencoderKL.from_pretrained("IDKiro/sdxs-512-0.9/vae_large")
|
| 24 |
+
|
| 25 |
+
#pipe.to("cuda")
|
| 26 |
+
|
| 27 |
+
prompt = "portrait photo of a girl, photograph, highly detailed face, depth of field, moody light, golden hour"
|
| 28 |
+
|
| 29 |
+
def sdxs_run(prompt, steps, guidance, seed):
|
| 30 |
+
# Ensure using 1 inference step and CFG set to 0.
|
| 31 |
+
image = pipe(
|
| 32 |
+
prompt,
|
| 33 |
+
num_inference_steps=steps,
|
| 34 |
+
guidance_scale=guidance,
|
| 35 |
+
generator=torch.Generator(device="cpu").manual_seed(seed)
|
| 36 |
+
).images[0]
|
| 37 |
+
return image
|
| 38 |
+
|
| 39 |
+
#image.save("output.png")
|
| 40 |
+
|
| 41 |
+
def update_seed(rand, seed):
|
| 42 |
+
if rand:
|
| 43 |
+
return gen_seed()
|
| 44 |
+
else:
|
| 45 |
+
return seed
|
| 46 |
+
|
| 47 |
+
desc = """# SDXS CPU Test Space
|
| 48 |
+
Just a quick test. Model is `sdxs-512-0.9` for txt2img.
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown(desc)
|
| 53 |
+
with gr.Group():
|
| 54 |
+
with gr.Row():
|
| 55 |
+
img = gr.Image(label='SDXS Generated Image')
|
| 56 |
+
with gr.Row():
|
| 57 |
+
prompt = gr.Textbox(label='Enter your prompt (English)', scale=8, value="portrait photo of a girl, photograph, highly detailed face, depth of field, moody light, golden hour")
|
| 58 |
+
with gr.Accordion("More options", open=False):
|
| 59 |
+
steps = gr.Slider(label="Number of steps", value=1, minimum=1, maximum=20, step=1)
|
| 60 |
+
guidance = gr.Slider(label="Guidance", value=0, minimum=0, maximum=2, step=0.1)
|
| 61 |
+
seed = gr.Slider(label="Seed", minimum=20, maximum=100000000, step=1, randomize=True)
|
| 62 |
+
rand = gr.Checkbox(label="Randomize Seed After Generation?", value=True)
|
| 63 |
+
with gr.Row():
|
| 64 |
+
submit = gr.Button(scale=1, variant='primary')
|
| 65 |
+
#clear = gr.ClearButton(components=[])
|
| 66 |
+
submit.click(fn=sdxs_run, inputs=[prompt, steps, guidance, seed], outputs=img).then(fn=update_seed, inputs=[rand, seed], outputs=seed)
|
| 67 |
+
|
| 68 |
+
demo.queue(max_size=20).launch()
|