Spaces:
Sleeping
Sleeping
Anurag181011
commited on
Commit
·
155699f
1
Parent(s):
5901c31
scscs
Browse files
app.py
CHANGED
|
@@ -1,53 +1,67 @@
|
|
| 1 |
-
import
|
| 2 |
import torch
|
|
|
|
| 3 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
from PIL import Image
|
| 5 |
-
|
| 6 |
-
#
|
| 7 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
|
|
|
|
|
|
|
|
|
| 8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
print(f"Using device: {device}")
|
| 10 |
-
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 11 |
|
| 12 |
-
# Ensure torch is installed
|
| 13 |
try:
|
| 14 |
torch.zeros(1).to(device)
|
| 15 |
print("Torch initialized successfully on", device)
|
| 16 |
except Exception as e:
|
| 17 |
print("Torch initialization error:", e)
|
| 18 |
|
| 19 |
-
# Load the Stable Diffusion model
|
| 20 |
model_id = "nitrosocke/Ghibli-Diffusion"
|
|
|
|
| 21 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 22 |
-
model_id,
|
| 23 |
-
torch_dtype=torch.
|
|
|
|
|
|
|
| 24 |
safety_checker=None
|
| 25 |
).to(device)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
input_image = input_image.resize((512, 512))
|
| 34 |
-
prompt = "ghibli style, cinematic lighting, hand-painted, anime aesthetics"
|
| 35 |
|
| 36 |
output = pipe(
|
| 37 |
prompt=prompt,
|
| 38 |
image=input_image,
|
| 39 |
-
strength=0.65,
|
| 40 |
-
guidance_scale=5
|
| 41 |
-
num_inference_steps=
|
| 42 |
)
|
|
|
|
| 43 |
return output.images[0]
|
| 44 |
|
| 45 |
-
# Gradio
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=transform_image,
|
| 48 |
inputs=gr.Image(type="pil", label="Upload your portrait/photo"),
|
| 49 |
outputs=gr.Image(type="pil", label="Studio Ghibli Style Output"),
|
| 50 |
-
title="Studio Ghibli
|
| 51 |
description="Upload a portrait or photo to transform it into a Studio Ghibli-style image.",
|
| 52 |
)
|
| 53 |
|
|
|
|
| 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
|
| 8 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
| 9 |
+
torch.backends.cudnn.benchmark = True
|
| 10 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 11 |
+
|
| 12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
print(f"Using device: {device}")
|
|
|
|
| 14 |
|
| 15 |
+
# Ensure torch is properly installed
|
| 16 |
try:
|
| 17 |
torch.zeros(1).to(device)
|
| 18 |
print("Torch initialized successfully on", device)
|
| 19 |
except Exception as e:
|
| 20 |
print("Torch initialization error:", e)
|
| 21 |
|
| 22 |
+
# Load the optimized Stable Diffusion model
|
| 23 |
model_id = "nitrosocke/Ghibli-Diffusion"
|
| 24 |
+
|
| 25 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 26 |
+
model_id,
|
| 27 |
+
torch_dtype=torch.float16,
|
| 28 |
+
use_safetensors=True,
|
| 29 |
+
low_cpu_mem_usage=True,
|
| 30 |
safety_checker=None
|
| 31 |
).to(device)
|
| 32 |
|
| 33 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 34 |
+
pipe.enable_model_cpu_offload()
|
| 35 |
+
pipe.enable_vae_slicing()
|
| 36 |
+
pipe.enable_attention_slicing()
|
| 37 |
|
| 38 |
+
# Enhanced prompt for Studio Ghibli-style transformation
|
| 39 |
+
prompt = (
|
| 40 |
+
"Beautiful Studio Ghibli anime-style portrait, breathtaking landscape background, "
|
| 41 |
+
"soft pastel colors, hand-painted texture, cinematic lighting, dreamy atmosphere, "
|
| 42 |
+
"vibrant and rich details, Miyazaki aesthetic, magical realism, watercolor effect, "
|
| 43 |
+
"warm sunlight, stunning composition, high detail, fantasy world."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
def transform_image(input_image):
|
| 47 |
input_image = input_image.resize((512, 512))
|
|
|
|
| 48 |
|
| 49 |
output = pipe(
|
| 50 |
prompt=prompt,
|
| 51 |
image=input_image,
|
| 52 |
+
strength=0.65,
|
| 53 |
+
guidance_scale=4.5,
|
| 54 |
+
num_inference_steps=20,
|
| 55 |
)
|
| 56 |
+
|
| 57 |
return output.images[0]
|
| 58 |
|
| 59 |
+
# Gradio UI
|
| 60 |
demo = gr.Interface(
|
| 61 |
fn=transform_image,
|
| 62 |
inputs=gr.Image(type="pil", label="Upload your portrait/photo"),
|
| 63 |
outputs=gr.Image(type="pil", label="Studio Ghibli Style Output"),
|
| 64 |
+
title="Studio Ghibli AI Converter",
|
| 65 |
description="Upload a portrait or photo to transform it into a Studio Ghibli-style image.",
|
| 66 |
)
|
| 67 |
|