Gertie01 commited on
Commit
9cf72f1
·
verified ·
1 Parent(s): 5a7d571

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -0
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import torch
5
+ from rudalle.pipelines import generate_images, show
6
+ from rudalle import get_rudalle_model, get_tokenizer, get_vae
7
+ from rudalle.utils import seed_everything
8
+ import warnings
9
+ warnings.filterwarnings("ignore")
10
+
11
+ # Load model and tokenizer
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+ model = get_rudalle_model("Malevich", pretrained=True, fp16=True, device=device)
14
+ tokenizer = get_tokenizer()
15
+ vae = get_vae(dwt=False).to(device)
16
+
17
+ def generate_images(prompt, negative_prompt="", progress=gr.Progress()):
18
+ """Generate 4 images using ruDALLE Malevich model"""
19
+ if not prompt.strip():
20
+ prompt = "beautiful landscape" # Default prompt if empty
21
+
22
+ try:
23
+ # Generate 4 images
24
+ images = []
25
+ pil_images = []
26
+
27
+ with progress.tqdm(total=4, desc="Generating images") as pbar:
28
+ for i in range(4):
29
+ # Generate image
30
+ _pil_image = generate_images(
31
+ prompt,
32
+ tokenizer,
33
+ model,
34
+ vae,
35
+ top_k=2048,
36
+ top_p=0.995,
37
+ temperature=1.0,
38
+ image_count=1
39
+ )[0]
40
+
41
+ images.append(_pil_image)
42
+ pil_images.append(_pil_image)
43
+ pbar.update(1)
44
+
45
+ return pil_images
46
+
47
+ except Exception as e:
48
+ print(f"Error generating images: {e}")
49
+ # Return placeholder images on error
50
+ placeholder = Image.new('RGB', (512, 512), color='gray')
51
+ return [placeholder] * 4
52
+
53
+ def show_selected_image(gallery, evt: gr.SelectData):
54
+ """Display the selected image in larger view"""
55
+ if gallery and evt.index < len(gallery):
56
+ return gallery[evt.index]
57
+ return None
58
+
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("# 🎨 ruDALLE Malevich Image Generation")
61
+ gr.Markdown("Generate beautiful images using the ai-forever/rudalle-Malevich model. [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)")
62
+
63
+ with gr.Row():
64
+ with gr.Column(scale=3):
65
+ prompt_input = gr.Textbox(
66
+ label="Prompt",
67
+ placeholder="Enter your prompt here (optional)",
68
+ lines=2
69
+ )
70
+ negative_prompt_input = gr.Textbox(
71
+ label="Negative Prompt",
72
+ placeholder="What to avoid in the image (optional)",
73
+ lines=2
74
+ )
75
+
76
+ with gr.Row():
77
+ generate_btn = gr.Button("🎨 Generate Images", variant="primary", size="lg")
78
+ clear_btn = gr.Button("🗑️ Clear", size="lg")
79
+
80
+ with gr.Column(scale=2):
81
+ gr.Markdown("""
82
+ ### Instructions:
83
+ - Enter a prompt or leave empty for random generation
84
+ - Add negative prompts to exclude elements
85
+ - Click Generate to create 4 images
86
+ - Click any image to view it larger
87
+ """)
88
+
89
+ with gr.Row():
90
+ gallery = gr.Gallery(
91
+ label="Generated Images",
92
+ columns=2,
93
+ rows=2,
94
+ height="auto",
95
+ allow_preview=True,
96
+ show_label=True,
97
+ elem_id="gallery"
98
+ )
99
+
100
+ with gr.Row():
101
+ selected_image = gr.Image(
102
+ label="Selected Image (Click an image above)",
103
+ height=512,
104
+ width=512,
105
+ interactive=False
106
+ )
107
+
108
+ # Event handlers
109
+ generate_btn.click(
110
+ fn=generate_images,
111
+ inputs=[prompt_input, negative_prompt_input],
112
+ outputs=[gallery],
113
+ api_visibility="public"
114
+ )
115
+
116
+ gallery.select(
117
+ fn=show_selected_image,
118
+ inputs=[gallery],
119
+ outputs=[selected_image],
120
+ api_visibility="public"
121
+ )
122
+
123
+ clear_btn.click(
124
+ fn=lambda: (None, None, None),
125
+ outputs=[prompt_input, negative_prompt_input, gallery],
126
+ api_visibility="public"
127
+ )
128
+
129
+ # Generate on load with empty prompt
130
+ demo.load(
131
+ fn=lambda: generate_images(""),
132
+ outputs=[gallery],
133
+ api_visibility="public"
134
+ )
135
+
136
+ # Launch with modern Gradio 6 theme and styling
137
+ demo.launch(
138
+ theme=gr.themes.Soft(
139
+ primary_hue="blue",
140
+ secondary_hue="indigo",
141
+ neutral_hue="slate",
142
+ font=gr.themes.GoogleFont("Inter"),
143
+ text_size="lg",
144
+ spacing_size="lg",
145
+ radius_size="md"
146
+ ).set(
147
+ button_primary_background_fill="*primary_600",
148
+ button_primary_background_fill_hover="*primary_700",
149
+ block_title_text_weight="600",
150
+ ),
151
+ css="""
152
+ #gallery {
153
+ border: 2px solid var(--primary_200);
154
+ border-radius: 12px;
155
+ padding: 10px;
156
+ background: var(--background_fill_secondary);
157
+ }
158
+ .gradio-container {
159
+ max-width: 1200px !important;
160
+ }
161
+ """,
162
+ footer_links=[
163
+ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
164
+ {"label": "ruDALLE Model", "url": "https://huggingface.co/ai-forever/rudalle-Malevich"}
165
+ ]
166
+ )