Anurag181011 commited on
Commit
03ce0df
·
1 Parent(s): 358c39a
Files changed (1) hide show
  1. app.py +64 -32
app.py CHANGED
@@ -4,7 +4,17 @@ import gradio as gr
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
@@ -18,60 +28,82 @@ 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
 
57
- output = pipe(
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]
 
 
 
66
 
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__":
77
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from diffusers import StableDiffusionImg2ImgPipeline
5
  from PIL import Image
6
 
7
+ # --- Configuration ---
8
+ SPACE_TITLE = "🎨 Studio Ghibli AI Art Generator"
9
+ SPACE_DESCRIPTION = "Upload a portrait or a photo and transform it into a breathtaking Studio Ghibli-style masterpiece!"
10
+ MODEL_ID = "nitrosocke/Ghibli-Diffusion"
11
+ STRENGTH = 0.65
12
+ GUIDANCE_SCALE = 5.0
13
+ NUM_INFERENCE_STEPS = 25
14
+ INPUT_IMAGE_SIZE = (512, 512)
15
+
16
+ # --- Device Setup ---
17
+ # Force CUDA usage, assuming A100 is the first GPU (index 0)
18
  os.environ["CUDA_VISIBLE_DEVICES"] = "0"
19
  torch.backends.cudnn.benchmark = True
20
  torch.backends.cuda.matmul.allow_tf32 = True
 
28
  torch.zeros(1).to(device)
29
  print("✅ Torch initialized successfully on", device)
30
  except Exception as e:
31
+ print(f"⚠️ Torch initialization error: {e}")
 
 
 
32
 
33
+ # --- Model Loading ---
34
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
35
+ MODEL_ID,
36
  torch_dtype=torch.float16 if device == "cuda" else torch.float32,
37
  use_safetensors=True,
38
  low_cpu_mem_usage=True
39
  ).to(device)
40
 
41
+ # --- Optimization (Conditional for CUDA) ---
42
+ if device == "cuda":
43
+ try:
44
+ pipe.enable_xformers_memory_efficient_attention()
45
+ print("✅ xFormers enabled!")
46
+ except Exception as e:
47
+ print(f"⚠️ xFormers not available: {e}")
48
+ pipe.enable_model_cpu_offload() # Keep most of the model on GPU, offload selectively
 
49
  pipe.enable_vae_slicing()
50
  pipe.enable_attention_slicing()
51
 
52
+ # --- Prompt Definition ---
53
+ GHIBLI_PROMPT = (
54
  "Studio Ghibli anime-style illustration, magical landscape, soft pastel colors, "
55
  "hand-painted textures, cinematic lighting, dreamy atmosphere, vibrant and rich details, "
56
  "Miyazaki-inspired fantasy world, watercolor aesthetic, warm sunlight, intricate composition, "
57
  "high detail, whimsical and nostalgic beauty."
58
  )
59
 
60
+ # --- Image Transformation Function ---
61
  def transform_image(input_image):
62
+ if input_image is None:
63
+ return None
64
+
65
+ try:
66
+ input_image = input_image.resize(INPUT_IMAGE_SIZE)
67
 
68
+ output = pipe(
69
+ prompt=GHIBLI_PROMPT,
70
+ image=input_image,
71
+ strength=STRENGTH,
72
+ guidance_scale=GUIDANCE_SCALE,
73
+ num_inference_steps=NUM_INFERENCE_STEPS,
74
+ )
75
 
76
+ return output.images[0]
77
+ except Exception as e:
78
+ print(f"❌ Error during image transformation: {e}")
79
+ return None
80
 
81
+ # --- Gradio UI ---
82
+ iface = gr.Interface(
83
  fn=transform_image,
84
  inputs=gr.Image(type="pil", label="Upload a Portrait/Photo"),
85
  outputs=gr.Image(type="pil", label="Studio Ghibli-Style Output"),
86
+ title=SPACE_TITLE,
87
+ description=SPACE_DESCRIPTION,
88
+ examples=[
89
+ "examples/portrait1.jpg",
90
+ "examples/photo1.jpg",
91
+ "examples/landscape1.jpg",
92
+ ],
93
  )
94
 
95
+ # --- Main Execution ---
96
  if __name__ == "__main__":
97
+ # Create an 'examples' directory if it doesn't exist and add some sample images
98
+ if not os.path.exists("examples"):
99
+ os.makedirs("examples")
100
+ # You'll need to download or create these example images
101
+ # and place them in the 'examples' folder.
102
+ # Example:
103
+ # from urllib.request import urlretrieve
104
+ # urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_1", "examples/portrait1.jpg")
105
+ # urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_2", "examples/photo1.jpg")
106
+ # urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_3", "examples/landscape1.jpg")
107
+ print("ℹ️ Created 'examples' directory. Please add sample images.")
108
+
109
+ iface.launch()