Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,194 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
torch_dtype=torch.float16
|
| 10 |
-
).to(device)
|
| 11 |
-
|
| 12 |
-
def generate_image(prompt):
|
| 13 |
-
# Menghasilkan gambar dari teks
|
| 14 |
-
image = pipeline(prompt).images[0]
|
| 15 |
-
return image
|
| 16 |
-
|
| 17 |
-
# Membuat Antarmuka Gradio
|
| 18 |
-
demo = gr.Interface(
|
| 19 |
-
fn=generate_image,
|
| 20 |
-
inputs=gr.Textbox(label="Input your prompt"),
|
| 21 |
-
outputs=gr.Image(label="Image Result"),
|
| 22 |
-
title="AI Text-to-Image Generator",
|
| 23 |
-
description="Input Text via Stable Diffusion."
|
| 24 |
)
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 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.1')
|
| 10 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 11 |
+
model_name,
|
| 12 |
+
torch_dtype=torch.float16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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],
|
| 20 |
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
| 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 19 aged rusian girl in a summer dress at the beach, golden sunset, professional photography, 8k", default_negative_prompt, 40, 7.5, 1024, 1024, 4],
|
| 30 |
+
["a beautiful japan woman in a vikini at the beach, noon sunset, professional photography, 8k", default_negative_prompt, 45, 7.0, 1024, 1024, 4],
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# Image generation function
|
| 34 |
+
@spaces.GPU(duration=120)
|
| 35 |
+
def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples, progress=gr.Progress()):
|
| 36 |
+
progress(0, desc="Preparing")
|
| 37 |
+
embeds, pooled = compel(prompt)
|
| 38 |
+
neg_embeds, neg_pooled = compel(negative_prompt)
|
| 39 |
+
|
| 40 |
+
progress(0.1, desc="Generating images")
|
| 41 |
+
|
| 42 |
+
# Define proper callback for step end
|
| 43 |
+
def callback_on_step_end(pipe, i, t, callback_kwargs):
|
| 44 |
+
progress((i + 1) / num_inference_steps)
|
| 45 |
+
return callback_kwargs
|
| 46 |
+
|
| 47 |
+
images = pipe(
|
| 48 |
+
prompt_embeds=embeds,
|
| 49 |
+
pooled_prompt_embeds=pooled,
|
| 50 |
+
negative_prompt_embeds=neg_embeds,
|
| 51 |
+
negative_pooled_prompt_embeds=neg_pooled,
|
| 52 |
+
num_inference_steps=num_inference_steps,
|
| 53 |
+
guidance_scale=guidance_scale,
|
| 54 |
+
width=width,
|
| 55 |
+
height=height,
|
| 56 |
+
num_images_per_prompt=num_samples,
|
| 57 |
+
callback_on_step_end=callback_on_step_end
|
| 58 |
+
).images
|
| 59 |
+
|
| 60 |
+
return images
|
| 61 |
+
|
| 62 |
+
# CSS styles
|
| 63 |
+
css = """
|
| 64 |
+
.gallery-item {
|
| 65 |
+
transition: transform 0.2s;
|
| 66 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
| 67 |
+
border-radius: 10px;
|
| 68 |
+
}
|
| 69 |
+
.gallery-item:hover {
|
| 70 |
+
transform: scale(1.03);
|
| 71 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
|
| 72 |
+
}
|
| 73 |
+
.container {
|
| 74 |
+
max-width: 1200px;
|
| 75 |
+
margin: auto;
|
| 76 |
+
}
|
| 77 |
+
.header {
|
| 78 |
+
text-align: center;
|
| 79 |
+
margin-bottom: 2rem;
|
| 80 |
+
padding: 1rem;
|
| 81 |
+
background: linear-gradient(90deg, rgba(76,0,161,0.8) 0%, rgba(28,110,164,0.8) 100%);
|
| 82 |
+
border-radius: 10px;
|
| 83 |
+
color: white;
|
| 84 |
+
}
|
| 85 |
+
.slider-container {
|
| 86 |
+
background-color: #f5f5f5;
|
| 87 |
+
padding: 1rem;
|
| 88 |
+
border-radius: 10px;
|
| 89 |
+
margin-bottom: 1rem;
|
| 90 |
+
}
|
| 91 |
+
.prompt-container {
|
| 92 |
+
background-color: #f0f8ff;
|
| 93 |
+
padding: 1rem;
|
| 94 |
+
border-radius: 10px;
|
| 95 |
+
margin-bottom: 1rem;
|
| 96 |
+
border: 1px solid #d0e8ff;
|
| 97 |
+
}
|
| 98 |
+
.examples-header {
|
| 99 |
+
background: linear-gradient(90deg, rgba(41,128,185,0.7) 0%, rgba(142,68,173,0.7) 100%);
|
| 100 |
+
color: white;
|
| 101 |
+
padding: 0.5rem;
|
| 102 |
+
border-radius: 8px;
|
| 103 |
+
text-align: center;
|
| 104 |
+
margin-bottom: 0.5rem;
|
| 105 |
+
}
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
# Gradio interface
|
| 109 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 110 |
+
gr.HTML("""
|
| 111 |
+
<style>
|
| 112 |
+
.gradio-container {
|
| 113 |
+
background: linear-gradient(135deg, #fef9f3 0%, #f0e6fa 50%, #e6f0fa 100%) !important;
|
| 114 |
+
}
|
| 115 |
+
footer {display: none !important;}
|
| 116 |
+
</style>
|
| 117 |
+
|
| 118 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 119 |
+
<h1 style="color: #6b5b7a; font-size: 2.2rem; font-weight: 700; margin-bottom: 0.3rem;">
|
| 120 |
+
🎨 Unfiltered AI NSFW Image Generator
|
| 121 |
+
</h1>
|
| 122 |
+
|
| 123 |
+
<p style="color: #8b7b9b; font-size: 1rem;">
|
| 124 |
+
Enter creative prompts and generate high-quality images.
|
| 125 |
+
</p>
|
| 126 |
+
|
| 127 |
+
<div style="margin-top: 12px; display: flex; justify-content: center; gap: 12px;">
|
| 128 |
+
<a href="https://huggingface.co/spaces/Heartsync/FREE-NSFW-HUB" target="_blank">
|
| 129 |
+
<img src="https://img.shields.io/static/v1?label=FREE&message=NSFW%20HUB&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=white&style=for-the-badge" alt="badge">
|
| 130 |
+
</a>
|
| 131 |
+
<a href="https://www.humangen.ai" target="_blank">
|
| 132 |
+
<img src="https://img.shields.io/static/v1?label=100% FREE&message=AI%20Playground&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
|
| 133 |
+
</a>
|
| 134 |
+
<a href="https://ginigen.ai/en" target="_blank">
|
| 135 |
+
<img src="https://img.shields.io/static/v1?label=Powered%20by&message=Hogwarts%20BANANA&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=white&style=for-the-badge" alt="badge">
|
| 136 |
+
</a>
|
| 137 |
+
</div>
|
| 138 |
+
</div>
|
| 139 |
+
""")
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
with gr.Column(scale=2):
|
| 145 |
+
with gr.Group(elem_classes="prompt-container"):
|
| 146 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe your desired image...", lines=3)
|
| 147 |
+
negative_prompt = gr.Textbox(
|
| 148 |
+
label="Negative Prompt",
|
| 149 |
+
value=default_negative_prompt,
|
| 150 |
+
lines=3
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
with gr.Group(elem_classes="slider-container"):
|
| 154 |
+
with gr.Row():
|
| 155 |
+
with gr.Column():
|
| 156 |
+
steps = gr.Slider(minimum=20, maximum=100, value=60, step=1, label="Inference Steps (Quality)", info="Higher values improve quality (longer generation time)")
|
| 157 |
+
guidance = gr.Slider(minimum=1, maximum=15, value=7, step=0.1, label="Guidance Scale (Creativity)", info="Lower values create more creative results")
|
| 158 |
+
|
| 159 |
+
with gr.Column():
|
| 160 |
+
with gr.Row():
|
| 161 |
+
width = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="Width")
|
| 162 |
+
height = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="Height")
|
| 163 |
+
|
| 164 |
+
num_samples = gr.Slider(minimum=1, maximum=8, value=4, step=1, label="Number of Images", info="Number of images to generate at once")
|
| 165 |
+
|
| 166 |
+
generate_btn = gr.Button("🚀 Generate Images", variant="primary", size="lg")
|
| 167 |
+
|
| 168 |
+
with gr.Column(scale=3):
|
| 169 |
+
output_gallery = gr.Gallery(label="Generated Images", elem_classes="gallery-item", columns=2, object_fit="contain", height=650)
|
| 170 |
+
|
| 171 |
+
gr.HTML("""<div class="examples-header"><h3>✨ Example Prompts</h3></div>""")
|
| 172 |
+
gr.Examples(
|
| 173 |
+
examples=example_prompts,
|
| 174 |
+
inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
|
| 175 |
+
outputs=output_gallery,
|
| 176 |
+
fn=generate,
|
| 177 |
+
cache_examples=True,
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Event connections
|
| 181 |
+
generate_btn.click(
|
| 182 |
+
fn=generate,
|
| 183 |
+
inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
|
| 184 |
+
outputs=output_gallery
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
gr.HTML("""
|
| 188 |
+
<div style="text-align: center; margin-top: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 10px;">
|
| 189 |
+
<p>💡 Tip: For high-quality images, use detailed prompts and higher inference steps.</p>
|
| 190 |
+
<p>Example: Add quality terms like "professional photography, 8k, highly detailed, sharp focus, HDR" to your prompts.</p>
|
| 191 |
+
</div>
|
| 192 |
+
""")
|
| 193 |
+
|
| 194 |
+
demo.launch()
|