File size: 2,449 Bytes
d9e6ed1
 
 
 
 
 
 
 
 
 
 
4f156b9
d9e6ed1
 
 
4f156b9
 
d9e6ed1
 
4f156b9
d9e6ed1
4f156b9
 
 
 
d9e6ed1
 
4f156b9
 
d9e6ed1
 
4f156b9
 
 
 
 
 
 
 
 
 
 
 
 
d9e6ed1
 
 
 
 
4f156b9
d9e6ed1
4f156b9
d9e6ed1
4f156b9
d9e6ed1
 
 
 
4f156b9
 
d9e6ed1
 
 
 
4f156b9
d9e6ed1
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from diffusers import AutoPipelineForText2Image
import torch
import os

# Model configuration
MODEL_NAME = "katuni4ka/tiny-random-flex.2-preview"
CACHE_DIR = "./model_cache"

os.makedirs(CACHE_DIR, exist_ok=True)

# Load model with Flux-specific settings
pipe = AutoPipelineForText2Image.from_pretrained(
    MODEL_NAME,
    torch_dtype=torch.float16,
    cache_dir=CACHE_DIR,
    max_seq_length=512  # Critical parameter for Flux models
).to("cuda" if torch.cuda.is_available() else "cpu")

# Aspect ratios using multiples of 16 for Flux compatibility
ASPECT_RATIOS = {
    "Square (512x512)": (512, 512),
    "Landscape (1024x512)": (1024, 512),
    "Portrait (512x1024)": (512, 1024),
    "A4 (768x1024)": (768, 1024)
}

def generate_image(prompt, aspect_ratio):
    """Generate image with Flux-specific parameters"""
    width, height = ASPECT_RATIOS[aspect_ratio]
    
    try:
        with torch.inference_mode():
            image = pipe(
                prompt=prompt,
                width=width,
                height=height,
                num_inference_steps=20,
                guidance_scale=4.5,
                generator=torch.Generator(device="cuda").manual_seed(42)  # Fixed seed
            ).images[0]
        return image
    except Exception as e:
        return f"Error: {str(e)}"

# UI Configuration
with gr.Blocks(theme="huggingface", analytics_enabled=False) as demo:
    gr.Markdown("""
    # Tiny Random Flex Text-to-Image Generator
    Experimental Flux-based model with critical fixes for tensor shape errors
    
    🔧 Important: This model requires specific input dimensions and has limited capabilities
    """)

    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(
                label="Prompt",
                placeholder="Try simple prompts like 'a colorful pattern'",
                lines=2
            )
            aspect_ratio = gr.Dropdown(
                label="Aspect Ratio",
                choices=list(ASPECT_RATIOS.keys()),
                value="Square (512x512)"
            )
            generate_btn = gr.Button("🎨 Generate Image", variant="primary")
        
        with gr.Column():
            output_image = gr.Image(label="Generated Image", interactive=False)
    
    generate_btn.click(
        fn=generate_image,
        inputs=[prompt, aspect_ratio],
        outputs=output_image
    )

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