Spaces:
Runtime error
Runtime error
updated app for making it a radio button to select between the experiment
Browse files
app.py
CHANGED
|
@@ -147,7 +147,6 @@ def generate_image(fish_name, masking_level_input,
|
|
| 147 |
# to image
|
| 148 |
grid = 255. * rearrange(grid, 'c h w -> h w c').cpu().numpy()
|
| 149 |
final_image = Image.fromarray(grid.astype(np.uint8))
|
| 150 |
-
# final_image.save(os.path.join(sample_path, f'{class_name.replace(" ", "-")}.png'))
|
| 151 |
|
| 152 |
return final_image
|
| 153 |
|
|
@@ -163,13 +162,6 @@ if __name__ == "__main__":
|
|
| 163 |
help="the prompt to render"
|
| 164 |
)
|
| 165 |
|
| 166 |
-
parser.add_argument(
|
| 167 |
-
"--outdir",
|
| 168 |
-
type=str,
|
| 169 |
-
nargs="?",
|
| 170 |
-
help="dir to write results to",
|
| 171 |
-
default="outputs/txt2img-samples"
|
| 172 |
-
)
|
| 173 |
parser.add_argument(
|
| 174 |
"--ddim_steps",
|
| 175 |
type=int,
|
|
@@ -196,20 +188,6 @@ if __name__ == "__main__":
|
|
| 196 |
help="sample this often",
|
| 197 |
)
|
| 198 |
|
| 199 |
-
parser.add_argument(
|
| 200 |
-
"--H",
|
| 201 |
-
type=int,
|
| 202 |
-
default=256,
|
| 203 |
-
help="image height, in pixel space",
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
parser.add_argument(
|
| 207 |
-
"--W",
|
| 208 |
-
type=int,
|
| 209 |
-
default=256,
|
| 210 |
-
help="image width, in pixel space",
|
| 211 |
-
)
|
| 212 |
-
|
| 213 |
parser.add_argument(
|
| 214 |
"--n_samples",
|
| 215 |
type=int,
|
|
@@ -217,20 +195,6 @@ if __name__ == "__main__":
|
|
| 217 |
help="how many samples to produce for the given prompt",
|
| 218 |
)
|
| 219 |
|
| 220 |
-
parser.add_argument(
|
| 221 |
-
"--output_dir_name",
|
| 222 |
-
type=str,
|
| 223 |
-
default='default_file',
|
| 224 |
-
help="name of folder",
|
| 225 |
-
)
|
| 226 |
-
|
| 227 |
-
parser.add_argument(
|
| 228 |
-
"--postfix",
|
| 229 |
-
type=str,
|
| 230 |
-
default='',
|
| 231 |
-
help="name of folder",
|
| 232 |
-
)
|
| 233 |
-
|
| 234 |
parser.add_argument(
|
| 235 |
"--scale",
|
| 236 |
type=float,
|
|
@@ -247,86 +211,97 @@ if __name__ == "__main__":
|
|
| 247 |
def load_example(prompt, level, option, components):
|
| 248 |
components['prompt_input'].value = prompt
|
| 249 |
components['masking_level_input'].value = level
|
| 250 |
-
|
| 251 |
-
|
| 252 |
def setup_interface():
|
| 253 |
with gr.Blocks() as demo:
|
| 254 |
-
|
| 255 |
gr.Markdown("# Phylo Diffusion - Generating Fish Images Tool")
|
| 256 |
gr.Markdown("### Write the Species name to generate a fish image")
|
| 257 |
-
gr.Markdown("###
|
| 258 |
-
|
| 259 |
-
|
| 260 |
with gr.Row():
|
| 261 |
with gr.Column():
|
| 262 |
gr.Markdown("## Generate Images Based on Prompts")
|
| 263 |
gr.Markdown("Enter a prompt to generate an image:")
|
| 264 |
prompt_input = gr.Textbox(label="Species Name")
|
| 265 |
-
|
| 266 |
-
with
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
gr.
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
| 274 |
submit_button = gr.Button("Generate")
|
|
|
|
| 275 |
gr.Markdown("## Phylogeny Tree")
|
| 276 |
architecture_image = "phylogeny_tree.jpg" # Update this with the actual path
|
| 277 |
gr.Image(value=architecture_image, label="Phylogeny Tree")
|
| 278 |
-
|
| 279 |
with gr.Column():
|
| 280 |
-
|
| 281 |
gr.Markdown("## Generated Image")
|
| 282 |
-
output_image = gr.Image(label="Generated Image",
|
| 283 |
-
|
| 284 |
-
|
| 285 |
# Place to put example buttons
|
| 286 |
gr.Markdown("## Select an example:")
|
| 287 |
examples = [
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
| 293 |
for text, level, swap_text, swap_level in examples:
|
| 294 |
if level == "None" and swap_text == "":
|
| 295 |
button = gr.Button(f"Species: {text}")
|
|
|
|
| 296 |
elif level != "None":
|
| 297 |
button = gr.Button(f"Species: {text} | Masking: {level}")
|
|
|
|
| 298 |
elif swap_text != "":
|
| 299 |
button = gr.Button(f"Species: {text} | Swapping with {swap_text} at {swap_level} ")
|
|
|
|
|
|
|
|
|
|
| 300 |
button.click(
|
| 301 |
-
fn=lambda text=text, level=level, swap_text=swap_text, swap_level=swap_level: (
|
|
|
|
|
|
|
| 302 |
inputs=[],
|
| 303 |
-
outputs=[prompt_input, masking_level_input, swap_fish_name, swap_level_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
)
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
submit_button.click(
|
| 311 |
fn=generate_image,
|
| 312 |
-
inputs=[prompt_input, masking_level_input,
|
| 313 |
-
swap_fish_name, swap_level_input],
|
| 314 |
outputs=output_image
|
| 315 |
)
|
| 316 |
-
|
| 317 |
-
return demo
|
| 318 |
-
|
| 319 |
-
# # Launch the interface
|
| 320 |
-
# iface = setup_interface()
|
| 321 |
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
# inputs=gr.Textbox(label="Prompt"),
|
| 325 |
-
# outputs=[
|
| 326 |
-
# gr.Image(label="Generated Image"),
|
| 327 |
-
# ]
|
| 328 |
-
# )
|
| 329 |
|
| 330 |
iface = setup_interface()
|
| 331 |
-
|
| 332 |
iface.launch(share=True)
|
|
|
|
| 147 |
# to image
|
| 148 |
grid = 255. * rearrange(grid, 'c h w -> h w c').cpu().numpy()
|
| 149 |
final_image = Image.fromarray(grid.astype(np.uint8))
|
|
|
|
| 150 |
|
| 151 |
return final_image
|
| 152 |
|
|
|
|
| 162 |
help="the prompt to render"
|
| 163 |
)
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
parser.add_argument(
|
| 166 |
"--ddim_steps",
|
| 167 |
type=int,
|
|
|
|
| 188 |
help="sample this often",
|
| 189 |
)
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
parser.add_argument(
|
| 192 |
"--n_samples",
|
| 193 |
type=int,
|
|
|
|
| 195 |
help="how many samples to produce for the given prompt",
|
| 196 |
)
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
parser.add_argument(
|
| 199 |
"--scale",
|
| 200 |
type=float,
|
|
|
|
| 211 |
def load_example(prompt, level, option, components):
|
| 212 |
components['prompt_input'].value = prompt
|
| 213 |
components['masking_level_input'].value = level
|
| 214 |
+
|
|
|
|
| 215 |
def setup_interface():
|
| 216 |
with gr.Blocks() as demo:
|
|
|
|
| 217 |
gr.Markdown("# Phylo Diffusion - Generating Fish Images Tool")
|
| 218 |
gr.Markdown("### Write the Species name to generate a fish image")
|
| 219 |
+
gr.Markdown("### Select one of the experiments: Trait Masking or Trait Swapping")
|
| 220 |
+
|
|
|
|
| 221 |
with gr.Row():
|
| 222 |
with gr.Column():
|
| 223 |
gr.Markdown("## Generate Images Based on Prompts")
|
| 224 |
gr.Markdown("Enter a prompt to generate an image:")
|
| 225 |
prompt_input = gr.Textbox(label="Species Name")
|
| 226 |
+
|
| 227 |
+
# Radio button to select experiment type, with no default selection
|
| 228 |
+
experiment_choice = gr.Radio(label="Select Experiment", choices=["Trait Masking", "Trait Swapping"], value=None)
|
| 229 |
+
|
| 230 |
+
# Trait Masking Inputs (hidden initially)
|
| 231 |
+
masking_level_input = gr.Dropdown(label="Select Ancestral Level", choices=["None", "Level 3", "Level 2"], value="None", visible=False)
|
| 232 |
+
|
| 233 |
+
# Trait Swapping Inputs (hidden initially)
|
| 234 |
+
swap_fish_name = gr.Textbox(label="Species Name to swap trait with:", visible=False)
|
| 235 |
+
swap_level_input = gr.Dropdown(label="Level of swapping", choices=["Level 3", "Level 2"], value="Level 3", visible=False)
|
| 236 |
+
|
| 237 |
submit_button = gr.Button("Generate")
|
| 238 |
+
|
| 239 |
gr.Markdown("## Phylogeny Tree")
|
| 240 |
architecture_image = "phylogeny_tree.jpg" # Update this with the actual path
|
| 241 |
gr.Image(value=architecture_image, label="Phylogeny Tree")
|
| 242 |
+
|
| 243 |
with gr.Column():
|
|
|
|
| 244 |
gr.Markdown("## Generated Image")
|
| 245 |
+
output_image = gr.Image(label="Generated Image", width=256, height=256)
|
| 246 |
+
|
|
|
|
| 247 |
# Place to put example buttons
|
| 248 |
gr.Markdown("## Select an example:")
|
| 249 |
examples = [
|
| 250 |
+
("Gambusia Affinis", "None", "", "Level 3"),
|
| 251 |
+
("Lepomis Auritus", "None", "", "Level 3"),
|
| 252 |
+
("Lepomis Auritus", "Level 3", "", "Level 3"),
|
| 253 |
+
("Noturus nocturnus", "None", "Notropis dorsalis", "Level 2")
|
| 254 |
+
]
|
| 255 |
+
|
| 256 |
for text, level, swap_text, swap_level in examples:
|
| 257 |
if level == "None" and swap_text == "":
|
| 258 |
button = gr.Button(f"Species: {text}")
|
| 259 |
+
experiment_type = "None"
|
| 260 |
elif level != "None":
|
| 261 |
button = gr.Button(f"Species: {text} | Masking: {level}")
|
| 262 |
+
experiment_type = "Trait Masking"
|
| 263 |
elif swap_text != "":
|
| 264 |
button = gr.Button(f"Species: {text} | Swapping with {swap_text} at {swap_level} ")
|
| 265 |
+
experiment_type = "Trait Swapping"
|
| 266 |
+
|
| 267 |
+
# Update radio button, fields and auto-trigger the "Generate" action
|
| 268 |
button.click(
|
| 269 |
+
fn=lambda text=text, level=level, swap_text=swap_text, swap_level=swap_level, experiment_type=experiment_type: (
|
| 270 |
+
text, level, swap_text, swap_level, experiment_type
|
| 271 |
+
),
|
| 272 |
inputs=[],
|
| 273 |
+
outputs=[prompt_input, masking_level_input, swap_fish_name, swap_level_input, experiment_choice]
|
| 274 |
+
).then(
|
| 275 |
+
fn=generate_image,
|
| 276 |
+
inputs=[prompt_input, masking_level_input, swap_fish_name, swap_level_input],
|
| 277 |
+
outputs=output_image
|
| 278 |
)
|
| 279 |
+
|
| 280 |
+
# Update visibility of inputs based on experiment selection
|
| 281 |
+
def update_inputs(experiment_type):
|
| 282 |
+
if experiment_type == "Trait Masking":
|
| 283 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 284 |
+
elif experiment_type == "Trait Swapping":
|
| 285 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
| 286 |
+
else:
|
| 287 |
+
# No experiment selected
|
| 288 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 289 |
+
|
| 290 |
+
experiment_choice.change(
|
| 291 |
+
fn=update_inputs,
|
| 292 |
+
inputs=[experiment_choice],
|
| 293 |
+
outputs=[masking_level_input, swap_fish_name, swap_level_input]
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
# Submit button functionality
|
| 297 |
submit_button.click(
|
| 298 |
fn=generate_image,
|
| 299 |
+
inputs=[prompt_input, masking_level_input, swap_fish_name, swap_level_input],
|
|
|
|
| 300 |
outputs=output_image
|
| 301 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
+
return demo
|
| 304 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
iface = setup_interface()
|
|
|
|
| 307 |
iface.launch(share=True)
|