nimool commited on
Commit
65a40f7
1 Parent(s): c71aff3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
4
+ from huggingface_hub import hf_hub_download
5
+ from safetensors.torch import load_file
6
+ import spaces
7
+ from PIL import Image
8
+
9
+ SAFETY_CHECKER = True
10
+
11
+ # Constants
12
+ base = "stabilityai/stable-diffusion-xl-base-1.0"
13
+ repo = "ByteDance/SDXL-Lightning"
14
+ checkpoints = {
15
+ "1-Step" : ["sdxl_lightning_1step_unet_x0.safetensors", 1],
16
+ "2-Step" : ["sdxl_lightning_2step_unet.safetensors", 2],
17
+ "4-Step" : ["sdxl_lightning_4step_unet.safetensors", 4],
18
+ "8-Step" : ["sdxl_lightning_8step_unet.safetensors", 8],
19
+ }
20
+ loaded = None
21
+
22
+ # Ensure model and scheduler are initialized in GPU-enabled function
23
+ if torch.cuda.is_available():
24
+ pipe = StableDiffusionXLPipeline.from_pretrained(base, torch_dtype=torch.float16, variant="fp16").to("cuda")
25
+
26
+ if SAFETY_CHECKER:
27
+ from safety_checker import StableDiffusionSafetyChecker
28
+ from transformers import CLIPFeatureExtractor
29
+
30
+ safety_checker = StableDiffusionSafetyChecker.from_pretrained(
31
+ "CompVis/stable-diffusion-safety-checker"
32
+ ).to("cuda")
33
+ feature_extractor = CLIPFeatureExtractor.from_pretrained(
34
+ "openai/clip-vit-base-patch32"
35
+ )
36
+
37
+ def check_nsfw_images(
38
+ images: list[Image.Image],
39
+ ) -> tuple[list[Image.Image], list[bool]]:
40
+ safety_checker_input = feature_extractor(images, return_tensors="pt").to("cuda")
41
+ has_nsfw_concepts = safety_checker(
42
+ images=[images],
43
+ clip_input=safety_checker_input.pixel_values.to("cuda")
44
+ )
45
+
46
+ return images, has_nsfw_concepts
47
+
48
+ # Function
49
+ @spaces.GPU(enable_queue=True)
50
+ def generate_image(prompt, ckpt):
51
+ global loaded
52
+ print(prompt, ckpt)
53
+
54
+ checkpoint = checkpoints[ckpt][0]
55
+ num_inference_steps = checkpoints[ckpt][1]
56
+
57
+ if loaded != num_inference_steps:
58
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample" if num_inference_steps==1 else "epsilon")
59
+ pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device="cuda"))
60
+ loaded = num_inference_steps
61
+
62
+ results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0)
63
+
64
+ if SAFETY_CHECKER:
65
+ images, has_nsfw_concepts = check_nsfw_images(results.images)
66
+ if any(has_nsfw_concepts):
67
+ gr.Warning("NSFW content detected.")
68
+ return Image.new("RGB", (512, 512))
69
+ return images[0]
70
+ return results.images[0]
71
+
72
+
73
+
74
+ # Gradio Interface
75
+
76
+ with gr.Blocks(css="style.css") as demo:
77
+ gr.HTML("<h1><center>SDXL-Lightning ⚡</center></h1>")
78
+ gr.HTML("<p><center>Lightning-fast text-to-image generation</center></p><p><center><a href='https://huggingface.co/ByteDance/SDXL-Lightning'>https://huggingface.co/ByteDance/SDXL-Lightning</a></center></p>")
79
+ with gr.Group():
80
+ with gr.Row():
81
+ prompt = gr.Textbox(label='Enter your prompt (English)', scale=8)
82
+ ckpt = gr.Dropdown(label='Select inference steps',choices=['1-Step', '2-Step', '4-Step', '8-Step'], value='4-Step', interactive=True)
83
+ submit = gr.Button(scale=1, variant='primary')
84
+ img = gr.Image(label='SDXL-Lightning Generated Image')
85
+
86
+ prompt.submit(fn=generate_image,
87
+ inputs=[prompt, ckpt],
88
+ outputs=img,
89
+ )
90
+ submit.click(fn=generate_image,
91
+ inputs=[prompt, ckpt],
92
+ outputs=img,
93
+ )
94
+
95
+ demo.queue().launch()