Spaces:
Sleeping
Sleeping
Anurag181011
commited on
Commit
·
535b58c
1
Parent(s):
c6897d0
scscscc
Browse files
app.py
CHANGED
@@ -1,18 +1,20 @@
|
|
1 |
import os
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
-
from diffusers import
|
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
|
10 |
-
|
|
|
|
|
11 |
STRENGTH = 0.60 # Adjust for better balance between input and style
|
12 |
GUIDANCE_SCALE = 7.5 # Increased for better prompt adherence
|
13 |
NUM_INFERENCE_STEPS = 30 # Increased for potentially higher quality
|
14 |
INPUT_IMAGE_SIZE = (512, 512)
|
15 |
-
PROMPT_PREFIX = "
|
16 |
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"
|
17 |
|
18 |
# --- Device Setup ---
|
@@ -33,24 +35,26 @@ except Exception as e:
|
|
33 |
print(f"⚠️ Torch initialization error: {e}")
|
34 |
|
35 |
# --- Model Loading ---
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
# --- Optimization (Conditional for CUDA) ---
|
45 |
if device == "cuda":
|
46 |
try:
|
47 |
-
|
48 |
print("✅ xFormers enabled!")
|
49 |
except Exception as e:
|
50 |
print(f"⚠️ xFormers not available: {e}")
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
# --- Image Transformation Function ---
|
56 |
def transform_image(input_image):
|
@@ -60,16 +64,18 @@ def transform_image(input_image):
|
|
60 |
try:
|
61 |
input_image = input_image.resize(INPUT_IMAGE_SIZE)
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
65 |
image=input_image,
|
66 |
strength=STRENGTH,
|
67 |
guidance_scale=GUIDANCE_SCALE,
|
68 |
num_inference_steps=NUM_INFERENCE_STEPS,
|
69 |
negative_prompt=NEGATIVE_PROMPT,
|
70 |
-
)
|
71 |
|
72 |
-
return output
|
73 |
except Exception as e:
|
74 |
print(f"❌ Error during image transformation: {e}")
|
75 |
return None
|
|
|
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 ---
|
|
|
35 |
print(f"⚠️ Torch initialization error: {e}")
|
36 |
|
37 |
# --- Model Loading ---
|
38 |
+
pipe = DiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=torch.bfloat16)
|
39 |
+
|
40 |
+
try:
|
41 |
+
pipe.load_lora_weights(LORA_REPO_ID)
|
42 |
+
print(f"✅ LoRA weights loaded from {LORA_REPO_ID}")
|
43 |
+
except Exception as e:
|
44 |
+
print(f"⚠️ Error loading LoRA weights: {e}")
|
45 |
+
|
46 |
+
pipe.to(device)
|
47 |
|
48 |
# --- Optimization (Conditional for CUDA) ---
|
49 |
if device == "cuda":
|
50 |
try:
|
51 |
+
pipe.enable_xformers_memory_efficient_attention()
|
52 |
print("✅ xFormers enabled!")
|
53 |
except Exception as e:
|
54 |
print(f"⚠️ xFormers not available: {e}")
|
55 |
+
pipe.enable_model_cpu_offload()
|
56 |
+
pipe.enable_vae_slicing()
|
57 |
+
pipe.enable_attention_slicing()
|
58 |
|
59 |
# --- Image Transformation Function ---
|
60 |
def transform_image(input_image):
|
|
|
64 |
try:
|
65 |
input_image = input_image.resize(INPUT_IMAGE_SIZE)
|
66 |
|
67 |
+
prompt = f"{PROMPT_PREFIX} {TRIGGER_WORD}, portrait of a person" # Incorporate trigger word
|
68 |
+
|
69 |
+
output = pipe(
|
70 |
+
prompt=prompt,
|
71 |
image=input_image,
|
72 |
strength=STRENGTH,
|
73 |
guidance_scale=GUIDANCE_SCALE,
|
74 |
num_inference_steps=NUM_INFERENCE_STEPS,
|
75 |
negative_prompt=NEGATIVE_PROMPT,
|
76 |
+
).images[0]
|
77 |
|
78 |
+
return output
|
79 |
except Exception as e:
|
80 |
print(f"❌ Error during image transformation: {e}")
|
81 |
return None
|