import gradio as gr from random import randint from all_models import models def load_models(model_names): loaded_models = {} for model_name in model_names: try: model = gr.load(f'models/{model_name}') except Exception: model = gr.Interface(lambda txt: "Model load error", inputs=["text"], outputs=["image"]) loaded_models[model_name] = model return loaded_models models_load = load_models(models) num_models = 6 default_models = models[:num_models] def extend_choices(choices, num_models=6): return choices + ['NA'] * (num_models - len(choices)) def update_image_boxes(choices): extended_choices = extend_choices(choices) return [gr.Image(label=model, visible=(model != 'NA')).style(padding="10px", border_radius="10px", box_shadow="0px 5px 20px rgba(0, 0, 0, 0.1)", transition="transform 0.3s ease-in-out") for model in extended_choices] def generate_image(model_name, prompt): if model_name == 'NA': return None # If you decide to add randomness to the prompt, uncomment the next line # prompt += f" {randint(0, 999999999)}" return models_load[model_name](prompt) with gr.Blocks() as demo: with gr.Tab("The Dream").style(padding="20px", border_radius="15px", box_shadow="0px 10px 40px rgba(0, 0, 0, 0.1)", transition="transform 0.3s ease-in-out"): txt_input = gr.Textbox(label="Your prompt:", lines=4).style(container=False, min_width=1200, border="2px solid #ccc", border_radius="10px", padding="10px", box_shadow="0px 3px 10px rgba(0, 0, 0, 0.1)") gen_button = gr.Button("Generate up to 6 images").style(background="#4CAF50", color="white", padding="10px 20px", border="none", border_radius="5px", cursor="pointer", transition="background 0.3s ease-in-out") stop_button = gr.Button("Stop", variant="secondary").style(background="#f44336", color="white", padding="10px 20px", border="none", border_radius="5px", cursor="pointer", transition="background 0.3s ease-in-out") gen_button.click(lambda _: gr.update(interactive=True), None, stop_button) with gr.Row(): output_images = [gr.Image(label=model, min_width=480) for model in default_models] model_textboxes = [gr.Textbox(model, visible=False) for model in default_models] for model_textbox, output_image in zip(model_textboxes, output_images): gen_event = gen_button.click(generate_image, [model_textbox, txt_input], output_image) stop_button.click(lambda _: gr.update(interactive=False), None, stop_button, cancels=[gen_event]) with gr.Accordion("Model selection").style(padding="20px", border_radius="10px", box_shadow="0px 5px 20px rgba(0, 0, 0, 0.1)", transition="transform 0.3s ease-in-out"): model_choice = gr.CheckboxGroup(choices=models, label=f"Choose up to {num_models} models from the 686 available!", value=default_models, multiselect=True, max_choices=num_models, interactive=True).style(margin="10px 0", padding="10px", border_radius="5px", background="#f9f9f9", box_shadow="0px 3px 10px rgba(0, 0, 0, 0.1)") model_choice.change(update_image_boxes, model_choice, output_images) model_choice.change(lambda choices: extend_choices(choices, num_models), model_choice, model_textboxes) with gr.Row(): gr.HTML(""" """).style(text_align="center", margin="20px 0") demo.queue(concurrency_count=200) demo.launch()