Gertie01 commited on
Commit
cad34ab
·
verified ·
1 Parent(s): 8b681e4

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +135 -0
  2. requirements.txt +17 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from rudalle.pipelines import generate_images, show
4
+ from rudalle import get_rudalle_model, get_tokenizer, get_vae
5
+ from rudalle.utils import seed_everything
6
+ import random
7
+ import numpy as np
8
+ from PIL import Image
9
+
10
+ # Load model
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model = get_rudalle_model('Malevich', pretrained=True, fp16=True, device=device)
13
+ vae = get_vae().to(device)
14
+ tokenizer = get_tokenizer()
15
+
16
+ def generate_images(prompt, negative_prompt=""):
17
+ if not prompt.strip():
18
+ prompt = "красивый пейзаж" # default prompt in Russian if empty
19
+
20
+ # Generate 4 images with random seeds
21
+ images = []
22
+ for _ in range(4):
23
+ # Use random seed for each image to ensure variety
24
+ seed = random.randint(0, 1000000)
25
+ seed_everything(seed)
26
+
27
+ with torch.no_grad():
28
+ pil_images = generate_images(prompt, tokenizer, model, vae, top_k=512, top_p=0.995, images_num=1, negative_prompt=negative_prompt)
29
+ if pil_images and len(pil_images) > 0:
30
+ images.append(pil_images[0])
31
+ else:
32
+ # Create a placeholder image if generation fails
33
+ img = Image.new('RGB', (512, 512), color='gray')
34
+ images.append(img)
35
+
36
+ return images
37
+
38
+ def select_image(images, evt: gr.SelectData):
39
+ """Return the selected image for closer look"""
40
+ if images and evt.index < len(images):
41
+ return images[evt.index]
42
+ return None
43
+
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown("# 🎨 ruDALL-E Malevich Image Generator")
46
+ gr.HTML('<p style="text-align: center; margin-bottom: 20px;">Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></p>')
47
+
48
+ with gr.Row():
49
+ with gr.Column(scale=3):
50
+ prompt_input = gr.Textbox(
51
+ label="Prompt",
52
+ placeholder="Enter your prompt here (can be empty)...",
53
+ lines=2,
54
+ value=""
55
+ )
56
+ negative_prompt_input = gr.Textbox(
57
+ label="Negative Prompt",
58
+ placeholder="What you don't want to see...",
59
+ lines=2,
60
+ value=""
61
+ )
62
+ generate_btn = gr.Button("Generate Images", variant="primary", size="lg")
63
+
64
+ with gr.Column(scale=1):
65
+ gr.Markdown("""
66
+ ### Instructions:
67
+ - Enter a prompt (Russian works best)
68
+ - Leave empty for random landscapes
69
+ - Negative prompt is optional
70
+ - Click Generate to create 4 images
71
+ - Click any image to see it larger
72
+ """)
73
+
74
+ with gr.Row():
75
+ gallery = gr.Gallery(
76
+ label="Generated Images",
77
+ columns=2,
78
+ rows=2,
79
+ height="auto",
80
+ object_fit="cover",
81
+ show_label=True,
82
+ allow_preview=True,
83
+ interactive=True
84
+ )
85
+
86
+ with gr.Row():
87
+ selected_image = gr.Image(
88
+ label="Selected Image (Click an image above to view)",
89
+ height=512,
90
+ width=512,
91
+ interactive=False
92
+ )
93
+
94
+ # Generate button event
95
+ generate_btn.click(
96
+ fn=generate_images,
97
+ inputs=[prompt_input, negative_prompt_input],
98
+ outputs=gallery,
99
+ api_visibility="public"
100
+ )
101
+
102
+ # Gallery selection event
103
+ gallery.select(
104
+ fn=select_image,
105
+ inputs=[gallery],
106
+ outputs=[selected_image],
107
+ api_visibility="private"
108
+ )
109
+
110
+ # Generate on enter key for prompt inputs
111
+ prompt_input.submit(
112
+ fn=generate_images,
113
+ inputs=[prompt_input, negative_prompt_input],
114
+ outputs=gallery
115
+ )
116
+
117
+ negative_prompt_input.submit(
118
+ fn=generate_images,
119
+ inputs=[prompt_input, negative_prompt_input],
120
+ outputs=gallery
121
+ )
122
+
123
+ # Launch with modern theme
124
+ demo.launch(
125
+ theme=gr.themes.Soft(
126
+ primary_hue="indigo",
127
+ secondary_hue="purple",
128
+ neutral_hue="slate",
129
+ font=gr.themes.GoogleFont("Inter"),
130
+ text_size="lg",
131
+ spacing_size="lg",
132
+ radius_size="md"
133
+ ),
134
+ footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}]
135
+ )
requirements.txt ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ torch
3
+ torchvision
4
+ torchaudio
5
+ Pillow
6
+ rudalle
7
+ gradio>=6.0
8
+ requests
9
+ git+https://github.com/huggingface/transformers
10
+ git+https://github.com/huggingface/diffusers
11
+ sentencepiece
12
+ accelerate
13
+ tokenizers
14
+ tqdm
15
+ matplotlib
16
+ opencv-python
17
+ scipy