File size: 986 Bytes
2562825
 
 
 
ddd1d55
2562825
 
5af654c
ddd1d55
 
5af654c
ddd1d55
 
1b4f79a
5af654c
 
2562825
 
ddd1d55
 
 
 
2562825
1b4f79a
2562825
 
ddd1d55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline

device = "cuda" if torch.cuda.is_available() else "cpu"
model_id = "nitrosocke/Ghibli-Diffusion"

# Load the model once and keep it in memory
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32)
pipe.to(device)
pipe.enable_attention_slicing()  # Optimize memory usage

def generate_ghibli_style(image):
    prompt = "ghibli style portrait"
    with torch.inference_mode():  # Disables gradient calculations for faster inference
        result = pipe(prompt, image=image, strength=0.6, guidance_scale=6.5, num_inference_steps=25).images[0]  # Reduced steps & optimized scale
    return result

iface = gr.Interface(
    fn=generate_ghibli_style,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(),
    title="Studio Ghibli Portrait Generator",
    description="Upload a photo to generate a Ghibli-style portrait!"
)

iface.launch()