Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import os
|
|
5 |
from compel import Compel, ReturnedEmbeddingsType
|
6 |
from diffusers import DiffusionPipeline
|
7 |
|
8 |
-
#
|
9 |
model_name = os.environ.get('MODEL_NAME', 'UnfilteredAI/NSFW-gen-v2')
|
10 |
pipe = DiffusionPipeline.from_pretrained(
|
11 |
model_name,
|
@@ -13,7 +13,7 @@ pipe = DiffusionPipeline.from_pretrained(
|
|
13 |
)
|
14 |
pipe.to('cuda')
|
15 |
|
16 |
-
# Compel
|
17 |
compel = Compel(
|
18 |
tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
|
19 |
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
@@ -21,10 +21,10 @@ compel = Compel(
|
|
21 |
requires_pooled=[False, True]
|
22 |
)
|
23 |
|
24 |
-
#
|
25 |
default_negative_prompt = "(low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn, (deformed | distorted | disfigured:1.3), bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers:1.4, disconnected limbs, blurry, amputation."
|
26 |
|
27 |
-
#
|
28 |
example_prompts = [
|
29 |
["a beautiful woman in a summer dress at the beach, golden sunset, professional photography, 8k", default_negative_prompt, 40, 7.5, 1024, 1024, 4],
|
30 |
["portrait of a cyberpunk character, highly detailed, neon lights, futuristic cityscape in background, 8k, ultra realistic", default_negative_prompt, 50, 8.0, 768, 1024, 4],
|
@@ -32,7 +32,7 @@ example_prompts = [
|
|
32 |
["photorealistic portrait of a stunning model, studio lighting, fashion photography", default_negative_prompt, 45, 7.0, 1024, 1024, 4],
|
33 |
]
|
34 |
|
35 |
-
#
|
36 |
@spaces.GPU(duration=120)
|
37 |
def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples, progress=gr.Progress()):
|
38 |
progress(0, desc="Preparing")
|
@@ -50,12 +50,13 @@ def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width
|
|
50 |
width=width,
|
51 |
height=height,
|
52 |
num_images_per_prompt=num_samples,
|
53 |
-
|
|
|
54 |
).images
|
55 |
|
56 |
return images
|
57 |
|
58 |
-
# CSS
|
59 |
css = """
|
60 |
.gallery-item {
|
61 |
transition: transform 0.2s;
|
@@ -101,21 +102,21 @@ css = """
|
|
101 |
}
|
102 |
"""
|
103 |
|
104 |
-
# Gradio
|
105 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
106 |
gr.HTML("""
|
107 |
<div class="header">
|
108 |
-
<h1>🎨 UnfilteredAI NSFW-gen-v2
|
109 |
-
<p
|
110 |
</div>
|
111 |
""")
|
112 |
|
113 |
with gr.Row():
|
114 |
with gr.Column(scale=2):
|
115 |
with gr.Group(elem_classes="prompt-container"):
|
116 |
-
prompt = gr.Textbox(label="
|
117 |
negative_prompt = gr.Textbox(
|
118 |
-
label="
|
119 |
value=default_negative_prompt,
|
120 |
lines=3
|
121 |
)
|
@@ -123,22 +124,22 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
123 |
with gr.Group(elem_classes="slider-container"):
|
124 |
with gr.Row():
|
125 |
with gr.Column():
|
126 |
-
steps = gr.Slider(minimum=20, maximum=100, value=60, step=1, label="
|
127 |
-
guidance = gr.Slider(minimum=1, maximum=15, value=7, step=0.1, label="
|
128 |
|
129 |
with gr.Column():
|
130 |
with gr.Row():
|
131 |
-
width = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="
|
132 |
-
height = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="
|
133 |
|
134 |
-
num_samples = gr.Slider(minimum=1, maximum=8, value=4, step=1, label="
|
135 |
|
136 |
-
generate_btn = gr.Button("🚀
|
137 |
|
138 |
with gr.Column(scale=3):
|
139 |
-
output_gallery = gr.Gallery(label="
|
140 |
|
141 |
-
gr.HTML("""<div class="examples-header"><h3>✨
|
142 |
gr.Examples(
|
143 |
examples=example_prompts,
|
144 |
inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
|
@@ -147,7 +148,7 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
147 |
cache_examples=True,
|
148 |
)
|
149 |
|
150 |
-
#
|
151 |
generate_btn.click(
|
152 |
fn=generate,
|
153 |
inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
|
@@ -156,8 +157,8 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
156 |
|
157 |
gr.HTML("""
|
158 |
<div style="text-align: center; margin-top: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 10px;">
|
159 |
-
<p>💡
|
160 |
-
<p
|
161 |
</div>
|
162 |
""")
|
163 |
|
|
|
5 |
from compel import Compel, ReturnedEmbeddingsType
|
6 |
from diffusers import DiffusionPipeline
|
7 |
|
8 |
+
# Load model
|
9 |
model_name = os.environ.get('MODEL_NAME', 'UnfilteredAI/NSFW-gen-v2')
|
10 |
pipe = DiffusionPipeline.from_pretrained(
|
11 |
model_name,
|
|
|
13 |
)
|
14 |
pipe.to('cuda')
|
15 |
|
16 |
+
# Compel setup
|
17 |
compel = Compel(
|
18 |
tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
|
19 |
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
|
|
21 |
requires_pooled=[False, True]
|
22 |
)
|
23 |
|
24 |
+
# Default negative prompt
|
25 |
default_negative_prompt = "(low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn, (deformed | distorted | disfigured:1.3), bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers:1.4, disconnected limbs, blurry, amputation."
|
26 |
|
27 |
+
# Example prompts
|
28 |
example_prompts = [
|
29 |
["a beautiful woman in a summer dress at the beach, golden sunset, professional photography, 8k", default_negative_prompt, 40, 7.5, 1024, 1024, 4],
|
30 |
["portrait of a cyberpunk character, highly detailed, neon lights, futuristic cityscape in background, 8k, ultra realistic", default_negative_prompt, 50, 8.0, 768, 1024, 4],
|
|
|
32 |
["photorealistic portrait of a stunning model, studio lighting, fashion photography", default_negative_prompt, 45, 7.0, 1024, 1024, 4],
|
33 |
]
|
34 |
|
35 |
+
# Image generation function
|
36 |
@spaces.GPU(duration=120)
|
37 |
def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples, progress=gr.Progress()):
|
38 |
progress(0, desc="Preparing")
|
|
|
50 |
width=width,
|
51 |
height=height,
|
52 |
num_images_per_prompt=num_samples,
|
53 |
+
callback_steps=1, # Fixed: Added callback_steps parameter
|
54 |
+
callback_on_step_end=lambda i, t, latents: progress((i + 1) / num_inference_steps)
|
55 |
).images
|
56 |
|
57 |
return images
|
58 |
|
59 |
+
# CSS styles
|
60 |
css = """
|
61 |
.gallery-item {
|
62 |
transition: transform 0.2s;
|
|
|
102 |
}
|
103 |
"""
|
104 |
|
105 |
+
# Gradio interface
|
106 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
107 |
gr.HTML("""
|
108 |
<div class="header">
|
109 |
+
<h1>🎨 UnfilteredAI NSFW-gen-v2 Image Generator</h1>
|
110 |
+
<p>Enter creative prompts and generate high-quality images.</p>
|
111 |
</div>
|
112 |
""")
|
113 |
|
114 |
with gr.Row():
|
115 |
with gr.Column(scale=2):
|
116 |
with gr.Group(elem_classes="prompt-container"):
|
117 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe your desired image...", lines=3)
|
118 |
negative_prompt = gr.Textbox(
|
119 |
+
label="Negative Prompt",
|
120 |
value=default_negative_prompt,
|
121 |
lines=3
|
122 |
)
|
|
|
124 |
with gr.Group(elem_classes="slider-container"):
|
125 |
with gr.Row():
|
126 |
with gr.Column():
|
127 |
+
steps = gr.Slider(minimum=20, maximum=100, value=60, step=1, label="Inference Steps (Quality)", info="Higher values improve quality (longer generation time)")
|
128 |
+
guidance = gr.Slider(minimum=1, maximum=15, value=7, step=0.1, label="Guidance Scale (Creativity)", info="Lower values create more creative results")
|
129 |
|
130 |
with gr.Column():
|
131 |
with gr.Row():
|
132 |
+
width = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="Width")
|
133 |
+
height = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="Height")
|
134 |
|
135 |
+
num_samples = gr.Slider(minimum=1, maximum=8, value=4, step=1, label="Number of Images", info="Number of images to generate at once")
|
136 |
|
137 |
+
generate_btn = gr.Button("🚀 Generate Images", variant="primary", size="lg")
|
138 |
|
139 |
with gr.Column(scale=3):
|
140 |
+
output_gallery = gr.Gallery(label="Generated Images", elem_classes="gallery-item", columns=2, object_fit="contain", height=650)
|
141 |
|
142 |
+
gr.HTML("""<div class="examples-header"><h3>✨ Example Prompts</h3></div>""")
|
143 |
gr.Examples(
|
144 |
examples=example_prompts,
|
145 |
inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
|
|
|
148 |
cache_examples=True,
|
149 |
)
|
150 |
|
151 |
+
# Event connections
|
152 |
generate_btn.click(
|
153 |
fn=generate,
|
154 |
inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
|
|
|
157 |
|
158 |
gr.HTML("""
|
159 |
<div style="text-align: center; margin-top: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 10px;">
|
160 |
+
<p>💡 Tip: For high-quality images, use detailed prompts and higher inference steps.</p>
|
161 |
+
<p>Example: Add quality terms like "professional photography, 8k, highly detailed, sharp focus, HDR" to your prompts.</p>
|
162 |
</div>
|
163 |
""")
|
164 |
|