Spaces:
Sleeping
Sleeping
Anurag181011
commited on
Commit
·
cecf975
1
Parent(s):
6d0f7d2
vb
Browse files
app.py
CHANGED
@@ -1,24 +1,10 @@
|
|
1 |
import os
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
-
from diffusers import
|
5 |
from PIL import Image
|
6 |
|
7 |
-
#
|
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 |
-
# Force CUDA usage, assuming A100 is the first GPU (index 0)
|
22 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
23 |
torch.backends.cudnn.benchmark = True
|
24 |
torch.backends.cuda.matmul.allow_tf32 = True
|
@@ -32,87 +18,61 @@ try:
|
|
32 |
torch.zeros(1).to(device)
|
33 |
print("✅ Torch initialized successfully on", device)
|
34 |
except Exception as e:
|
35 |
-
print(
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
|
|
|
47 |
try:
|
48 |
-
pipe.
|
49 |
-
print(
|
50 |
except Exception as e:
|
51 |
-
print(f"⚠️
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
# --- Optimization (Conditional for CUDA) ---
|
56 |
-
if device == "cuda":
|
57 |
-
try:
|
58 |
-
pipe.enable_xformers_memory_efficient_attention()
|
59 |
-
print("✅ xFormers enabled!")
|
60 |
-
except Exception as e:
|
61 |
-
print(f"⚠️ xFormers not available: {e}")
|
62 |
-
pipe.enable_model_cpu_offload()
|
63 |
pipe.enable_vae_slicing()
|
64 |
pipe.enable_attention_slicing()
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
|
85 |
-
|
86 |
-
except Exception as e:
|
87 |
-
print(f"❌ Error during image transformation: {e}")
|
88 |
-
return None
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
fn=transform_image,
|
93 |
inputs=gr.Image(type="pil", label="Upload a Portrait/Photo"),
|
94 |
outputs=gr.Image(type="pil", label="Studio Ghibli-Style Output"),
|
95 |
-
title=
|
96 |
-
description=
|
97 |
-
examples=[
|
98 |
-
"examples/portrait1.jpg",
|
99 |
-
"examples/photo1.jpg",
|
100 |
-
"examples/landscape1.jpg",
|
101 |
-
],
|
102 |
)
|
103 |
|
104 |
-
# --- Main Execution ---
|
105 |
if __name__ == "__main__":
|
106 |
-
|
107 |
-
if not os.path.exists("examples"):
|
108 |
-
os.makedirs("examples")
|
109 |
-
# You'll need to download or create these example images
|
110 |
-
# and place them in the 'examples' folder.
|
111 |
-
# Example:
|
112 |
-
# from urllib.request import urlretrieve
|
113 |
-
# urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_1", "examples/portrait1.jpg")
|
114 |
-
# urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_2", "examples/photo1.jpg")
|
115 |
-
# urlretrieve("URL_TO_YOUR_EXAMPLE_IMAGE_3", "examples/landscape1.jpg")
|
116 |
-
print("ℹ️ Created 'examples' directory. Please add sample images.")
|
117 |
-
|
118 |
-
iface.launch()
|
|
|
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
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|