Spaces:
Running
Running
| import gradio as gr | |
| from diffusers import StableDiffusionImg2ImgPipeline | |
| import torch | |
| from PIL import Image | |
| # Load the model | |
| model_id = "nitrosocke/Ghibli-Diffusion" | |
| pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float32) | |
| # Move pipeline to GPU if available | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| pipe = pipe.to(device) | |
| # Define the inference function | |
| def ghibli_transform(input_image, prompt="ghibli style", strength=0.75, guidance_scale=7.5, num_steps=50): | |
| if input_image is None: | |
| raise gr.Error("No image uploaded! Please upload an image before clicking Transform.") | |
| # Process the input image (keep it as PIL Image) | |
| try: | |
| init_image = input_image.convert("RGB").resize((768, 768)) | |
| except Exception as e: | |
| raise gr.Error(f"Failed to process image: {str(e)}") | |
| # Generate the Ghibli-style image | |
| try: | |
| output = pipe( | |
| prompt=prompt, | |
| image=init_image, | |
| strength=strength, | |
| guidance_scale=guidance_scale, | |
| num_inference_steps=num_steps # Use the UI-provided value | |
| ).images[0] | |
| except Exception as e: | |
| raise gr.Error(f"Pipeline error: {str(e)}") | |
| return output | |
| # Create the Gradio interface | |
| with gr.Blocks(title="Ghibli Diffusion Image Transformer") as demo: | |
| gr.Markdown("# Ghibli Diffusion Image Transformer") | |
| gr.Markdown("Upload an image and transform it into Studio Ghibli style using nitrosocke/Ghibli-Diffusion!") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_img = gr.Image(label="Upload Image", type="pil") | |
| prompt = gr.Textbox(label="Prompt", value="ghibli style") | |
| strength = gr.Slider(0, 1, value=0.75, step=0.05, label="Strength (How much to transform)") | |
| guidance = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale") | |
| num_steps = gr.Slider(10, 100, value=50, step=5, label="Inference Steps (Higher = Better Quality, Slower)") | |
| submit_btn = gr.Button("Transform") | |
| with gr.Column(): | |
| output_img = gr.Image(label="Ghibli-Style Output") | |
| # Connect the button to the function | |
| submit_btn.click( | |
| fn=ghibli_transform, | |
| inputs=[input_img, prompt, strength, guidance, num_steps], | |
| outputs=output_img | |
| ) | |
| # Launch the Space with share=True for public link | |
| demo.launch(share=True) |