andre commited on
Commit
ee00dcd
·
verified ·
1 Parent(s): b51a224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +240 -149
app.py CHANGED
@@ -1,154 +1,245 @@
1
- import gradio as gr
2
- import numpy as np
3
  import random
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
- import torch
8
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
-
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
-
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
- height,
32
- guidance_scale,
33
- num_inference_steps,
34
- progress=gr.Progress(track_tqdm=True),
35
- ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
-
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
51
- return image, seed
52
-
53
-
54
- examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
  ]
59
 
60
- css = """
61
- #col-container {
62
- margin: 0 auto;
63
- max-width: 640px;
64
- }
65
- """
66
-
67
- with gr.Blocks(css=css) as demo:
68
- with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
-
71
- with gr.Row():
72
- prompt = gr.Text(
73
- label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
- )
79
-
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
-
82
- result = gr.Image(label="Result", show_label=False)
83
-
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
- )
91
-
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
- )
99
-
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
- with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
- )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
- )
127
-
128
- num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
- minimum=1,
131
- maximum=50,
132
- step=1,
133
- value=2, # Replace with defaults that work for your model
134
- )
135
-
136
- gr.Examples(examples=examples, inputs=[prompt])
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn=infer,
140
- inputs=[
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  prompt,
142
- negative_prompt,
143
- seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
- ],
150
- outputs=[result, seed],
151
- )
152
-
153
- if __name__ == "__main__":
154
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
 
2
  import random
3
+ from huggingface_hub import InferenceClient
4
+ from PIL import Image
5
+ from google.colab import userdata
6
+ from IPython.display import display, clear_output
7
+ import ipywidgets as widgets
8
+ from datetime import datetime
9
+
10
+ # Retrieve the Hugging Face token from Colab secrets
11
+ api_token = userdata.get("HF_TOKEN")
12
+
13
+ # List of models with aliases
14
+ models = [
15
+ {
16
+ "alias": "FLUX.1-dev",
17
+ "name": "black-forest-labs/FLUX.1-dev"
18
+ },
19
+ {
20
+ "alias": "Stable Diffusion 3.5 turbo",
21
+ "name": "stabilityai/stable-diffusion-3.5-large-turbo"
22
+ },
23
+ {
24
+ "alias": "Midjourney",
25
+ "name": "strangerzonehf/Flux-Midjourney-Mix2-LoRA"
26
+ }
27
+ ]
28
 
