Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485# Importsimport gradio as grimport randomimport spacesimport torchimport numpyimport uuidimport jsonimport osfrom diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteSchedulerfrom PIL import Image
# Pre-InitializeDEVICE = "auto"if DEVICE == "auto": DEVICE = "cuda" if torch.cuda.is_available() else "cpu"print(f"[SYSTEM] | Using {DEVICE} type compute device.")
# Variables
MAX_SEED = 9007199254740991DEFAULT_INPUT = ""DEFAULT_NEGATIVE_INPUT = ""DEFAULT_HEIGHT = 1024DEFAULT_WIDTH = 1024
REPO = "sd-community/sdxl-flash"REPO_WEIGHT = "ehristoforu/dalle-3-xl-v2"WEIGHT = "dalle-3-xl-lora-v2.safetensors"ADAPTER = "dalle"
model = StableDiffusionXLPipeline.from_pretrained(REPO, torch_dtype=torch.float16, use_safetensors=True, add_watermarker=False)model.scheduler = EulerAncestralDiscreteScheduler.from_config(model.scheduler.config)model.load_lora_weights(REPO_WEIGHT, weight_name=WEIGHT, adapter_name=ADAPTER)model.set_adapters(ADAPTER, adapter_weights=[0.7])model.to(DEVICE)
def get_seed(seed): seed = seed.strip() if seed.isdigit(): return int(seed) else: return random.randint(0, MAX_SEED)
@spaces.GPU(duration=30)def generate(input=DEFAULT_INPUT, negative_input=DEFAULT_NEGATIVE_INPUT, height=DEFAULT_HEIGHT, width=DEFAULT_WIDTH, steps=1, guidance=0, seed=None): print(input, negative_input, height, width, steps, guidance, seed)
pipe.to(DEVICE) seed = int(randomize_seed_fn(seed, randomize_seed))
parameters = { "prompt": prompt, "negative_prompt": negative_prompt, "height": height, "width": width, "num_inference_steps": steps, "guidance_scale": guidance_scale, "generator": torch.Generator().manual_seed(get_seed(seed)), "use_resolution_binning": True, "output_type":"pil", } images = pipe(**parameters).images image_paths = [save_image(img) for img in images] return image_paths
with gr.Blocks() as main: with gr.Column(): input = gr.Textbox(lines=1, value=DEFAULT_INPUT, label="Input") negative_input = gr.Textbox(lines=1, value=DEFAULT_NEGATIVE_INPUT, label="Input Negative") height = gr.Slider(minimum=1, maximum=2160, step=1, value=DEFAULT_HEIGHT, label="Height") width = gr.Slider(minimum=1, maximum=2160, step=1, value=DEFAULT_WIDTH, label="Width") steps = gr.Slider(minimum=0, maximum=100, step=1, value=1, label="Steps") guidance = gr.Slider(minimum=0, maximum=100, step=0.001, value=0, label = "Guidance") seed = gr.Textbox(lines=1, value="", label="Seed (Blank for random)") submit = gr.Button("▶")
with gr.Column(): image = gr.Image(label="Image") submit.click(generate, inputs=[input, negative_input, height, width, steps, guidance, seed], outputs=[image])
main.launch(show_api=True)app.pyAdded+85