Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# --- Model Loading --- #
|
| 8 |
+
print('Loading Ghibli and Anime style models...')
|
| 9 |
+
|
| 10 |
+
# Determine the device to run the model on
|
| 11 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 12 |
+
print(f"Using device: {device}")
|
| 13 |
+
|
| 14 |
+
# --- Load Ghibli Style Model ---
|
| 15 |
+
ghibli_model_id = 'nitrosocke/Ghibli-Diffusion'
|
| 16 |
+
|
| 17 |
+
# Note: `torch_dtype` is used here instead of `dtype` due to observed behavior in diffusers 0.36.0
|
| 18 |
+
# where `dtype` was ignored and `torch_dtype` was effective despite deprecation warnings.
|
| 19 |
+
if device == 'cuda':
|
| 20 |
+
ghibli_pipeline = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 21 |
+
ghibli_model_id,
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
cache_dir='./model_cache_ghibli'
|
| 24 |
+
)
|
| 25 |
+
else:
|
| 26 |
+
ghibli_pipeline = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 27 |
+
ghibli_model_id,
|
| 28 |
+
torch_dtype=torch.float32,
|
| 29 |
+
cache_dir='./model_cache_ghibli'
|
| 30 |
+
)
|
| 31 |
+
ghibli_pipeline.to(device)
|
| 32 |
+
print(f'Ghibli Style Model ({ghibli_model_id}) loaded successfully.')
|
| 33 |
+
|
| 34 |
+
# --- Load Anime Style Model ---
|
| 35 |
+
anime_model_id = 'hakurei/waifu-diffusion'
|
| 36 |
+
if device == 'cuda':
|
| 37 |
+
anime_pipeline = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 38 |
+
anime_model_id,
|
| 39 |
+
torch_dtype=torch.float16,
|
| 40 |
+
cache_dir='./model_cache_anime'
|
| 41 |
+
)
|
| 42 |
+
else:
|
| 43 |
+
anime_pipeline = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 44 |
+
anime_model_id,
|
| 45 |
+
torch_dtype=torch.float32,
|
| 46 |
+
cache_dir='./model_cache_anime'
|
| 47 |
+
)
|
| 48 |
+
anime_pipeline.to(device)
|
| 49 |
+
print(f'Anime Style Model ({anime_model_id}) loaded successfully.')
|
| 50 |
+
|
| 51 |
+
print('Both Ghibli and Anime Style Models loaded and moved to device successfully.')
|
| 52 |
+
|
| 53 |
+
# --- Transformation Function ---
|
| 54 |
+
def cartoon_transform(input_image: Image.Image, style: str) -> Image.Image:
|
| 55 |
+
"""
|
| 56 |
+
Applies a cartoon-style transformation to the input image using the loaded Stable Diffusion pipelines.
|
| 57 |
+
|
| 58 |
+
Args:
|
| 59 |
+
input_image (PIL.Image.Image): The input image to transform.
|
| 60 |
+
style (str): The desired cartoon style ('Ghibli' or 'Anime').
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
PIL.Image.Image: The transformed image in the selected cartoon style.
|
| 64 |
+
"""
|
| 65 |
+
# Ensure the image is in RGB format
|
| 66 |
+
if input_image.mode != 'RGB':
|
| 67 |
+
input_image = input_image.convert('RGB')
|
| 68 |
+
|
| 69 |
+
# Set reasonable dimensions to avoid excessive memory usage and ensure reasonable processing time
|
| 70 |
+
# while maintaining aspect ratio
|
| 71 |
+
max_dim = 768 # Maximum dimension for processing
|
| 72 |
+
width, height = input_image.size
|
| 73 |
+
if max(width, height) > max_dim:
|
| 74 |
+
ratio = max_dim / max(width, height)
|
| 75 |
+
new_width = int(width * ratio)
|
| 76 |
+
new_height = int(height * ratio)
|
| 77 |
+
input_image = input_image.resize((new_width, new_height), Image.LANCZOS)
|
| 78 |
+
|
| 79 |
+
# Define pipelines, prompts, and parameters based on style
|
| 80 |
+
if style == 'Ghibli':
|
| 81 |
+
pipeline_to_use = ghibli_pipeline
|
| 82 |
+
prompt = "Studio Ghibli style, detailed, vibrant colors, fantasy, magical, serene"
|
| 83 |
+
strength = 0.75
|
| 84 |
+
guidance_scale = 7.5
|
| 85 |
+
num_inference_steps = 25 # Reduced for faster processing
|
| 86 |
+
elif style == 'Anime':
|
| 87 |
+
pipeline_to_use = anime_pipeline
|
| 88 |
+
prompt = "anime character, vibrant, digital art, high quality, detailed eyes"
|
| 89 |
+
strength = 0.8 # Slightly higher strength for a more pronounced anime effect
|
| 90 |
+
guidance_scale = 8.0
|
| 91 |
+
num_inference_steps = 25 # Reduced for faster processing
|
| 92 |
+
else:
|
| 93 |
+
raise ValueError(f"Unsupported style: {style}. Choose from 'Ghibli' or 'Anime'.")
|
| 94 |
+
|
| 95 |
+
# Run the image-to-image pipeline
|
| 96 |
+
transformed_image = pipeline_to_use(
|
| 97 |
+
prompt=prompt,
|
| 98 |
+
image=input_image,
|
| 99 |
+
strength=strength,
|
| 100 |
+
guidance_scale=guidance_scale,
|
| 101 |
+
num_inference_steps=num_inference_steps
|
| 102 |
+
).images[0]
|
| 103 |
+
|
| 104 |
+
print(f'Image transformed to {style} style.')
|
| 105 |
+
return transformed_image
|
| 106 |
+
|
| 107 |
+
# --- Gradio Interface ---
|
| 108 |
+
# Available cartoon styles
|
| 109 |
+
cartoon_styles = ['Ghibli', 'Anime']
|
| 110 |
+
|
| 111 |
+
# Create the Gradio interface
|
| 112 |
+
iface = gr.Interface(
|
| 113 |
+
fn=cartoon_transform,
|
| 114 |
+
inputs=[
|
| 115 |
+
gr.Image(type='pil', label='Upload your picture'),
|
| 116 |
+
gr.Dropdown(
|
| 117 |
+
choices=cartoon_styles,
|
| 118 |
+
label='Select Cartoon Style',
|
| 119 |
+
value='Ghibli' # Default selection
|
| 120 |
+
)
|
| 121 |
+
],
|
| 122 |
+
outputs=gr.Image(type='pil', label='Transformed Image'),
|
| 123 |
+
title='Cartoon Style Image Transformer',
|
| 124 |
+
description='Upload a picture and transform it into various cartoon styles.'
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Launch the Gradio app - this part is typically removed or commented out when deploying to Hugging Face Spaces,
|
| 128 |
+
# as Spaces handle the launch automatically.
|
| 129 |
+
if __name__ == '__main__':
|
| 130 |
+
print('Launching Gradio interface locally...')
|
| 131 |
+
iface.launch(share=True) # share=True for Colab, change to False for local dev
|