29
+ # Initialize the InferenceClient with the default model
30
+ client = InferenceClient(models[0]["name"], token=api_token)
31
+
32
+ # List of 10 prompts with intense combat
33
+ prompts = [
34
+ {
35
+ "alias": "Castle Siege",
36
+ "text": "A medieval castle under siege, with archers firing arrows from the walls, knights charging on horses, and catapults launching fireballs. The enemy army, dressed in {enemy_color} armor, is fiercely attacking the castle, with soldiers scaling ladders and clashing swords with the defenders. Arrows fly through the air, explosions light up the battlefield, and injured knights lie on the ground. Fire engulfs parts of the castle, and the air is thick with smoke and chaos. Unreal Engine render style, photorealistic, realistic fantasy style."
37
+ },
38
+ {
39
+ "alias": "Forest Battle",
40
+ "text": "A fierce battle between two armies in a dense forest, with knights wielding swords and axes, horses rearing, and the ground covered in mud and blood. The enemy army, dressed in {enemy_color} armor, is locked in brutal combat, with soldiers fighting hand-to-hand amidst the trees. Arrows whiz past, and the sounds of clashing steel echo through the forest. Injured soldiers scream in pain, and the forest is littered with broken weapons and shields. Unreal Engine render style, photorealistic, realistic fantasy style."
41
+ },
42
+ {
43
+ "alias": "Boiling Oil Defense",
44
+ "text": "A dramatic moment in a medieval siege, with a knight leading a charge against a castle gate, while defenders pour boiling oil from the walls. The enemy army, dressed in {enemy_color} armor, is relentlessly attacking, with soldiers screaming as they are hit by the oil. Knights clash swords at the gate, and arrows rain down from above. The ground is littered with the bodies of fallen soldiers, and the air is filled with the smell of burning flesh. Unreal Engine render style, photorealistic, realistic fantasy style."
45
+ },
46
+ {
47
+ "alias": "Burning Castle Battle",
48
+ "text": "A chaotic battlefield with knights on horseback clashing with infantry, archers firing volleys of arrows, and a castle burning in the background. The enemy army, dressed in {enemy_color} armor, is fighting fiercely, with soldiers engaging in brutal melee combat. Flames light up the scene as knights charge through the chaos. Injured soldiers crawl on the ground, and the air is filled with the sounds of clashing steel and screams of pain. Unreal Engine render style, photorealistic, realistic fantasy style."
49
+ },
50
+ {
51
+ "alias": "Heroic Last Stand",
52
+ "text": "A heroic last stand of a small group of knights defending a bridge against a massive army, with arrows flying and swords clashing. The enemy army, dressed in {enemy_color} armor, is overwhelming the defenders, but the knights fight bravely, cutting down enemy soldiers as they advance. The bridge is littered with bodies and broken weapons. Blood stains the ground, and the air is thick with the sounds of battle. Unreal Engine render style, photorealistic, realistic fantasy style."
53
+ },
54
+ {
55
+ "alias": "Siege Tower Attack",
56
+ "text": "A medieval siege tower approaching a castle wall, with knights scaling ladders and defenders throwing rocks and shooting arrows. The enemy army, dressed in {enemy_color} armor, is fighting desperately to breach the walls, with soldiers clashing swords on the battlements. Arrows fly in all directions, and the siege tower is engulfed in flames. Injured soldiers fall from the ladders, and the ground is littered with the bodies of the fallen. Unreal Engine render style, photorealistic, realistic fantasy style."
57
+ },
58
+ {
59
+ "alias": "Knight Duel",
60
+ "text": "A dramatic duel between two knights in the middle of a battlefield, with their armies watching and the castle in the background. The enemy army, dressed in {enemy_color} armor, is engaged in fierce combat all around, with soldiers clashing swords and firing arrows. The duelists fight with skill and determination, their blades flashing in the sunlight. Injured soldiers lie on the ground, and the air is filled with the sounds of battle. Unreal Engine render style, photorealistic, realistic fantasy style."
61
+ },
62
+ {
63
+ "alias": "Night Battle",
64
+ "text": "A night battle during a medieval siege, with torches lighting the scene, knights fighting in the shadows, and the castle walls looming in the background. The enemy army, dressed in {enemy_color} armor, is locked in brutal combat, with soldiers clashing swords and firing arrows in the dim light. Flames from burning siege equipment illuminate the chaos. Injured soldiers scream in pain, and the ground is littered with the bodies of the fallen. Unreal Engine render style, photorealistic, realistic fantasy style."
65
+ },
66
+ {
67
+ "alias": "Marching Army",
68
+ "text": "A massive army of knights and infantry marching towards a distant castle, with banners flying and the sun setting behind them. The enemy army, dressed in {enemy_color} armor, is engaging in skirmishes along the way, with soldiers clashing swords and firing arrows. The battlefield is alive with the sounds of combat and the clash of steel. Injured soldiers lie on the ground, and the air is thick with the smell of blood and smoke. Unreal Engine render style, photorealistic, realistic fantasy style."
69
+ },
70
+ {
71
+ "alias": "Snowy Battlefield",
72
+ "text": "A medieval battle in a snowy landscape, with knights in heavy armor fighting on a frozen lake, and the castle visible in the distance. The enemy army, dressed in {enemy_color} armor, is locked in fierce combat, with soldiers slipping on the ice as they clash swords. Arrows fly through the air, and the snow is stained red with blood. Injured soldiers crawl on the ground, and the air is filled with the sounds of battle. Unreal Engine render style, photorealistic, realistic fantasy style."
73
+ }
 
 
 
 
 
 
 
 
74
  ]
75
 
