Spaces:
Sleeping
Sleeping
Anurag181011
commited on
Commit
·
358c39a
1
Parent(s):
b5cdc6f
xvxvx
Browse files
app.py
CHANGED
@@ -4,50 +4,53 @@ import gradio as gr
|
|
4 |
from diffusers import StableDiffusionImg2ImgPipeline
|
5 |
from PIL import Image
|
6 |
|
7 |
-
# Force CUDA usage
|
8 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
9 |
torch.backends.cudnn.benchmark = True
|
10 |
torch.backends.cuda.matmul.allow_tf32 = True
|
11 |
|
12 |
-
|
13 |
-
|
|
|
14 |
|
15 |
-
# Ensure
|
16 |
try:
|
17 |
torch.zeros(1).to(device)
|
18 |
-
print("Torch initialized successfully on", device)
|
19 |
except Exception as e:
|
20 |
-
print("Torch initialization error:", e)
|
21 |
|
22 |
# Load the optimized Stable Diffusion model
|
23 |
model_id = "nitrosocke/Ghibli-Diffusion"
|
24 |
|
25 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
26 |
model_id,
|
27 |
-
torch_dtype=torch.float16,
|
28 |
use_safetensors=True,
|
29 |
low_cpu_mem_usage=True
|
30 |
-
).to(
|
31 |
|
32 |
-
# Try enabling
|
33 |
try:
|
34 |
pipe.enable_xformers_memory_efficient_attention()
|
35 |
print("✅ xFormers enabled!")
|
36 |
except Exception as e:
|
37 |
print(f"⚠️ xFormers not available: {e}")
|
38 |
-
|
|
|
39 |
pipe.enable_model_cpu_offload()
|
40 |
pipe.enable_vae_slicing()
|
41 |
pipe.enable_attention_slicing()
|
42 |
|
43 |
-
# Enhanced
|
44 |
prompt = (
|
45 |
-
"
|
46 |
-
"
|
47 |
-
"
|
48 |
-
"
|
49 |
)
|
50 |
|
|
|
51 |
def transform_image(input_image):
|
52 |
input_image = input_image.resize((512, 512))
|
53 |
|
@@ -55,8 +58,8 @@ def transform_image(input_image):
|
|
55 |
prompt=prompt,
|
56 |
image=input_image,
|
57 |
strength=0.65,
|
58 |
-
guidance_scale=
|
59 |
-
num_inference_steps=
|
60 |
)
|
61 |
|
62 |
return output.images[0]
|
@@ -64,10 +67,10 @@ def transform_image(input_image):
|
|
64 |
# Gradio UI
|
65 |
demo = gr.Interface(
|
66 |
fn=transform_image,
|
67 |
-
inputs=gr.Image(type="pil", label="Upload
|
68 |
-
outputs=gr.Image(type="pil", label="Studio Ghibli
|
69 |
-
title="Studio Ghibli AI
|
70 |
-
description="Upload a portrait or photo
|
71 |
)
|
72 |
|
73 |
if __name__ == "__main__":
|
|
|
4 |
from diffusers import StableDiffusionImg2ImgPipeline
|
5 |
from PIL import Image
|
6 |
|
7 |
+
# Force CUDA usage if available
|
8 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
9 |
torch.backends.cudnn.benchmark = True
|
10 |
torch.backends.cuda.matmul.allow_tf32 = True
|
11 |
|
12 |
+
# Check if GPU is available
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
print(f"🚀 Using device: {device}")
|
15 |
|
16 |
+
# Ensure Torch is correctly initialized
|
17 |
try:
|
18 |
torch.zeros(1).to(device)
|
19 |
+
print("✅ Torch initialized successfully on", device)
|
20 |
except Exception as e:
|
21 |
+
print("⚠️ Torch initialization error:", e)
|
22 |
|
23 |
# Load the optimized Stable Diffusion model
|
24 |
model_id = "nitrosocke/Ghibli-Diffusion"
|
25 |
|
26 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
27 |
model_id,
|
28 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
29 |
use_safetensors=True,
|
30 |
low_cpu_mem_usage=True
|
31 |
+
).to(device)
|
32 |
|
33 |
+
# Try enabling xFormers for memory efficiency
|
34 |
try:
|
35 |
pipe.enable_xformers_memory_efficient_attention()
|
36 |
print("✅ xFormers enabled!")
|
37 |
except Exception as e:
|
38 |
print(f"⚠️ xFormers not available: {e}")
|
39 |
+
|
40 |
+
# Apply additional optimizations for performance
|
41 |
pipe.enable_model_cpu_offload()
|
42 |
pipe.enable_vae_slicing()
|
43 |
pipe.enable_attention_slicing()
|
44 |
|
45 |
+
# Enhanced Studio Ghibli-style transformation prompt
|
46 |
prompt = (
|
47 |
+
"Studio Ghibli anime-style illustration, magical landscape, soft pastel colors, "
|
48 |
+
"hand-painted textures, cinematic lighting, dreamy atmosphere, vibrant and rich details, "
|
49 |
+
"Miyazaki-inspired fantasy world, watercolor aesthetic, warm sunlight, intricate composition, "
|
50 |
+
"high detail, whimsical and nostalgic beauty."
|
51 |
)
|
52 |
|
53 |
+
# Image transformation function
|
54 |
def transform_image(input_image):
|
55 |
input_image = input_image.resize((512, 512))
|
56 |
|
|
|
58 |
prompt=prompt,
|
59 |
image=input_image,
|
60 |
strength=0.65,
|
61 |
+
guidance_scale=5.0, # Slightly increased for better stylization
|
62 |
+
num_inference_steps=25, # More steps for higher quality output
|
63 |
)
|
64 |
|
65 |
return output.images[0]
|
|
|
67 |
# Gradio UI
|
68 |
demo = gr.Interface(
|
69 |
fn=transform_image,
|
70 |
+
inputs=gr.Image(type="pil", label="Upload a Portrait/Photo"),
|
71 |
+
outputs=gr.Image(type="pil", label="Studio Ghibli-Style Output"),
|
72 |
+
title="🎨 Studio Ghibli AI Art Generator",
|
73 |
+
description="Upload a portrait or a photo and transform it into a breathtaking Studio Ghibli-style masterpiece!",
|
74 |
)
|
75 |
|
76 |
if __name__ == "__main__":
|