Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ from PIL import Image
|
|
9 |
|
10 |
SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", "0") == "1"
|
11 |
|
12 |
-
# Constants
|
13 |
base = "stabilityai/stable-diffusion-xl-base-1.0"
|
14 |
repo = "ByteDance/SDXL-Lightning"
|
15 |
checkpoints = {
|
@@ -26,13 +26,12 @@ if torch.cuda.is_available():
|
|
26 |
|
27 |
if SAFETY_CHECKER:
|
28 |
from safety_checker import StableDiffusionSafetyChecker
|
29 |
-
from transformers import CLIPFeatureExtractor
|
30 |
|
31 |
safety_checker = StableDiffusionSafetyChecker.from_pretrained(
|
32 |
"CompVis/stable-diffusion-safety-checker"
|
33 |
).to("cuda")
|
34 |
-
|
35 |
-
feature_extractor = CLIPProcessor.from_pretrained(
|
36 |
"openai/clip-vit-base-patch32"
|
37 |
)
|
38 |
|
@@ -45,11 +44,10 @@ if SAFETY_CHECKER:
|
|
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]
|
@@ -58,20 +56,21 @@ def generate_image(prompt, ckpt):
|
|
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=
|
63 |
|
64 |
if SAFETY_CHECKER:
|
65 |
images, has_nsfw_concepts = check_nsfw_images(results.images)
|
66 |
if any(has_nsfw_concepts):
|
67 |
-
gr.
|
68 |
-
return Image.new("RGB", (512, 512))
|
69 |
return images[0]
|
70 |
return results.images[0]
|
71 |
|
72 |
-
# Gradio Interface
|
73 |
description = """
|
74 |
-
Welcome
|
|
|
|
|
75 |
"""
|
76 |
|
77 |
with gr.Blocks(css="style.css") as demo:
|
@@ -81,10 +80,11 @@ with gr.Blocks(css="style.css") as demo:
|
|
81 |
with gr.Row():
|
82 |
prompt = gr.Textbox(label='Your cosmic prompt (English)', scale=8)
|
83 |
ckpt = gr.Dropdown(label='Warp Factor (Inference Speed)', choices=['Warp 1', 'Warp 2', 'Warp 4', 'Warp 8'], value='Warp 4', interactive=True)
|
84 |
-
submit = gr.Button(scale=1, variant='primary')
|
85 |
img = gr.Image(label='The Universe, As You Envision It')
|
86 |
|
87 |
prompt.submit(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
|
88 |
submit.click(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
|
89 |
|
90 |
demo.queue().launch()
|
|
|
|
9 |
|
10 |
SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", "0") == "1"
|
11 |
|
12 |
+
# Constants for Starfleet Command theme
|
13 |
base = "stabilityai/stable-diffusion-xl-base-1.0"
|
14 |
repo = "ByteDance/SDXL-Lightning"
|
15 |
checkpoints = {
|
|
|
26 |
|
27 |
if SAFETY_CHECKER:
|
28 |
from safety_checker import StableDiffusionSafetyChecker
|
29 |
+
from transformers import CLIPFeatureExtractor
|
30 |
|
31 |
safety_checker = StableDiffusionSafetyChecker.from_pretrained(
|
32 |
"CompVis/stable-diffusion-safety-checker"
|
33 |
).to("cuda")
|
34 |
+
feature_extractor = CLIPFeatureExtractor.from_pretrained(
|
|
|
35 |
"openai/clip-vit-base-patch32"
|
36 |
)
|
37 |
|
|
|
44 |
|
45 |
return images, has_nsfw_concepts
|
46 |
|
|
|
47 |
@spaces.GPU(enable_queue=True)
|
48 |
def generate_image(prompt, ckpt):
|
49 |
global loaded
|
50 |
+
print("🌌 Starfleet Command: ", prompt, ckpt)
|
51 |
|
52 |
checkpoint = checkpoints[ckpt][0]
|
53 |
num_inference_steps = checkpoints[ckpt][1]
|
|
|
56 |
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample" if num_inference_steps == 1 else "epsilon")
|
57 |
pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device="cuda"))
|
58 |
loaded = num_inference_steps
|
59 |
+
|
60 |
+
results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=7.5)
|
61 |
|
62 |
if SAFETY_CHECKER:
|
63 |
images, has_nsfw_concepts = check_nsfw_images(results.images)
|
64 |
if any(has_nsfw_concepts):
|
65 |
+
gr.Alert("🚨 NSFW content detected. Displaying a safe placeholder instead.").show()
|
66 |
+
return Image.new("RGB", (512, 512), "black")
|
67 |
return images[0]
|
68 |
return results.images[0]
|
69 |
|
|
|
70 |
description = """
|
71 |
+
🌌 Welcome to Starfleet Command's Text-to-Image Warp Drive - SDXL-Lightning ⚡.
|
72 |
+
Utilizing ByteDance's SDXL-Lightning model, this interface allows you to generate high-quality cosmic images from textual prompts.
|
73 |
+
Embark on your voyage of imagination and see the universe through the lens of advanced AI.
|
74 |
"""
|
75 |
|
76 |
with gr.Blocks(css="style.css") as demo:
|
|
|
80 |
with gr.Row():
|
81 |
prompt = gr.Textbox(label='Your cosmic prompt (English)', scale=8)
|
82 |
ckpt = gr.Dropdown(label='Warp Factor (Inference Speed)', choices=['Warp 1', 'Warp 2', 'Warp 4', 'Warp 8'], value='Warp 4', interactive=True)
|
83 |
+
submit = gr.Button(text="Engage", scale=1, variant='primary')
|
84 |
img = gr.Image(label='The Universe, As You Envision It')
|
85 |
|
86 |
prompt.submit(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
|
87 |
submit.click(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
|
88 |
|
89 |
demo.queue().launch()
|
90 |
+
|