Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,96 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
|
| 9 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
else:
|
| 15 |
-
torch_dtype = torch.float32
|
| 16 |
|
| 17 |
-
|
| 18 |
-
pipe
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE =
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
prompt
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
-
guidance_scale,
|
| 33 |
-
num_inference_steps,
|
| 34 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
|
| 39 |
-
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
-
examples = [
|
| 55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 56 |
-
"An astronaut riding a green horse",
|
| 57 |
-
"A delicious ceviche cheesecake slice",
|
| 58 |
-
]
|
| 59 |
-
|
| 60 |
css = """
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
| 73 |
label="Prompt",
|
| 74 |
show_label=False,
|
| 75 |
max_lines=1,
|
| 76 |
-
placeholder="Enter your prompt",
|
| 77 |
container=False,
|
| 78 |
)
|
| 79 |
|
| 80 |
-
run_button = gr.Button("Run", scale=0
|
| 81 |
|
| 82 |
result = gr.Image(label="Result", show_label=False)
|
| 83 |
-
|
| 84 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 85 |
negative_prompt = gr.Text(
|
| 86 |
label="Negative prompt",
|
| 87 |
max_lines=1,
|
| 88 |
placeholder="Enter a negative prompt",
|
| 89 |
-
|
| 90 |
)
|
| 91 |
|
| 92 |
seed = gr.Slider(
|
|
@@ -105,7 +109,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 105 |
minimum=256,
|
| 106 |
maximum=MAX_IMAGE_SIZE,
|
| 107 |
step=32,
|
| 108 |
-
value=1024,
|
| 109 |
)
|
| 110 |
|
| 111 |
height = gr.Slider(
|
|
@@ -113,42 +117,30 @@ with gr.Blocks(css=css) as demo:
|
|
| 113 |
minimum=256,
|
| 114 |
maximum=MAX_IMAGE_SIZE,
|
| 115 |
step=32,
|
| 116 |
-
value=1024,
|
| 117 |
)
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
guidance_scale = gr.Slider(
|
| 121 |
label="Guidance scale",
|
| 122 |
minimum=0.0,
|
| 123 |
-
maximum=
|
| 124 |
step=0.1,
|
| 125 |
-
value=
|
| 126 |
)
|
| 127 |
|
| 128 |
num_inference_steps = gr.Slider(
|
| 129 |
label="Number of inference steps",
|
| 130 |
minimum=1,
|
| 131 |
-
maximum=
|
| 132 |
step=1,
|
| 133 |
-
value=
|
| 134 |
)
|
| 135 |
|
| 136 |
-
|
| 137 |
-
gr.on(
|
| 138 |
-
triggers=[run_button.click, prompt.submit],
|
| 139 |
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
+
import PIL.Image
|
| 5 |
+
from PIL import Image
|
| 6 |
import random
|
| 7 |
+
from diffusers import StableDiffusionXLPipeline
|
| 8 |
+
from diffusers import EulerAncestralDiscreteScheduler
|
|
|
|
| 9 |
import torch
|
| 10 |
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
|
| 13 |
+
# Make sure to use torch.float16 consistently throughout the pipeline
|
| 14 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 15 |
+
"votepurchase/pornmasterPro_noobV3VAE",
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
+
variant="fp16", # Explicitly use fp16 variant
|
| 18 |
+
use_safetensors=True # Use safetensors if available
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 22 |
+
pipe.to(device)
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Force all components to use the same dtype
|
| 25 |
+
pipe.text_encoder.to(torch.float16)
|
| 26 |
+
pipe.text_encoder_2.to(torch.float16)
|
| 27 |
+
pipe.vae.to(torch.float16)
|
| 28 |
+
pipe.unet.to(torch.float16)
|
| 29 |
|
| 30 |
MAX_SEED = np.iinfo(np.int32).max
|
| 31 |
+
MAX_IMAGE_SIZE = 1216
|
| 32 |
+
|
| 33 |
+
@spaces.GPU
|
| 34 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 35 |
+
# Check and truncate prompt if too long (CLIP can only handle 77 tokens)
|
| 36 |
+
if len(prompt.split()) > 60: # Rough estimate to avoid exceeding token limit
|
| 37 |
+
print("Warning: Prompt may be too long and will be truncated by the model")
|
| 38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
if randomize_seed:
|
| 40 |
seed = random.randint(0, MAX_SEED)
|
| 41 |
|
| 42 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
output_image = pipe(
|
| 46 |
+
prompt=prompt,
|
| 47 |
+
negative_prompt=negative_prompt,
|
| 48 |
+
guidance_scale=guidance_scale,
|
| 49 |
+
num_inference_steps=num_inference_steps,
|
| 50 |
+
width=width,
|
| 51 |
+
height=height,
|
| 52 |
+
generator=generator
|
| 53 |
+
).images[0]
|
| 54 |
+
|
| 55 |
+
return output_image
|
| 56 |
+
except RuntimeError as e:
|
| 57 |
+
print(f"Error during generation: {e}")
|
| 58 |
+
# Return a blank image with error message
|
| 59 |
+
error_img = Image.new('RGB', (width, height), color=(0, 0, 0))
|
| 60 |
+
return error_img
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
css = """
|
| 64 |
#col-container {
|
| 65 |
margin: 0 auto;
|
| 66 |
+
max-width: 520px;
|
| 67 |
}
|
| 68 |
"""
|
| 69 |
|
| 70 |
with gr.Blocks(css=css) as demo:
|
| 71 |
+
|
| 72 |
with gr.Column(elem_id="col-container"):
|
|
|
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
prompt = gr.Text(
|
| 76 |
label="Prompt",
|
| 77 |
show_label=False,
|
| 78 |
max_lines=1,
|
| 79 |
+
placeholder="Enter your prompt (keep it under 60 words for best results)",
|
| 80 |
container=False,
|
| 81 |
)
|
| 82 |
|
| 83 |
+
run_button = gr.Button("Run", scale=0)
|
| 84 |
|
| 85 |
result = gr.Image(label="Result", show_label=False)
|
| 86 |
+
|
| 87 |
with gr.Accordion("Advanced Settings", open=False):
|
| 88 |
+
|
| 89 |
negative_prompt = gr.Text(
|
| 90 |
label="Negative prompt",
|
| 91 |
max_lines=1,
|
| 92 |
placeholder="Enter a negative prompt",
|
| 93 |
+
value="nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn"
|
| 94 |
)
|
| 95 |
|
| 96 |
seed = gr.Slider(
|
|
|
|
| 109 |
minimum=256,
|
| 110 |
maximum=MAX_IMAGE_SIZE,
|
| 111 |
step=32,
|
| 112 |
+
value=1024,
|
| 113 |
)
|
| 114 |
|
| 115 |
height = gr.Slider(
|
|
|
|
| 117 |
minimum=256,
|
| 118 |
maximum=MAX_IMAGE_SIZE,
|
| 119 |
step=32,
|
| 120 |
+
value=1024,
|
| 121 |
)
|
| 122 |
|
| 123 |
with gr.Row():
|
| 124 |
guidance_scale = gr.Slider(
|
| 125 |
label="Guidance scale",
|
| 126 |
minimum=0.0,
|
| 127 |
+
maximum=20.0,
|
| 128 |
step=0.1,
|
| 129 |
+
value=7,
|
| 130 |
)
|
| 131 |
|
| 132 |
num_inference_steps = gr.Slider(
|
| 133 |
label="Number of inference steps",
|
| 134 |
minimum=1,
|
| 135 |
+
maximum=28,
|
| 136 |
step=1,
|
| 137 |
+
value=28,
|
| 138 |
)
|
| 139 |
|
| 140 |
+
run_button.click(
|
|
|
|
|
|
|
| 141 |
fn=infer,
|
| 142 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 143 |
+
outputs=[result]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
+
demo.queue().launch()
|
|
|