Spaces:
Sleeping
Sleeping
Anurag181011
commited on
Commit
Β·
cdbd072
1
Parent(s):
cecf975
op
Browse files
app.py
CHANGED
@@ -1,15 +1,24 @@
|
|
1 |
import os
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
-
from diffusers import
|
5 |
from PIL import Image
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
|
|
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:
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
# Try enabling xFormers for memory efficiency
|
34 |
try:
|
35 |
-
pipe.
|
36 |
-
print("β
|
37 |
except Exception as e:
|
38 |
-
print(f"β οΈ
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
pipe.enable_vae_slicing()
|
43 |
pipe.enable_attention_slicing()
|
44 |
|
45 |
-
#
|
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
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
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=
|
74 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
75 |
)
|
76 |
|
|
|
77 |
if __name__ == "__main__":
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|