chrisjayden commited on
Commit
3b3bbf5
·
verified ·
1 Parent(s): ac225bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -2,16 +2,26 @@ import gradio as gr
2
  from diffusers import StableDiffusionImg2ImgPipeline
3
  import torch
4
  from PIL import Image
 
5
 
6
  # Load the model (this runs when the Space starts)
7
  model_id = "nitrosocke/Ghibli-Diffusion"
8
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float32) # Use float32 for CPU
9
- #pipe = pipe.to("cuda") # Use GPU if available in the Space
10
 
11
  # Define the inference function
12
  def ghibli_transform(input_image, prompt="ghibli style", strength=0.75, guidance_scale=7.5):
13
- # Convert Gradio image (numpy array) to PIL Image and resize
14
- init_image = Image.fromarray(input_image).convert("RGB").resize((768, 768))
 
 
 
 
 
 
 
 
 
 
15
 
16
  # Generate the Ghibli-style image
17
  output = pipe(
 
2
  from diffusers import StableDiffusionImg2ImgPipeline
3
  import torch
4
  from PIL import Image
5
+ import numpy as np
6
 
7
  # Load the model (this runs when the Space starts)
8
  model_id = "nitrosocke/Ghibli-Diffusion"
9
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
 
10
 
11
  # Define the inference function
12
  def ghibli_transform(input_image, prompt="ghibli style", strength=0.75, guidance_scale=7.5):
13
+ # Check if input_image is valid
14
+ if input_image is None:
15
+ raise gr.Error("Please upload an image before clicking Transform!")
16
+
17
+ # Ensure input_image is a NumPy array and convert to PIL
18
+ if not isinstance(input_image, np.ndarray):
19
+ raise gr.Error("Input image format is invalid. Expected a NumPy array.")
20
+
21
+ try:
22
+ init_image = Image.fromarray(input_image).convert("RGB").resize((768, 768))
23
+ except Exception as e:
24
+ raise gr.Error(f"Failed to process image: {str(e)}")
25
 
26
  # Generate the Ghibli-style image
27
  output = pipe(