File size: 2,654 Bytes
e918b7d
 
e10c510
e918b7d
 
28662a7
6172564
28662a7
 
e918b7d
28662a7
e918b7d
6172564
28662a7
 
 
 
 
6172564
 
 
28662a7
 
 
e918b7d
 
 
28662a7
e918b7d
28662a7
 
e918b7d
28662a7
e918b7d
28662a7
e918b7d
 
28662a7
 
6172564
e918b7d
 
6172564
28662a7
6172564
28662a7
 
e918b7d
28662a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e918b7d
 
 
28662a7
 
 
e918b7d
28662a7
 
 
 
 
 
 
 
e918b7d
28662a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6172564
28662a7
 
e918b7d
28662a7
e918b7d
28662a7
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
import gc

MODEL_ID = "CompVis/stable-diffusion-v1-4"

pipe = None
device = "cuda" if torch.cuda.is_available() else "cpu"

print(f"πŸš€ Device: {device}")

def load_model():
    global pipe

    if pipe is not None:
        return "βœ… Model sudah siap!"

    gc.collect()
    if device == "cuda":
        torch.cuda.empty_cache()

    print("πŸ“¦ Loading model...")

    pipe = StableDiffusionPipeline.from_pretrained(
        MODEL_ID,
        torch_dtype=torch.float16 if device == "cuda" else torch.float32,
        safety_checker=None
    )

    # πŸ”₯ OPTIMASI WAJIB
    pipe.enable_attention_slicing()

    if device == "cuda":
        pipe.to("cuda")
    else:
        pipe.enable_vae_slicing()

    print("βœ… Model ready")
    return "βœ… Model siap digunakan!"

def generate(prompt, negative_prompt, steps, guidance, width, height, seed):
    global pipe

    if pipe is None:
        return None, "⚠️ Model belum siap"

    try:
        # Limit ukuran (biar ga OOM di free tier)
        width = min(width, 512)
        height = min(height, 512)

        generator = torch.manual_seed(int(seed)) if seed != -1 else None

        image = pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            num_inference_steps=int(steps),
            guidance_scale=float(guidance),
            width=width,
            height=height,
            generator=generator
        ).images[0]

        gc.collect()
        if device == "cuda":
            torch.cuda.empty_cache()

        return image, "βœ… Done"

    except Exception as e:
        return None, f"❌ Error: {str(e)}"

# UI
with gr.Blocks() as demo:
    gr.Markdown("# 🎨 AI Image Generator (HF Free Tier Safe)")

    status = gr.Markdown("⏳ Loading model...")

    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="Prompt")
            negative = gr.Textbox(label="Negative Prompt", value="blurry, low quality")

            steps = gr.Slider(10, 25, value=18)
            guidance = gr.Slider(1, 10, value=7)

            width = gr.Dropdown([256, 384, 512], value=512)
            height = gr.Dropdown([256, 384, 512], value=512)

            seed = gr.Number(value=-1)

            btn = gr.Button("Generate")

        with gr.Column():
            output = gr.Image()
            result = gr.Markdown()

    demo.load(load_model, outputs=status)

    btn.click(
        generate,
        inputs=[prompt, negative, steps, guidance, width, height, seed],
        outputs=[output, result]
    )

if __name__ == "__main__":
    demo.launch()