Update app.py
Browse files
app.py
CHANGED
|
@@ -11,13 +11,13 @@ MODEL_NAME = "Heartsync/NSFW-Uncensored"
|
|
| 11 |
CACHE_DIR = "./model_cache"
|
| 12 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 16 |
MODEL_NAME,
|
| 17 |
-
torch_dtype=torch.float32,
|
| 18 |
cache_dir=CACHE_DIR,
|
| 19 |
low_cpu_mem_usage=True,
|
| 20 |
-
safety_checker=None
|
| 21 |
)
|
| 22 |
|
| 23 |
DISCLAIMER = """
|
|
@@ -26,10 +26,19 @@ This model may generate explicit/sensitive content. By using this application, y
|
|
| 26 |
"""
|
| 27 |
|
| 28 |
def generate_image(prompt, width, height, num_inference_steps=20, guidance_scale=7.0):
|
|
|
|
| 29 |
if not prompt.strip():
|
| 30 |
-
return None, "
|
| 31 |
|
| 32 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
with torch.inference_mode():
|
| 34 |
result = pipe(
|
| 35 |
prompt=prompt,
|
|
@@ -38,10 +47,16 @@ def generate_image(prompt, width, height, num_inference_steps=20, guidance_scale
|
|
| 38 |
num_inference_steps=num_inference_steps,
|
| 39 |
guidance_scale=guidance_scale
|
| 40 |
).images[0]
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
return None, f"Error: {str(e)}"
|
| 44 |
|
|
|
|
| 45 |
ASPECT_RATIOS = {
|
| 46 |
"Square (512x512)": (512, 512),
|
| 47 |
"Portrait (512x768)": (512, 768),
|
|
@@ -55,7 +70,11 @@ with gr.Blocks(theme="soft", css=".disclaimer {color: #FF4B2B; font-size: 0.9em;
|
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column():
|
| 58 |
-
prompt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
width = gr.Number(value=512, label="Width", interactive=True)
|
| 61 |
height = gr.Number(value=512, label="Height", interactive=True)
|
|
@@ -68,11 +87,11 @@ with gr.Blocks(theme="soft", css=".disclaimer {color: #FF4B2B; font-size: 0.9em;
|
|
| 68 |
|
| 69 |
with gr.Column():
|
| 70 |
output_image = gr.Image(label="Result", interactive=False)
|
| 71 |
-
error_msg = gr.Textbox(label="Status",
|
| 72 |
|
| 73 |
def update_dimensions(choice):
|
| 74 |
if choice == "Custom":
|
| 75 |
-
return [gr.Number(interactive=True), gr.Number(interactive=True)]
|
| 76 |
else:
|
| 77 |
w, h = ASPECT_RATIOS[choice]
|
| 78 |
return [gr.Number(value=w, interactive=True), gr.Number(value=h, interactive=True)]
|
|
|
|
| 11 |
CACHE_DIR = "./model_cache"
|
| 12 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
+
# Load model with strict CPU settings
|
| 15 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 16 |
MODEL_NAME,
|
| 17 |
+
torch_dtype=torch.float32,
|
| 18 |
cache_dir=CACHE_DIR,
|
| 19 |
low_cpu_mem_usage=True,
|
| 20 |
+
safety_checker=None
|
| 21 |
)
|
| 22 |
|
| 23 |
DISCLAIMER = """
|
|
|
|
| 26 |
"""
|
| 27 |
|
| 28 |
def generate_image(prompt, width, height, num_inference_steps=20, guidance_scale=7.0):
|
| 29 |
+
"""Generate image with strict validation"""
|
| 30 |
if not prompt.strip():
|
| 31 |
+
return None, "Error: Prompt cannot be empty"
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
# Force integer dimensions
|
| 35 |
+
width = int(width) if width else 512
|
| 36 |
+
height = int(height) if height else 512
|
| 37 |
+
|
| 38 |
+
# Validate dimensions (must be divisible by 8)
|
| 39 |
+
width = width - (width % 8)
|
| 40 |
+
height = height - (height % 8)
|
| 41 |
+
|
| 42 |
with torch.inference_mode():
|
| 43 |
result = pipe(
|
| 44 |
prompt=prompt,
|
|
|
|
| 47 |
num_inference_steps=num_inference_steps,
|
| 48 |
guidance_scale=guidance_scale
|
| 49 |
).images[0]
|
| 50 |
+
|
| 51 |
+
if result is None:
|
| 52 |
+
return None, "Error: Failed to generate image"
|
| 53 |
+
|
| 54 |
+
return result.convert("RGB"), f"Success ({width}x{height})"
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
return None, f"Generation Error: {str(e)}"
|
| 58 |
|
| 59 |
+
# UI Configuration
|
| 60 |
ASPECT_RATIOS = {
|
| 61 |
"Square (512x512)": (512, 512),
|
| 62 |
"Portrait (512x768)": (512, 768),
|
|
|
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column():
|
| 73 |
+
prompt = gr.Textbox(
|
| 74 |
+
label="Prompt",
|
| 75 |
+
placeholder="Describe your image...",
|
| 76 |
+
lines=3
|
| 77 |
+
)
|
| 78 |
with gr.Row():
|
| 79 |
width = gr.Number(value=512, label="Width", interactive=True)
|
| 80 |
height = gr.Number(value=512, label="Height", interactive=True)
|
|
|
|
| 87 |
|
| 88 |
with gr.Column():
|
| 89 |
output_image = gr.Image(label="Result", interactive=False)
|
| 90 |
+
error_msg = gr.Textbox(label="Status", value="Ready", interactive=False)
|
| 91 |
|
| 92 |
def update_dimensions(choice):
|
| 93 |
if choice == "Custom":
|
| 94 |
+
return [gr.Number(value=512, interactive=True), gr.Number(value=512, interactive=True)]
|
| 95 |
else:
|
| 96 |
w, h = ASPECT_RATIOS[choice]
|
| 97 |
return [gr.Number(value=w, interactive=True), gr.Number(value=h, interactive=True)]
|