Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,47 +3,40 @@ import torch
|
|
3 |
from diffusers import StableDiffusionImg2ImgPipeline
|
4 |
from PIL import Image
|
5 |
|
6 |
-
#
|
7 |
-
device = "
|
8 |
-
dtype = torch.float16
|
9 |
|
10 |
-
# Load
|
11 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
12 |
-
"
|
13 |
torch_dtype=dtype
|
14 |
).to(device)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
|
19 |
-
if torch.__version__ >= "2.0":
|
20 |
-
pipe.unet = torch.compile(pipe.unet) # Speed up inference
|
21 |
-
|
22 |
-
if device == "cuda":
|
23 |
-
torch.backends.cudnn.benchmark = True # Optimize CUDA performance
|
24 |
-
|
25 |
-
# Process image
|
26 |
def process_image(input_img):
|
27 |
if input_img is None:
|
28 |
return None
|
29 |
-
|
30 |
-
input_img = input_img.convert("RGB").resize((
|
31 |
|
32 |
result = pipe(
|
33 |
prompt="ghibli style, studio ghibli, anime art",
|
34 |
image=input_img,
|
35 |
-
strength=0.
|
36 |
-
guidance_scale=7
|
37 |
).images[0]
|
38 |
|
39 |
return result
|
40 |
|
41 |
-
#
|
42 |
demo = gr.Interface(
|
43 |
fn=process_image,
|
44 |
inputs=gr.Image(type="pil"),
|
45 |
outputs=gr.Image(type="pil"),
|
46 |
-
title="🎨 Ghibli Style Transfer",
|
47 |
description="Upload an image to transform it into Studio Ghibli style artwork"
|
48 |
)
|
49 |
|
|
|
3 |
from diffusers import StableDiffusionImg2ImgPipeline
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Use CPU and optimize precision
|
7 |
+
device = "cpu"
|
8 |
+
dtype = torch.float32 # float16 is only for GPUs
|
9 |
|
10 |
+
# Load model with reduced precision for CPU
|
11 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
12 |
+
"nitrosocke/Ghibli-Diffusion",
|
13 |
torch_dtype=dtype
|
14 |
).to(device)
|
15 |
|
16 |
+
# Disable xformers (only for GPU)
|
17 |
+
print("⚠️ Running on CPU: xformers disabled, inference will be slow.")
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def process_image(input_img):
|
20 |
if input_img is None:
|
21 |
return None
|
22 |
+
|
23 |
+
input_img = input_img.convert("RGB").resize((512, 512))
|
24 |
|
25 |
result = pipe(
|
26 |
prompt="ghibli style, studio ghibli, anime art",
|
27 |
image=input_img,
|
28 |
+
strength=0.5, # Reduce strength to speed up processing
|
29 |
+
guidance_scale=7.5 # Lower guidance for faster inference
|
30 |
).images[0]
|
31 |
|
32 |
return result
|
33 |
|
34 |
+
# Gradio UI
|
35 |
demo = gr.Interface(
|
36 |
fn=process_image,
|
37 |
inputs=gr.Image(type="pil"),
|
38 |
outputs=gr.Image(type="pil"),
|
39 |
+
title="🎨 Ghibli Style Transfer (CPU Optimized)",
|
40 |
description="Upload an image to transform it into Studio Ghibli style artwork"
|
41 |
)
|
42 |
|