Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,10 +9,11 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 9 |
|
| 10 |
try:
|
| 11 |
print(f"Loading model on device: {device}")
|
| 12 |
-
|
| 13 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 14 |
MODEL_ID,
|
| 15 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
|
|
|
| 16 |
)
|
| 17 |
pipe = pipe.to(device)
|
| 18 |
print("Model loaded successfully.")
|
|
@@ -35,9 +36,7 @@ def generate_avatar(
|
|
| 35 |
The 'style_strength' controls how much the new image deviates from the original.
|
| 36 |
"""
|
| 37 |
if pipe is None:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if not prompt:
|
| 41 |
return initial_image, "Please enter a descriptive text prompt."
|
| 42 |
|
| 43 |
if initial_image is None:
|
|
@@ -46,7 +45,7 @@ def generate_avatar(
|
|
| 46 |
print(f"Generating image with prompt: '{prompt}' and strength: {style_strength}")
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
generated_image = pipe(
|
| 51 |
prompt=prompt,
|
| 52 |
image=initial_image.convert("RGB"), # Ensure image is in RGB format
|
|
@@ -62,6 +61,8 @@ def generate_avatar(
|
|
| 62 |
print(f"Inference error: {e}")
|
| 63 |
return initial_image, f"An error occurred during generation: {e}"
|
| 64 |
|
|
|
|
|
|
|
| 65 |
image_input = gr.Image(type="pil", label="1. Upload Base Image (Your Photo or Style Reference)")
|
| 66 |
prompt_input = gr.Textbox(
|
| 67 |
label="2. Creative Prompt (e.g., 'A professional LinkedIn headshot, cyberpunk aesthetic, highly detailed')",
|
|
@@ -88,7 +89,7 @@ negative_prompt_input = gr.Textbox(
|
|
| 88 |
gr.Interface(
|
| 89 |
fn=generate_avatar,
|
| 90 |
inputs=[image_input, prompt_input, strength_slider, steps_slider, negative_prompt_input],
|
| 91 |
-
outputs=[gr.Image(type="pil", label="Generated Profile Picture
|
| 92 |
title="✨ Creative Profile Picture Generator (SFW)",
|
| 93 |
description="Upload a reference photo and a creative prompt to generate a new, stylized avatar using image-to-image diffusion. Transformation strength controls how much the output deviates from the input image (0.1 is subtle, 0.9 is highly stylized).",
|
| 94 |
live=False,
|
|
|
|
| 9 |
|
| 10 |
try:
|
| 11 |
print(f"Loading model on device: {device}")
|
| 12 |
+
|
| 13 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 14 |
MODEL_ID,
|
| 15 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 16 |
+
low_cpu_mem_usage=True, # Added optimization for low RAM environments
|
| 17 |
)
|
| 18 |
pipe = pipe.to(device)
|
| 19 |
print("Model loaded successfully.")
|
|
|
|
| 36 |
The 'style_strength' controls how much the new image deviates from the original.
|
| 37 |
"""
|
| 38 |
if pipe is None:
|
| 39 |
+
if not prompt:
|
|
|
|
|
|
|
| 40 |
return initial_image, "Please enter a descriptive text prompt."
|
| 41 |
|
| 42 |
if initial_image is None:
|
|
|
|
| 45 |
print(f"Generating image with prompt: '{prompt}' and strength: {style_strength}")
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
with torch.autocast(device) if device == "cuda" else torch.no_grad():
|
| 49 |
generated_image = pipe(
|
| 50 |
prompt=prompt,
|
| 51 |
image=initial_image.convert("RGB"), # Ensure image is in RGB format
|
|
|
|
| 61 |
print(f"Inference error: {e}")
|
| 62 |
return initial_image, f"An error occurred during generation: {e}"
|
| 63 |
|
| 64 |
+
|
| 65 |
+
# Define the input components
|
| 66 |
image_input = gr.Image(type="pil", label="1. Upload Base Image (Your Photo or Style Reference)")
|
| 67 |
prompt_input = gr.Textbox(
|
| 68 |
label="2. Creative Prompt (e.g., 'A professional LinkedIn headshot, cyberpunk aesthetic, highly detailed')",
|
|
|
|
| 89 |
gr.Interface(
|
| 90 |
fn=generate_avatar,
|
| 91 |
inputs=[image_input, prompt_input, strength_slider, steps_slider, negative_prompt_input],
|
| 92 |
+
outputs=[gr.Image(type="pil", label="Generated Profile Picture"), gr.Textbox(label="Status")],
|
| 93 |
title="✨ Creative Profile Picture Generator (SFW)",
|
| 94 |
description="Upload a reference photo and a creative prompt to generate a new, stylized avatar using image-to-image diffusion. Transformation strength controls how much the output deviates from the input image (0.1 is subtle, 0.9 is highly stylized).",
|
| 95 |
live=False,
|