76
+ # Dropdown menu for model selection
77
+ model_dropdown = widgets.Dropdown(
78
+ options=[(model["alias"], model["name"]) for model in models],
79
+ description="Select Model:",
80
+ style={"description_width": "initial"}
81
+ )
82
+
83
+ # Dropdown menu for prompt selection
84
+ prompt_dropdown = widgets.Dropdown(
85
+ options=[(prompt["alias"], prompt["text"]) for prompt in prompts],
86
+ description="Select Prompt:",
87
+ style={"description_width": "initial"}
88
+ )
89
+
90
+ # Dropdown menu for team selection
91
+ team_dropdown = widgets.Dropdown(
92
+ options=["Red", "Blue"],
93
+ description="Select Team:",
94
+ style={"description_width": "initial"}
95
+ )
96
+
97
+ # Input for height
98
+ height_input = widgets.IntText(
99
+ value=360,
100
+ description="Height:",
101
+ style={"description_width": "initial"}
102
+ )
103
+
104
+ # Input for width
105
+ width_input = widgets.IntText(
106
+ value=640,
107
+ description="Width:",
108
+ style={"description_width": "initial"}
109
+ )
110
+
111
+ # Input for number of inference steps
112
+ num_inference_steps_input = widgets.IntSlider(
113
+ value=20,
114
+ min=10,
115
+ max=100,
116
+ step=1,
117
+ description="Inference Steps:",
118
+ style={"description_width": "initial"}
119
+ )
120
+
121
+ # Input for guidance scale
122
+ guidance_scale_input = widgets.FloatSlider(
123
+ value=2,
124
+ min=1.0,
125
+ max=20.0,
126
+ step=0.5,
127
+ description="Guidance Scale:",
128
+ style={"description_width": "initial"}
129
+ )
130
+
131
+ # Input for seed
132
+ seed_input = widgets.IntText(
133
+ value=random.randint(0, 1000000),
134
+ description="Seed:",
135
+ style={"description_width": "initial"}
136
+ )
137
+
138
+ # Checkbox to randomize seed
139
+ randomize_seed_checkbox = widgets.Checkbox(
140
+ value=True,
141
+ description="Randomize Seed",
142
+ style={"description_width": "initial"}
143
+ )
144
+
145
+ # Button to generate image
146
+ generate_button = widgets.Button(
147
+ description="Generate Image",
148
+ button_style="success"
149
+ )
150
+
151
+ # Output area to display the image
152
+ output = widgets.Output()
153
+
154
+ # Function to generate images based on the selected prompt, team, and model
155
+ def generate_image(prompt, team, model_name, height, width, num_inference_steps, guidance_scale, seed):
156
+ # Determine the enemy color
157
+ enemy_color = "blue" if team.lower() == "red" else "red"
158
+
159
+ # Replace {enemy_color} in the prompt
160
+ prompt = prompt.format(enemy_color=enemy_color)
161
+
162
+ if team.lower() == "red":
163
+ prompt += " The winning army is dressed in red armor and banners."
164
+ elif team.lower() == "blue":
165
+ prompt += " The winning army is dressed in blue armor and banners."
166
+ else:
167
+ return "Invalid team selection. Please choose 'Red' or 'Blue'."
168
+
169
+ try:
170
+ # Randomize the seed if the checkbox is checked
171
+ if randomize_seed_checkbox.value:
172
+ seed = random.randint(0, 1000000)
173
+ seed_input.value = seed # Update the seed input box
174
+
175
+ print(f"Using seed: {seed}")
176
+
177
+ # Debug: Indicate that the image is being generated
178
+ print("Generating image... Please wait.")
179
+
180
+ # Initialize the InferenceClient with the selected model
181
+ client = InferenceClient(model_name, token=api_token)
182
+
183
+ # Generate the image using the Inference API with parameters
184
+ image = client.text_to_image(
185
  prompt,
186
+ guidance_scale=guidance_scale, # Guidance scale
187
+ num_inference_steps=num_inference_steps, # Number of inference steps
188
+ width=width, # Width
189
+ height=height, # Height
190
+ seed=seed # Random seed
191
+ )
192
+ return image
193
+ except Exception as e:
194
+ return f"An error occurred: {e}"
195
+
196
+ # Function to handle button click event
197
+ def on_generate_button_clicked(b):
198
+ with output:
199
+ clear_output(wait=True) # Clear previous output
200
+ selected_prompt = prompt_dropdown.value
201
+ selected_team = team_dropdown.value
202
+ selected_model = model_dropdown.value
203
+ height = height_input.value
204
+ width = width_input.value
205
+ num_inference_steps = num_inference_steps_input.value
206
+ guidance_scale = guidance_scale_input.value
207
+ seed = seed_input.value
208
+
209
+ # Debug: Show selected parameters
210
+ print(f"Selected Model: {model_dropdown.label}")
211
+ print(f"Selected Prompt: {prompt_dropdown.label}")
212
+ print(f"Selected Team: {selected_team}")
213
+ print(f"Height: {height}")
214
+ print(f"Width: {width}")
215
+ print(f"Inference Steps: {num_inference_steps}")
216
+ print(f"Guidance Scale: {guidance_scale}")
217
+ print(f"Seed: {seed}")
218
+
219
+ # Generate the image
220
+ image = generate_image(selected_prompt, selected_team, selected_model, height, width, num_inference_steps, guidance_scale, seed)
221
+
222
+ if isinstance(image, str):
223
+ print(image)
224
+ else:
225
+ # Debug: Indicate that the image is being displayed and saved
226
+ print("Image generated successfully!")
227
+ print("Displaying image...")
228
+
229
+ # Display the image in the notebook
230
+ display(image)
231
+
232
+ # Save the image with a timestamped filename
233
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
234
+ output_filename = f"{timestamp}_{model_dropdown.label.replace(' ', '_').lower()}_{prompt_dropdown.label.replace(' ', '_').lower()}_{selected_team.lower()}.png"
235
+ print(f"Saving image as {output_filename}...")
236
+ image.save(output_filename)
237
+ print(f"Image saved as {output_filename}")
238
+
239
+ # Attach the button click event handler
240
+ generate_button.on_click(on_generate_button_clicked)
241
+
242
+ # Display the widgets
243
+ #display(model_dropdown, prompt_dropdown, team_dropdown, height_input, width_input, num_inference_steps_input, guidance_scale_input, seed_input, randomize_seed_checkbox, generate_button, output)
244
+
245
+ display(prompt_dropdown, team_dropdown, generate_button, output)