File size: 1,626 Bytes
6405f1a
 
c7cf423
6405f1a
 
c7cf423
 
6405f1a
 
 
7411c73
c7cf423
6405f1a
0db4d23
 
7411c73
 
c7cf423
7411c73
6405f1a
7411c73
6405f1a
 
7411c73
6405f1a
c7cf423
 
 
 
 
 
 
 
6405f1a
 
 
c7cf423
6405f1a
c7cf423
 
6405f1a
 
7411c73
c7cf423
 
 
6405f1a
 
c7cf423
 
6405f1a
c7cf423
 
 
 
 
6405f1a
c7cf423
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
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image

# Use a smaller SD model variant that fits within free tier
MODEL_ID = "CompVis/ldm-super-resolution-4x-openimages"  # Only ~1.4GB

@gr.cache()
def load_model():
    pipe = StableDiffusionPipeline.from_pretrained(
        MODEL_ID,
        torch_dtype=torch.float16,
        safety_checker=None,
        use_safetensors=True
    )
    pipe = pipe.to("cpu")
    pipe.enable_attention_slicing()  # Reduces memory usage
    return pipe

def generate_character(prompt, seed=42):
    try:
        pipe = load_model()
        generator = torch.Generator(device="cpu").manual_seed(seed)
        
        image = pipe(
            prompt=f"pixel art {prompt}, clean lines, vibrant colors",
            num_inference_steps=20,
            guidance_scale=7.0,
            width=256,
            height=256,
            generator=generator
        ).images[0]
        
        return image
    except Exception as e:
        return f"Error: {str(e)}\nTry a simpler prompt."

with gr.Blocks(theme=gr.themes.Default()) as demo:
    gr.Markdown("# 🎮 Lightweight Character Generator")
    
    with gr.Row():
        prompt = gr.Textbox(
            label="Describe your character",
            placeholder="e.g. 'robot pirate with laser eye'",
            max_lines=2
        )
    
    generate_btn = gr.Button("Generate", variant="primary")
    output = gr.Image(label="Your Character", type="pil")
    
    generate_btn.click(
        generate_character,
        inputs=prompt,
        outputs=output
    )

demo.launch(debug=False)