Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import random
|
|
| 4 |
import spaces # [uncomment to use ZeroGPU]
|
| 5 |
from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
|
| 6 |
import torch
|
|
|
|
| 7 |
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl" # Replace to the model you would like to use
|
|
@@ -14,34 +15,14 @@ else:
|
|
| 14 |
torch_dtype = torch.float32
|
| 15 |
|
| 16 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 17 |
-
# pipe.scheduler = DPMSolverSDEScheduler.from_config(pipe.scheduler.config, algorithm_type="dpmsolver++", solver_order=2, use_karras_sigmas=True)
|
| 18 |
pipe = pipe.to(device)
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
| 23 |
-
# First set of tags
|
| 24 |
-
tag_options_1 = {
|
| 25 |
-
"Fantasy": "fantasy",
|
| 26 |
-
"Sci-Fi": "sci-fi",
|
| 27 |
-
"Realistic": "realistic",
|
| 28 |
-
"Cyberpunk": "cyberpunk",
|
| 29 |
-
"Noir": "noir",
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
# Second set of tags
|
| 33 |
-
tag_options_2 = {
|
| 34 |
-
"Vibrant Colors": "vibrant colors",
|
| 35 |
-
"Dark Theme": "dark theme",
|
| 36 |
-
"Minimalist": "minimalist",
|
| 37 |
-
"Photorealistic": "photorealistic",
|
| 38 |
-
"Abstract": "abstract",
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
@spaces.GPU # [uncomment to use ZeroGPU]
|
| 42 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, tag_selection_1, tag_selection_2, use_tags, progress=gr.Progress(track_tqdm=True)):
|
| 43 |
|
| 44 |
-
# Determine prompt based on the tab selection
|
| 45 |
if use_tags:
|
| 46 |
selected_tags_1 = [tag_options_1[tag] for tag in tag_selection_1 if tag in tag_options_1]
|
| 47 |
selected_tags_2 = [tag_options_2[tag] for tag in tag_selection_2 if tag in tag_options_2]
|
|
@@ -87,9 +68,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 87 |
# Text-to-Image Gradio Template
|
| 88 |
""")
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
# Tabbed interface to select either Prompt or Tags
|
| 91 |
with gr.Tabs() as tabs:
|
| 92 |
-
# Prompt-only tab
|
| 93 |
with gr.TabItem("Prompt Input"):
|
| 94 |
prompt = gr.Text(
|
| 95 |
label="Prompt",
|
|
@@ -98,17 +81,18 @@ with gr.Blocks(css=css) as demo:
|
|
| 98 |
placeholder="Enter your prompt",
|
| 99 |
container=False,
|
| 100 |
)
|
| 101 |
-
use_tags = gr.State(False)
|
| 102 |
|
| 103 |
-
# Tag-based input tab
|
| 104 |
with gr.TabItem("Tag Selection"):
|
| 105 |
-
|
|
|
|
| 106 |
tag_selection_1 = gr.CheckboxGroup(choices=list(tag_options_1.keys()), label="Select Tags (Style)")
|
|
|
|
| 107 |
tag_selection_2 = gr.CheckboxGroup(choices=list(tag_options_2.keys()), label="Select Tags (Theme)")
|
| 108 |
-
use_tags = gr.State(True)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
|
| 113 |
with gr.Accordion("Advanced Settings", open=False):
|
| 114 |
negative_prompt = gr.Text(
|
|
@@ -134,7 +118,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 134 |
minimum=256,
|
| 135 |
maximum=MAX_IMAGE_SIZE,
|
| 136 |
step=32,
|
| 137 |
-
value=1024,
|
| 138 |
)
|
| 139 |
|
| 140 |
height = gr.Slider(
|
|
@@ -142,7 +126,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 142 |
minimum=256,
|
| 143 |
maximum=MAX_IMAGE_SIZE,
|
| 144 |
step=32,
|
| 145 |
-
value=1024,
|
| 146 |
)
|
| 147 |
|
| 148 |
with gr.Row():
|
|
@@ -151,7 +135,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 151 |
minimum=0.0,
|
| 152 |
maximum=10.0,
|
| 153 |
step=0.1,
|
| 154 |
-
value=7,
|
| 155 |
)
|
| 156 |
|
| 157 |
num_inference_steps = gr.Slider(
|
|
@@ -159,7 +143,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 159 |
minimum=1,
|
| 160 |
maximum=50,
|
| 161 |
step=1,
|
| 162 |
-
value=35,
|
| 163 |
)
|
| 164 |
|
| 165 |
gr.Examples(
|
|
@@ -167,13 +151,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 167 |
inputs=[prompt]
|
| 168 |
)
|
| 169 |
|
| 170 |
-
# Use `run_button.click` event to check which tab is selected
|
| 171 |
def check_tab(prompt, tag_selection_1, tag_selection_2, selected_tab):
|
| 172 |
-
|
| 173 |
-
return False # Use prompt only
|
| 174 |
-
return True # Use tags if on "Tag Selection"
|
| 175 |
|
| 176 |
-
# Set the selected tab
|
| 177 |
tabs.change(check_tab, inputs=[prompt, tag_selection_1, tag_selection_2, tabs], outputs=use_tags)
|
| 178 |
|
| 179 |
gr.on(
|
|
|
|
| 4 |
import spaces # [uncomment to use ZeroGPU]
|
| 5 |
from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
|
| 6 |
import torch
|
| 7 |
+
from tags import tag_options_1, tag_options_2 # Import tags here
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl" # Replace to the model you would like to use
|
|
|
|
| 15 |
torch_dtype = torch.float32
|
| 16 |
|
| 17 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
|
|
|
| 18 |
pipe = pipe.to(device)
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@spaces.GPU # [uncomment to use ZeroGPU]
|
| 24 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, tag_selection_1, tag_selection_2, use_tags, progress=gr.Progress(track_tqdm=True)):
|
| 25 |
|
|
|
|
| 26 |
if use_tags:
|
| 27 |
selected_tags_1 = [tag_options_1[tag] for tag in tag_selection_1 if tag in tag_options_1]
|
| 28 |
selected_tags_2 = [tag_options_2[tag] for tag in tag_selection_2 if tag in tag_options_2]
|
|
|
|
| 68 |
# Text-to-Image Gradio Template
|
| 69 |
""")
|
| 70 |
|
| 71 |
+
# Display result image at the top
|
| 72 |
+
result = gr.Image(label="Result", show_label=False)
|
| 73 |
+
|
| 74 |
# Tabbed interface to select either Prompt or Tags
|
| 75 |
with gr.Tabs() as tabs:
|
|
|
|
| 76 |
with gr.TabItem("Prompt Input"):
|
| 77 |
prompt = gr.Text(
|
| 78 |
label="Prompt",
|
|
|
|
| 81 |
placeholder="Enter your prompt",
|
| 82 |
container=False,
|
| 83 |
)
|
| 84 |
+
use_tags = gr.State(False)
|
| 85 |
|
|
|
|
| 86 |
with gr.TabItem("Tag Selection"):
|
| 87 |
+
# Separate each tag section vertically
|
| 88 |
+
with gr.Column():
|
| 89 |
tag_selection_1 = gr.CheckboxGroup(choices=list(tag_options_1.keys()), label="Select Tags (Style)")
|
| 90 |
+
with gr.Column():
|
| 91 |
tag_selection_2 = gr.CheckboxGroup(choices=list(tag_options_2.keys()), label="Select Tags (Theme)")
|
| 92 |
+
use_tags = gr.State(True)
|
| 93 |
|
| 94 |
+
# Full-width "Run" button
|
| 95 |
+
run_button = gr.Button("Run", scale=0, full_width=True)
|
| 96 |
|
| 97 |
with gr.Accordion("Advanced Settings", open=False):
|
| 98 |
negative_prompt = gr.Text(
|
|
|
|
| 118 |
minimum=256,
|
| 119 |
maximum=MAX_IMAGE_SIZE,
|
| 120 |
step=32,
|
| 121 |
+
value=1024,
|
| 122 |
)
|
| 123 |
|
| 124 |
height = gr.Slider(
|
|
|
|
| 126 |
minimum=256,
|
| 127 |
maximum=MAX_IMAGE_SIZE,
|
| 128 |
step=32,
|
| 129 |
+
value=1024,
|
| 130 |
)
|
| 131 |
|
| 132 |
with gr.Row():
|
|
|
|
| 135 |
minimum=0.0,
|
| 136 |
maximum=10.0,
|
| 137 |
step=0.1,
|
| 138 |
+
value=7,
|
| 139 |
)
|
| 140 |
|
| 141 |
num_inference_steps = gr.Slider(
|
|
|
|
| 143 |
minimum=1,
|
| 144 |
maximum=50,
|
| 145 |
step=1,
|
| 146 |
+
value=35,
|
| 147 |
)
|
| 148 |
|
| 149 |
gr.Examples(
|
|
|
|
| 151 |
inputs=[prompt]
|
| 152 |
)
|
| 153 |
|
|
|
|
| 154 |
def check_tab(prompt, tag_selection_1, tag_selection_2, selected_tab):
|
| 155 |
+
return selected_tab == "Tag Selection"
|
|
|
|
|
|
|
| 156 |
|
|
|
|
| 157 |
tabs.change(check_tab, inputs=[prompt, tag_selection_1, tag_selection_2, tabs], outputs=use_tags)
|
| 158 |
|
| 159 |
gr.on(
|