Update app.py
Browse files
app.py
CHANGED
|
@@ -2,70 +2,98 @@ import gradio as gr
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
os.environ["HF_HOME"] = "./cache"
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 12 |
-
|
| 13 |
-
torch_dtype=torch.
|
|
|
|
|
|
|
| 14 |
low_cpu_mem_usage=True,
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
).to("cpu")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
def generate_image(prompt,
|
| 24 |
-
"""Generate
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return images
|
| 36 |
except Exception as e:
|
| 37 |
-
return f"Error: {str(e)}
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column():
|
| 50 |
-
prompt = gr.Textbox(
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
with gr.Row():
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
with gr.Column():
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
generate_btn.click(
|
| 65 |
fn=generate_image,
|
| 66 |
-
inputs=[prompt,
|
| 67 |
-
outputs=
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
demo.
|
|
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import warnings
|
| 7 |
|
| 8 |
+
# Suppress warnings for cleaner output
|
| 9 |
+
warnings.filterwarnings("ignore")
|
|
|
|
| 10 |
|
| 11 |
+
# Model loading configuration
|
| 12 |
+
MODEL_NAME = "Heartsync/NSFW-Uncensored"
|
| 13 |
+
CACHE_DIR = "./model_cache"
|
| 14 |
+
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 15 |
+
|
| 16 |
+
# Load model with memory optimization
|
| 17 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
MODEL_NAME,
|
| 19 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 20 |
+
cache_dir=CACHE_DIR,
|
| 21 |
+
revision="fp16" if torch.cuda.is_available() else None,
|
| 22 |
low_cpu_mem_usage=True,
|
| 23 |
+
device_map="cpu"
|
| 24 |
+
)
|
|
|
|
| 25 |
|
| 26 |
+
# Safety disclaimer
|
| 27 |
+
DISCLAIMER = """
|
| 28 |
+
⚠️ **Content Warning**
|
| 29 |
+
This model may generate explicit/sensitive content. By using this application, you agree to comply with [HuggingFace's Acceptable Use Policy](https://huggingface.co/policies/acceptable-use ).
|
| 30 |
+
"""
|
| 31 |
|
| 32 |
+
def generate_image(prompt, width, height, num_inference_steps=30, guidance_scale=7.5):
|
| 33 |
+
"""Generate image with safety checks and memory management"""
|
| 34 |
+
if not prompt.strip():
|
| 35 |
+
return None, "Please enter a prompt"
|
| 36 |
+
|
| 37 |
try:
|
| 38 |
+
with torch.inference_mode():
|
| 39 |
+
result = pipe(
|
| 40 |
+
prompt=prompt,
|
| 41 |
+
width=width,
|
| 42 |
+
height=height,
|
| 43 |
+
num_inference_steps=num_inference_steps,
|
| 44 |
+
guidance_scale=guidance_scale
|
| 45 |
+
).images[0]
|
| 46 |
+
return result.convert("RGB"), None
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
+
return None, f"Error: {str(e)}"
|
| 49 |
|
| 50 |
+
# UI Configuration
|
| 51 |
+
ASPECT_RATIOS = {
|
| 52 |
+
"Square (512x512)": (512, 512),
|
| 53 |
+
"Portrait (512x768)": (512, 768),
|
| 54 |
+
"Landscape (768x512)": (768, 512),
|
| 55 |
+
"Phone (384x832)": (384, 832),
|
| 56 |
+
"Custom": None
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
with gr.Blocks(theme="soft", css=".disclaimer {color: #FF4B2B; font-size: 0.9em;}") as demo:
|
| 60 |
+
gr.Markdown("# 🔞 NSFW-Uncensored Text-to-Image Generator\n" + DISCLAIMER)
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
with gr.Column():
|
| 64 |
+
prompt = gr.Textbox(
|
| 65 |
+
label="Prompt",
|
| 66 |
+
placeholder="Describe your image...",
|
| 67 |
+
lines=3
|
| 68 |
+
)
|
| 69 |
with gr.Row():
|
| 70 |
+
width = gr.Number(value=512, label="Width", interactive=True)
|
| 71 |
+
height = gr.Number(value=512, label="Height", interactive=True)
|
| 72 |
+
aspect_ratio = gr.Dropdown(
|
| 73 |
+
choices=list(ASPECT_RATIOS.keys()),
|
| 74 |
+
value="Square (512x512)",
|
| 75 |
+
label="Aspect Ratio"
|
| 76 |
+
)
|
| 77 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 78 |
+
|
| 79 |
with gr.Column():
|
| 80 |
+
output_image = gr.Image(label="Result", interactive=False)
|
| 81 |
+
error_msg = gr.Textbox(label="Status", visible=True)
|
| 82 |
+
|
| 83 |
+
def update_dimensions(choice):
|
| 84 |
+
if choice == "Custom":
|
| 85 |
+
return [gr.Number(interactive=True), gr.Number(interactive=True)]
|
| 86 |
+
else:
|
| 87 |
+
w, h = ASPECT_RATIOS[choice]
|
| 88 |
+
return [gr.Number(value=w, interactive=True), gr.Number(value=h, interactive=True)]
|
| 89 |
|
| 90 |
+
aspect_ratio.change(fn=update_dimensions, inputs=[aspect_ratio], outputs=[width, height])
|
| 91 |
|
| 92 |
generate_btn.click(
|
| 93 |
fn=generate_image,
|
| 94 |
+
inputs=[prompt, width, height],
|
| 95 |
+
outputs=[output_image, error_msg]
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
+
demo.launch()
|