Anurag181011 commited on
Commit
f28e1e9
·
1 Parent(s): 60a8fdc
Files changed (1) hide show
  1. app.py +2 -18
app.py CHANGED
@@ -1,14 +1,14 @@
1
  import os
2
  import torch
3
  import gradio as gr
4
- from diffusers import StableDiffusionImg2ImgPipeline, StableDiffusionPipeline
5
  from PIL import Image
6
 
7
  # --- Configuration ---
8
  SPACE_TITLE = "🎨 Enhanced Studio Ghibli AI Art Generator"
9
  SPACE_DESCRIPTION = "Upload a portrait or a photo and transform it into a breathtaking Studio Ghibli-style masterpiece! Improved model and prompting for better results."
10
  MAIN_MODEL_ID = "nitrosocke/Ghibli-Diffusion"
11
- STYLE_MODEL_ID = "sayakpaul/sd-anime-diff" # Experiment with other anime style models
12
  STRENGTH = 0.60 # Adjust for better balance between input and style
13
  GUIDANCE_SCALE = 7.5 # Increased for better prompt adherence
14
  NUM_INFERENCE_STEPS = 30 # Increased for potentially higher quality
@@ -42,25 +42,14 @@ img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
42
  low_cpu_mem_usage=True
43
  ).to(device)
44
 
45
- # Load a separate Stable Diffusion model for generating initial style (optional, but can help)
46
- # You can comment this out if you only want to rely on the img2img model
47
- style_pipe = StableDiffusionPipeline.from_pretrained(
48
- STYLE_MODEL_ID,
49
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
50
- use_safetensors=True,
51
- low_cpu_mem_usage=True
52
- ).to(device)
53
-
54
  # --- Optimization (Conditional for CUDA) ---
55
  if device == "cuda":
56
  try:
57
  img2img_pipe.enable_xformers_memory_efficient_attention()
58
- style_pipe.enable_xformers_memory_efficient_attention()
59
  print("✅ xFormers enabled!")
60
  except Exception as e:
61
  print(f"⚠️ xFormers not available: {e}")
62
  img2img_pipe.enable_model_cpu_offload()
63
- style_pipe.enable_model_cpu_offload()
64
  pipe.enable_vae_slicing()
65
  pipe.enable_attention_slicing()
66
 
@@ -72,11 +61,6 @@ def transform_image(input_image):
72
  try:
73
  input_image = input_image.resize(INPUT_IMAGE_SIZE)
74
 
75
- # Generate an initial stylized image using the style model (optional)
76
- # initial_style_prompt = f"{PROMPT_PREFIX} portrait of a person"
77
- # initial_style_image = style_pipe(prompt=initial_style_prompt, negative_prompt=NEGATIVE_PROMPT, num_inference_steps=NUM_INFERENCE_STEPS // 2).images[0]
78
-
79
- # Use the input image directly with the img2img pipeline
80
  output = img2img_pipe(
81
  prompt=PROMPT_PREFIX + "portrait of a person", # Adjust prompt based on input
82
  image=input_image,
 
1
  import os
2
  import torch
3
  import gradio as gr
4
+ from diffusers import StableDiffusionImg2ImgPipeline
5
  from PIL import Image
6
 
7
  # --- Configuration ---
8
  SPACE_TITLE = "🎨 Enhanced Studio Ghibli AI Art Generator"
9
  SPACE_DESCRIPTION = "Upload a portrait or a photo and transform it into a breathtaking Studio Ghibli-style masterpiece! Improved model and prompting for better results."
10
  MAIN_MODEL_ID = "nitrosocke/Ghibli-Diffusion"
11
+ # STYLE_MODEL_ID = "sayakpaul/sd-anime-diff" # Removed the problematic style model
12
  STRENGTH = 0.60 # Adjust for better balance between input and style
13
  GUIDANCE_SCALE = 7.5 # Increased for better prompt adherence
14
  NUM_INFERENCE_STEPS = 30 # Increased for potentially higher quality
 
42
  low_cpu_mem_usage=True
43
  ).to(device)
44
 
 
 
 
 
 
 
 
 
 
45
  # --- Optimization (Conditional for CUDA) ---
46
  if device == "cuda":
47
  try:
48
  img2img_pipe.enable_xformers_memory_efficient_attention()
 
49
  print("✅ xFormers enabled!")
50
  except Exception as e:
51
  print(f"⚠️ xFormers not available: {e}")
52
  img2img_pipe.enable_model_cpu_offload()
 
53
  pipe.enable_vae_slicing()
54
  pipe.enable_attention_slicing()
55
 
 
61
  try:
62
  input_image = input_image.resize(INPUT_IMAGE_SIZE)
63
 
 
 
 
 
 
64
  output = img2img_pipe(
65
  prompt=PROMPT_PREFIX + "portrait of a person", # Adjust prompt based on input
66
  image=input_image,