Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	File size: 2,795 Bytes
			
			6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 0db4d23 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73 6405f1a 7411c73  | 
								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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90  | 
								import gradio as gr
import torch
from diffusers import StableDiffusionPipeline, DPMSolverSinglestepScheduler
from PIL import Image
# Load a memory-efficient SD variant (under 12GB)
model_id = "runwayml/stable-diffusion-v1-5"
@gr.cache()
def load_model():
    pipe = StableDiffusionPipeline.from_pretrained(
        model_id,
        torch_dtype=torch.float16,
        safety_checker=None,
        use_safetensors=True
    )
    pipe.scheduler = DPMSolverSinglestepScheduler.from_config(pipe.scheduler.config)
    pipe = pipe.to("cpu")
    pipe.enable_attention_slicing()  # Reduces memory by 30%
    pipe.enable_model_cpu_offload()  # Only loads needed components
    return pipe
def generate_character(prompt, seed=42):
    try:
        pipe = load_model()
        generator = torch.Generator(device="cpu").manual_seed(seed)
        
        with torch.inference_mode():
            image = pipe(
                prompt=f"cartoon character {prompt}, vibrant colors, clean lines",
                negative_prompt="blurry, deformed, ugly",
                num_inference_steps=20,
                guidance_scale=7.5,
                width=512,
                height=512,
                generator=generator
            ).images[0]
        
        return image
    except Exception as e:
        return f"Error: {str(e)}\nTry simplifying your prompt."
# Animation through img2img
def generate_animation(prompt, frames=3):
    base_image = generate_character(prompt)
    if isinstance(base_image, str):  # If error
        return base_image
    
    images = [base_image]
    pipe = load_model()
    
    for i in range(1, frames):
        result = pipe(
            prompt=prompt,
            image=images[-1],
            strength=0.3,  # Small changes per frame
            generator=torch.Generator().manual_seed(i)
        )
        images.append(result.images[0])
    
    images[0].save(
        "animation.gif",
        save_all=True,
        append_images=images[1:],
        duration=500,
        loop=0
    )
    return "animation.gif"
with gr.Blocks(theme=gr.themes.Base()) as demo:
    gr.Markdown("# 🎬 Character Animator (12GB Optimized)")
    
    with gr.Row():
        prompt = gr.Textbox(
            label="Character Description",
            placeholder="e.g. 'cyberpunk fox wearing sunglasses'"
        )
    
    with gr.Tab("Single Image"):
        img_out = gr.Image(label="Generated Character", type="pil")
        gen_btn = gr.Button("Generate")
    
    with gr.Tab("Animation"):
        anim_out = gr.Image(label="Animation", format="gif")
        anim_btn = gr.Button("Create Animation (3 frames)")
    
    gen_btn.click(generate_character, inputs=prompt, outputs=img_out)
    anim_btn.click(generate_animation, inputs=prompt, outputs=anim_out)
demo.launch() |