c4dr commited on
Commit
705608a
·
verified ·
1 Parent(s): bc13ee0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -3,26 +3,37 @@ import torch
3
  from diffusers import StableDiffusionImg2ImgPipeline
4
  from PIL import Image
5
 
6
- # Initialize pipeline
7
- dtype = torch.float16 if torch.cuda.is_available() else torch.float32
 
 
 
8
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
9
- "nitrosocke/Ghibli-Diffusion",
10
  torch_dtype=dtype
11
- ).to("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
 
 
 
12
 
 
 
 
 
13
  def process_image(input_img):
14
  if input_img is None:
15
  return None
 
 
16
 
17
- # Convert and resize input
18
- input_img = input_img.convert("RGB").resize((512, 512))
19
-
20
- # Generate output
21
  result = pipe(
22
  prompt="ghibli style, studio ghibli, anime art",
23
  image=input_img,
24
- strength=0.7,
25
- guidance_scale=10
26
  ).images[0]
27
 
28
  return result
@@ -36,4 +47,4 @@ demo = gr.Interface(
36
  description="Upload an image to transform it into Studio Ghibli style artwork"
37
  )
38
 
39
- demo.launch()
 
3
  from diffusers import StableDiffusionImg2ImgPipeline
4
  from PIL import Image
5
 
6
+ # Device and dtype setup
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ dtype = torch.float16 if device == "cuda" else torch.float32
9
+
10
+ # Load optimized model
11
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
12
+ "stabilityai/sdxl-turbo", # Faster model
13
  torch_dtype=dtype
14
+ ).to(device)
15
+
16
+ # Enable optimizations
17
+ pipe.enable_xformers_memory_efficient_attention()
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((384, 384)) # Optimize input size
31
 
 
 
 
 
32
  result = pipe(
33
  prompt="ghibli style, studio ghibli, anime art",
34
  image=input_img,
35
+ strength=0.6,
36
+ guidance_scale=7
37
  ).images[0]
38
 
39
  return result
 
47
  description="Upload an image to transform it into Studio Ghibli style artwork"
48
  )
49
 
50
+ demo.launch()