Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Imports
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import random
|
| 4 |
+
import spaces
|
| 5 |
+
import torch
|
| 6 |
+
import numpy
|
| 7 |
+
import uuid
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
+
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 11 |
+
from PIL import Image
|
| 12 |
+
|
| 13 |
+
# Pre-Initialize
|
| 14 |
+
DEVICE = "auto"
|
| 15 |
+
if DEVICE == "auto":
|
| 16 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
+
print(f"[SYSTEM] | Using {DEVICE} type compute device.")
|
| 18 |
+
|
| 19 |
+
# Variables
|
| 20 |
+
|
| 21 |
+
MAX_SEED = 9007199254740991
|
| 22 |
+
DEFAULT_INPUT = ""
|
| 23 |
+
DEFAULT_NEGATIVE_INPUT = ""
|
| 24 |
+
DEFAULT_HEIGHT = 1024
|
| 25 |
+
DEFAULT_WIDTH = 1024
|
| 26 |
+
|
| 27 |
+
REPO = "sd-community/sdxl-flash"
|
| 28 |
+
REPO_WEIGHT = "ehristoforu/dalle-3-xl-v2"
|
| 29 |
+
WEIGHT = "dalle-3-xl-lora-v2.safetensors"
|
| 30 |
+
ADAPTER = "dalle"
|
| 31 |
+
|
| 32 |
+
model = StableDiffusionXLPipeline.from_pretrained(REPO, torch_dtype=torch.float16, use_safetensors=True, add_watermarker=False)
|
| 33 |
+
model.scheduler = EulerAncestralDiscreteScheduler.from_config(model.scheduler.config)
|
| 34 |
+
model.load_lora_weights(REPO_WEIGHT, weight_name=WEIGHT, adapter_name=ADAPTER)
|
| 35 |
+
model.set_adapters(ADAPTER, adapter_weights=[0.7])
|
| 36 |
+
model.to(DEVICE)
|
| 37 |
+
|
| 38 |
+
def get_seed(seed):
|
| 39 |
+
seed = seed.strip()
|
| 40 |
+
if seed.isdigit():
|
| 41 |
+
return int(seed)
|
| 42 |
+
else:
|
| 43 |
+
return random.randint(0, MAX_SEED)
|
| 44 |
+
|
| 45 |
+
@spaces.GPU(duration=30)
|
| 46 |
+
def generate(input=DEFAULT_INPUT, negative_input=DEFAULT_NEGATIVE_INPUT, height=DEFAULT_HEIGHT, width=DEFAULT_WIDTH, steps=1, guidance=0, seed=None):
|
| 47 |
+
|
| 48 |
+
print(input, negative_input, height, width, steps, guidance, seed)
|
| 49 |
+
|
| 50 |
+
pipe.to(DEVICE)
|
| 51 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 52 |
+
|
| 53 |
+
parameters = {
|
| 54 |
+
"prompt": prompt,
|
| 55 |
+
"negative_prompt": negative_prompt,
|
| 56 |
+
"height": height,
|
| 57 |
+
"width": width,
|
| 58 |
+
"num_inference_steps": steps,
|
| 59 |
+
"guidance_scale": guidance_scale,
|
| 60 |
+
"generator": torch.Generator().manual_seed(get_seed(seed)),
|
| 61 |
+
"use_resolution_binning": True,
|
| 62 |
+
"output_type":"pil",
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
images = pipe(**parameters).images
|
| 66 |
+
image_paths = [save_image(img) for img in images]
|
| 67 |
+
return image_paths
|
| 68 |
+
|
| 69 |
+
with gr.Blocks() as main:
|
| 70 |
+
with gr.Column():
|
| 71 |
+
input = gr.Textbox(lines=1, value=DEFAULT_INPUT, label="Input")
|
| 72 |
+
negative_input = gr.Textbox(lines=1, value=DEFAULT_NEGATIVE_INPUT, label="Input Negative")
|
| 73 |
+
height = gr.Slider(minimum=1, maximum=2160, step=1, value=DEFAULT_HEIGHT, label="Height")
|
| 74 |
+
width = gr.Slider(minimum=1, maximum=2160, step=1, value=DEFAULT_WIDTH, label="Width")
|
| 75 |
+
steps = gr.Slider(minimum=0, maximum=100, step=1, value=1, label="Steps")
|
| 76 |
+
guidance = gr.Slider(minimum=0, maximum=100, step=0.001, value=0, label = "Guidance")
|
| 77 |
+
seed = gr.Textbox(lines=1, value="", label="Seed (Blank for random)")
|
| 78 |
+
submit = gr.Button("▶")
|
| 79 |
+
|
| 80 |
+
with gr.Column():
|
| 81 |
+
image = gr.Image(label="Image")
|
| 82 |
+
|
| 83 |
+
submit.click(generate, inputs=[input, negative_input, height, width, steps, guidance, seed], outputs=[image])
|
| 84 |
+
|
| 85 |
+
main.launch(show_api=True)
|