Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,36 @@ import gradio as gr
|
|
| 2 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
# Load the model
|
| 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 |
-
print(f"Input received: {input_image is not None}")
|
| 14 |
-
print(f"Input type: {type(input_image)}")
|
| 15 |
if input_image is None:
|
| 16 |
raise gr.Error("No image uploaded! Please upload an image before clicking Transform.")
|
| 17 |
|
| 18 |
-
# Process the input image
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
init_image = input_image.resize((768, 768))
|
| 22 |
-
print(f"Converted init_image size: {init_image.size}, mode: {init_image.mode}")
|
| 23 |
-
except AttributeError as e:
|
| 24 |
-
raise gr.Error(f"Input is not a valid image: {str(e)}")
|
| 25 |
except Exception as e:
|
| 26 |
raise gr.Error(f"Failed to process image: {str(e)}")
|
| 27 |
|
| 28 |
-
# Convert to NumPy array as a workaround
|
| 29 |
-
init_image_np = np.array(init_image)
|
| 30 |
-
print(f"Converted to NumPy: {type(init_image_np)}, shape: {init_image_np.shape}")
|
| 31 |
-
|
| 32 |
# Generate the Ghibli-style image
|
| 33 |
try:
|
| 34 |
output = pipe(
|
| 35 |
prompt=prompt,
|
| 36 |
-
init_image
|
| 37 |
strength=strength,
|
| 38 |
guidance_scale=guidance_scale,
|
| 39 |
num_inference_steps=50
|
| 40 |
).images[0]
|
| 41 |
-
print("Pipeline executed successfully")
|
| 42 |
except Exception as e:
|
| 43 |
raise gr.Error(f"Pipeline error: {str(e)}")
|
| 44 |
|
|
@@ -66,5 +59,5 @@ with gr.Blocks(title="Ghibli Diffusion Image Transformer") as demo:
|
|
| 66 |
outputs=output_img
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# Launch the Space
|
| 70 |
-
demo.launch()
|
|
|
|
| 2 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
model_id = "nitrosocke/Ghibli-Diffusion"
|
| 8 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 9 |
|
| 10 |
+
# Move pipeline to GPU if available
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
pipe = pipe.to(device)
|
| 13 |
+
|
| 14 |
# Define the inference function
|
| 15 |
def ghibli_transform(input_image, prompt="ghibli style", strength=0.75, guidance_scale=7.5):
|
|
|
|
|
|
|
| 16 |
if input_image is None:
|
| 17 |
raise gr.Error("No image uploaded! Please upload an image before clicking Transform.")
|
| 18 |
|
| 19 |
+
# Process the input image (keep it as PIL Image)
|
| 20 |
try:
|
| 21 |
+
# Ensure the image is in RGB and resized
|
| 22 |
+
init_image = 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 |
try:
|
| 28 |
output = pipe(
|
| 29 |
prompt=prompt,
|
| 30 |
+
image=init_image, # Pass PIL Image directly
|
| 31 |
strength=strength,
|
| 32 |
guidance_scale=guidance_scale,
|
| 33 |
num_inference_steps=50
|
| 34 |
).images[0]
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
raise gr.Error(f"Pipeline error: {str(e)}")
|
| 37 |
|
|
|
|
| 59 |
outputs=output_img
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Launch the Space with share=True for public link
|
| 63 |
+
demo.launch(share=True)
|