Anurag181011 commited on
Commit
cdbd072
Β·
1 Parent(s): cecf975
Files changed (1) hide show
  1. app.py +81 -46
app.py CHANGED
@@ -1,15 +1,24 @@
1
  import os
2
  import torch
3
  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
 
 
 
 
 
 
 
 
11
 
12
- # Check if GPU is available
 
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
  print(f"πŸš€ Using device: {device}")
15
 
@@ -18,61 +27,87 @@ 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 correct Stable Diffusion pipeline
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
- # Pass the image as `init_image`
58
- output = pipe(
59
- prompt=prompt,
60
- init_image=input_image, # βœ… FIXED: Changed from "image" to "init_image"
61
- strength=0.65,
62
- guidance_scale=5.0, # Slightly increased for better stylization
63
- num_inference_steps=25, # More steps for higher quality output
64
- )
65
 
66
- return output.images[0]
 
 
 
 
 
 
 
67
 
68
- # Gradio UI
69
- demo = gr.Interface(
 
 
 
 
 
70
  fn=transform_image,
71
  inputs=gr.Image(type="pil", label="Upload a Portrait/Photo"),
72
  outputs=gr.Image(type="pil", label="Studio Ghibli-Style Output"),
73
- title="🎨 Studio Ghibli AI Art Generator",
74
- description="Upload a portrait or a photo and transform it into a breathtaking Studio Ghibli-style masterpiece!",
 
 
 
 
 
75
  )
76
 
 
77
  if __name__ == "__main__":
78
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import torch
3
  import gradio as gr
4
+ from diffusers import DiffusionPipeline
5
  from PIL import Image
6
 
7
+ # --- Configuration ---
8
+ SPACE_TITLE = "🎨 Enhanced Studio Ghibli AI Art Generator (LoRA)"
9
+ SPACE_DESCRIPTION = "Upload a portrait or a photo and transform it into a breathtaking Studio Ghibli-style masterpiece using a LoRA for fine-tuned results."
10
+ BASE_MODEL_ID = "black-forest-labs/FLUX.1-dev"
11
+ LORA_REPO_ID = "strangerzonehf/Flux-Ghibli-Art-LoRA"
12
+ TRIGGER_WORD = "Ghibli Art"
13
+ STRENGTH = 0.60 # Adjust for better balance between input and style
14
+ GUIDANCE_SCALE = 7.5 # Increased for better prompt adherence
15
+ NUM_INFERENCE_STEPS = 30 # Increased for potentially higher quality
16
+ INPUT_IMAGE_SIZE = (512, 512)
17
+ PROMPT_PREFIX = "" # No need for separate prefix as LoRA is targeted
18
+ NEGATIVE_PROMPT = "ugly, deformed, blurry, low quality, bad anatomy, bad proportions, disfigured, poorly drawn face, mutation, mutated, extra limbs, extra fingers, body horror, glitchy, tiling"
19
 
20
+ # --- Device Setup ---
21
+ # Attempt to use CUDA if available, otherwise fallback to CPU
22
  device = "cuda" if torch.cuda.is_available() else "cpu"
23
  print(f"πŸš€ Using device: {device}")
24
 
 
27
  torch.zeros(1).to(device)
28
  print("βœ… Torch initialized successfully on", device)
29
  except Exception as e:
30
+ print(f"⚠️ Torch initialization error: {e}")
31
 
32
+ # --- Model Loading ---
33
+ try:
34
+ pipe = DiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=torch.bfloat16)
35
+ except ValueError as e:
36
+ if "sentencepiece" in str(e):
37
+ print("⚠️ Error: sentencepiece is not installed. Please install it with: pip install sentencepiece")
38
+ raise
39
+ else:
40
+ raise e
41
 
 
42
  try:
43
+ pipe.load_lora_weights(LORA_REPO_ID)
44
+ print(f"βœ… LoRA weights loaded from {LORA_REPO_ID}")
45
  except Exception as e:
46
+ print(f"⚠️ Error loading LoRA weights: {e}")
47
 
48
+ pipe.to(device)
49
+
50
+ # --- Optimization (Conditional for CUDA) ---
51
+ if device == "cuda":
52
+ try:
53
+ pipe.enable_xformers_memory_efficient_attention()
54
+ print("βœ… xFormers enabled!")
55
+ except Exception as e:
56
+ print(f"⚠️ xFormers not available: {e}")
57
+ pipe.enable_model_cpu_offload()
58
  pipe.enable_vae_slicing()
59
  pipe.enable_attention_slicing()
60
 
61
+ # --- Image Transformation Function ---
 
 
 
 
 
 
 
 
62
  def transform_image(input_image):
63
+ if input_image is None:
64
+ return None
65
+
66
+ try:
67
+ input_image = input_image.resize(INPUT_IMAGE_SIZE)
68
 
69
+ prompt = f"{PROMPT_PREFIX} {TRIGGER_WORD}, portrait of a person" # Incorporate trigger word
 
 
 
 
 
 
 
70
 
71
+ output = pipe(
72
+ prompt=prompt,
73
+ image=input_image,
74
+ strength=STRENGTH,
75
+ guidance_scale=GUIDANCE_SCALE,
76
+ num_inference_steps=NUM_INFERENCE_STEPS,
77
+ negative_prompt=NEGATIVE_PROMPT,
78
+ ).images[0]
79
 
80
+ return output
81
+ except Exception as e:
82
+ print(f"❌ Error during image transformation: {e}")
83
+ return None
84
+
85
+ # --- Gradio UI ---
86
+ iface = gr.Interface(
87
  fn=transform_image,
88
  inputs=gr.Image(type="pil", label="Upload a Portrait/Photo"),
89
  outputs=gr.Image(type="pil", label="Studio Ghibli-Style Output"),
90
+ title=SPACE_TITLE,
91
+ description=SPACE_DESCRIPTION,
92
+ examples=[
93
+ "examples/portrait1.jpg",
94
+ "examples/photo1.jpg",
95
+ "examples/landscape1.jpg",
96
+ ],
97
  )
98
 
99
+ # --- Main Execution ---
100
  if __name__ == "__main__":
101
+ # Create an 'examples' directory if it doesn't exist and add some sample images
102
+ if not os.path.exists("examples"):
103
+ os.makedirs("examples")
104
+ # You'll need to download or create these example images
105
+ # and place them in the 'examples' folder.
106
+ # Example:
107
+ # from urllib.request import urlretrieve
108
+ # urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_1", "examples/portrait1.jpg")
109
+ # urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_2", "examples/photo1.jpg")
110
+ # urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_3", "examples/landscape1.jpg")
111
+ print("ℹ️ Created 'examples' directory. Please add sample images.")
112
+
113
+ iface.launch()