File size: 2,223 Bytes
578f301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from diffusers import DiffusionPipeline
import os

# Load the model locally
pipe = DiffusionPipeline.from_pretrained("lightx2v/Qwen-Image-Lightning")

def generate_image(prompt: str):
    """
    Generate an image from text prompt using Qwen-Image-Lightning model.

    Args:
        prompt (str): The text description for image generation

    Returns:
        PIL.Image: The generated image
    """
    try:
        # Generate image using the local model
        image = pipe(prompt).images[0]
        return image
    except Exception as e:
        # Handle errors gracefully
        raise gr.Error(f"Error generating image: {str(e)}")

# Create Gradio interface
with gr.Blocks(title="Text-to-Image Generator", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🖼️ Text-to-Image Generator")
    gr.Markdown("Generate images from text prompts using the Qwen-Image-Lightning model. Powered by Hugging Face.")
    
    with gr.Row():
        prompt_input = gr.Textbox(
            label="Enter your prompt",
            placeholder="A beautiful sunset over mountains...",
            lines=3,
            show_copy_button=True
        )
    
    generate_btn = gr.Button("Generate Image", variant="primary", size="lg")
    
    output_image = gr.Image(label="Generated Image", show_download_button=True)
    
    # Examples for users to try
    gr.Examples(
        examples=[
            "A futuristic city at night with neon lights",
            "A cute kitten playing with yarn",
            "An astronaut walking on the moon",
            "A serene lake surrounded by autumn trees",
            "A steampunk airship flying over Victorian London"
        ],
        inputs=prompt_input,
        outputs=output_image,
        fn=generate_image,
        cache_examples=False
    )
    
    # Event handling
    prompt_input.submit(generate_image, inputs=prompt_input, outputs=output_image)
    generate_btn.click(generate_image, inputs=prompt_input, outputs=output_image)
    
    gr.Markdown("---")
    gr.Markdown('<p style="text-align: center;">Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></p>')

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