File size: 3,574 Bytes
71c9241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
import gradio as gr
from diffusers import AutoPipelineForText2Image
import torch

# Initialize the Imagen 4 model
pipeline = AutoPipelineForText2Image.from_pretrained(
    "google/imagen-4",
    torch_dtype=torch.float16,
    variant="fp16",
).to("cuda")

def generate_image(prompt, negative_prompt="", steps=30, guidance_scale=7.5):
    """Generate image from text prompt using Imagen 4"""
    with torch.inference_mode():
        result = pipeline(
            prompt=prompt,
            negative_prompt=negative_prompt,
            num_inference_steps=steps,
            guidance_scale=guidance_scale
        )
        return result.images[0]

# Custom theme for modern look
custom_theme = gr.themes.Soft(
    primary_hue="blue",
    secondary_hue="indigo",
    neutral_hue="slate",
    font=gr.themes.GoogleFont("Inter"),
    text_size="lg",
    spacing_size="lg",
    radius_size="md"
).set(
    button_primary_background_fill="*primary_600",
    button_primary_background_fill_hover="*primary_700",
    block_title_text_weight="600",
)

# Gradio 6 app
with gr.Blocks() as demo:
    gr.Markdown(
        """
        # 🎨 Imagen 4 Image Generator
        **Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)**
        Generate high-quality images from text prompts using Google's Imagen 4 model.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            prompt = gr.Textbox(
                label="Prompt",
                placeholder="Describe the image you want to generate...",
                lines=3
            )
            negative_prompt = gr.Textbox(
                label="Negative Prompt",
                placeholder="Things you don't want in the image...",
                lines=2
            )
            
            with gr.Accordion("Advanced Settings", open=False):
                steps = gr.Slider(
                    label="Inference Steps",
                    minimum=10,
                    maximum=100,
                    value=30,
                    step=1
                )
                guidance_scale = gr.Slider(
                    label="Guidance Scale",
                    minimum=1.0,
                    maximum=20.0,
                    value=7.5,
                    step=0.1
                )
            
            generate_btn = gr.Button("Generate Image", variant="primary")
        
        with gr.Column(scale=1):
            output_image = gr.Image(
                label="Generated Image",
                height=512,
                width=512
            )
            with gr.Row():
                download_btn = gr.Button("Download")
                share_btn = gr.Button("Share", variant="secondary")
    
    # Event handlers
    generate_btn.click(
        fn=generate_image,
        inputs=[prompt, negative_prompt, steps, guidance_scale],
        outputs=output_image,
        api_visibility="public"
    )
    
    # Example prompts
    gr.Examples(
        examples=[
            ["A majestic lion in the savannah at sunset, photorealistic"],
            ["Cyberpunk cityscape at night with neon lights"],
            ["Cute anime-style cat wearing a spacesuit"],
        ],
        inputs=prompt,
        label="Try these examples!"
    )

# Launch with modern theme and settings
demo.launch(
    theme=custom_theme,
    footer_links=[
        {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
        {"label": "Imagen 4 Model", "url": "https://huggingface.co/google/imagen-4"}
    ],
    allowed_paths=["."]
)