Anurag181011 commited on
Commit
4e7c0a2
·
1 Parent(s): 1fe2c76
Files changed (1) hide show
  1. app.py +18 -6
app.py CHANGED
@@ -3,26 +3,38 @@ import torch
3
  from diffusers import StableDiffusionImg2ImgPipeline
4
  from PIL import Image
5
 
6
- # Load the pre-trained Studio Ghibli style model
7
- model_id = "nitrosocke/Ghibli-Diffusion"
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
9
 
 
 
10
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
11
- model_id, torch_dtype=torch.float16
12
- ).to(device)
 
 
 
 
 
 
 
13
 
14
  def transform_image(input_image: Image.Image) -> Image.Image:
15
  input_image = input_image.resize((512, 512))
16
- prompt = "ghibli style"
 
 
17
  output = pipe(
18
  prompt=prompt,
19
  image=input_image,
20
  strength=0.75,
21
  guidance_scale=7.5,
22
- num_inference_steps=50,
23
  )
24
  return output.images[0]
25
 
 
26
  demo = gr.Interface(
27
  fn=transform_image,
28
  inputs=gr.Image(type="pil", label="Upload your portrait/photo"),
 
3
  from diffusers import StableDiffusionImg2ImgPipeline
4
  from PIL import Image
5
 
6
+ # Check if GPU is available
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ print(f"Using device: {device}")
9
 
10
+ # Load the model correctly
11
+ model_id = "nitrosocke/Ghibli-Diffusion"
12
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
13
+ model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32
14
+ )
15
+
16
+ # Enable optimization if using CUDA
17
+ if device == "cuda":
18
+ pipe.to(device)
19
+ pipe.enable_model_cpu_offload() # Helps with memory efficiency
20
+ else:
21
+ pipe.to("cpu") # Ensure CPU compatibility
22
 
23
  def transform_image(input_image: Image.Image) -> Image.Image:
24
  input_image = input_image.resize((512, 512))
25
+
26
+ # Move image to proper format for inference
27
+ prompt = "ghibli style, cinematic lighting, hand-painted, anime aesthetics"
28
  output = pipe(
29
  prompt=prompt,
30
  image=input_image,
31
  strength=0.75,
32
  guidance_scale=7.5,
33
+ num_inference_steps=30, # Reduced steps for faster generation
34
  )
35
  return output.images[0]
36
 
37
+ # Build the Gradio UI
38
  demo = gr.Interface(
39
  fn=transform_image,
40
  inputs=gr.Image(type="pil", label="Upload your portrait/photo"),