import gradio as gr import numpy as np import random import spaces import torch from diffusers import DiffusionPipeline import importlib # to import tag modules dynamically device = "cuda" if torch.cuda.is_available() else "cpu" model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl" # Replace with your desired model if torch.cuda.is_available(): torch_dtype = torch.float16 else: torch_dtype = torch.float32 pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype) pipe.to(device) MAX_SEED = np.iinfo(np.int32).max MAX_IMAGE_SIZE = 1024 # Function to load tags dynamically based on the selected tab def load_tags(active_tab): if active_tab == "Gay": tags_module = importlib.import_module('tags_gay') # dynamically import the tags_gay module elif active_tab == "Straight": tags_module = importlib.import_module('tags_straight') # dynamically import the tags_straight module elif active_tab == "Lesbian": tags_module = importlib.import_module('tags_lesbian') # dynamically import the tags_lesbian module else: raise ValueError(f"Unknown tab: {active_tab}") return tags_module @spaces.GPU def infer( prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, selected_participant_tags, selected_tribe_tags, selected_role_tags, selected_skin_tone_tags, selected_body_type_tags, selected_tattoo_tags, selected_piercing_tags, selected_expression_tags, selected_eye_tags, selected_hair_style_tags, selected_position_tags, selected_fetish_tags, selected_location_tags, selected_camera_tags, selected_atmosphere_tags, active_tab, progress=gr.Progress(track_tqdm=True) ): # Dynamically load the correct tags module based on active tab tags_module = load_tags(active_tab) # Now use the tags from the loaded module participant_tags = tags_module.participant_tags tribe_tags = tags_module.tribe_tags role_tags = tags_module.role_tags skin_tone_tags = tags_module.skin_tone_tags body_type_tags = tags_module.body_type_tags tattoo_tags = tags_module.tattoo_tags piercing_tags = tags_module.piercing_tags expression_tags = tags_module.expression_tags eye_tags = tags_module.eye_tags hair_style_tags = tags_module.hair_style_tags position_tags = tags_module.position_tags fetish_tags = tags_module.fetish_tags location_tags = tags_module.location_tags camera_tags = tags_module.camera_tags atmosphere_tags = tags_module.atmosphere_tags # Handle the active tab and generate the prompt accordingly tag_list = ( [participant_tags[tag] for tag in selected_participant_tags] + [tribe_tags[tag] for tag in selected_tribe_tags] + [role_tags[tag] for tag in selected_role_tags] + [skin_tone_tags[tag] for tag in selected_skin_tone_tags] + [body_type_tags[tag] for tag in selected_body_type_tags] + [tattoo_tags[tag] for tag in selected_tattoo_tags] + [piercing_tags[tag] for tag in selected_piercing_tags] + [expression_tags[tag] for tag in selected_expression_tags] + [eye_tags[tag] for tag in selected_eye_tags] + [hair_style_tags[tag] for tag in selected_hair_style_tags] + [position_tags[tag] for tag in selected_position_tags] + [fetish_tags[tag] for tag in selected_fetish_tags] + [location_tags[tag] for tag in selected_location_tags] + [camera_tags[tag] for tag in selected_camera_tags] + [atmosphere_tags[tag] for tag in selected_atmosphere_tags] ) final_prompt = f"score_9, score_8_up, score_7_up, source_anime, {', '.join(tag_list)}" # Concatenate additional negative prompts additional_negatives = "worst quality, bad quality, jpeg artifacts, source_cartoon, 3d, (censor), monochrome, blurry, lowres, watermark" full_negative_prompt = f"{additional_negatives}, {negative_prompt}" if randomize_seed: seed = random.randint(0, MAX_SEED) generator = torch.Generator(device=device).manual_seed(seed) image = pipe( prompt=final_prompt, negative_prompt=full_negative_prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, width=width, height=height, generator=generator ).images[0] return image, seed, f"Prompt: {final_prompt}\nNegative Prompt: {full_negative_prompt}" # CSS for the layout css = """ #col-container { margin: 0 auto; max-width: 640px; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.Markdown("# Image Generator with Tags and Prompts") result = gr.Image(label="Result", show_label=False) prompt_info = gr.Textbox(label="Prompts Used", lines=3, interactive=False) active_tab = gr.State("Prompt Input") # State variables for selected tags for each tab selected_participant_tags_straight = gr.State([]) selected_tribe_tags_straight = gr.State([]) selected_role_tags_straight = gr.State([]) selected_skin_tone_tags_straight = gr.State([]) selected_body_type_tags_straight = gr.State([]) selected_tattoo_tags_straight = gr.State([]) selected_piercing_tags_straight = gr.State([]) selected_expression_tags_straight = gr.State([]) selected_eye_tags_straight = gr.State([]) selected_hair_style_tags_straight = gr.State([]) selected_position_tags_straight = gr.State([]) selected_fetish_tags_straight = gr.State([]) selected_location_tags_straight = gr.State([]) selected_camera_tags_straight = gr.State([]) selected_atmosphere_tags_straight = gr.State([]) selected_participant_tags_gay = gr.State([]) selected_tribe_tags_gay = gr.State([]) selected_role_tags_gay = gr.State([]) selected_skin_tone_tags_gay = gr.State([]) selected_body_type_tags_gay = gr.State([]) selected_tattoo_tags_gay = gr.State([]) selected_piercing_tags_gay = gr.State([]) selected_expression_tags_gay = gr.State([]) selected_eye_tags_gay = gr.State([]) selected_hair_style_tags_gay = gr.State([]) selected_position_tags_gay = gr.State([]) selected_fetish_tags_gay = gr.State([]) selected_location_tags_gay = gr.State([]) selected_camera_tags_gay = gr.State([]) selected_atmosphere_tags_gay = gr.State([]) selected_participant_tags_lesbian = gr.State([]) selected_tribe_tags_lesbian = gr.State([]) selected_role_tags_lesbian = gr.State([]) selected_skin_tone_tags_lesbian = gr.State([]) selected_body_type_tags_lesbian = gr.State([]) selected_tattoo_tags_lesbian = gr.State([]) selected_piercing_tags_lesbian = gr.State([]) selected_expression_tags_lesbian = gr.State([]) selected_eye_tags_lesbian = gr.State([]) selected_hair_style_tags_lesbian = gr.State([]) selected_position_tags_lesbian = gr.State([]) selected_fetish_tags_lesbian = gr.State([]) selected_location_tags_lesbian = gr.State([]) selected_camera_tags_lesbian = gr.State([]) selected_atmosphere_tags_lesbian = gr.State([]) with gr.Tabs() as tabs: # Prompt Input Tab with gr.TabItem("Prompt Input"): prompt = gr.Textbox(label="Prompt", placeholder="Enter your custom prompt") tabs.select(lambda: "Prompt Input", inputs=None, outputs=active_tab) with gr.TabItem("Straight"): # Dynamically load the tags for the "Straight" tab tags_module = load_tags("Straight") selected_participant_tags_straight = gr.CheckboxGroup(choices=list(tags_module.participant_tags.keys()), label="Participant Tags") selected_tribe_tags_straight = gr.CheckboxGroup(choices=list(tags_module.tribe_tags.keys()), label="Tribe Tags") selected_role_tags_straight = gr.CheckboxGroup(choices=list(tags_module.role_tags.keys()), label="Role Tags") selected_skin_tone_tags_straight = gr.CheckboxGroup(choices=list(tags_module.skin_tone_tags.keys()), label="Skin Tone Tags") selected_body_type_tags_straight = gr.CheckboxGroup(choices=list(tags_module.body_type_tags.keys()), label="Body Type Tags") selected_tattoo_tags_straight = gr.CheckboxGroup(choices=list(tags_module.tattoo_tags.keys()), label="Tattoo Tags") selected_piercing_tags_straight = gr.CheckboxGroup(choices=list(tags_module.piercing_tags.keys()), label="Piercing Tags") selected_expression_tags_straight = gr.CheckboxGroup(choices=list(tags_module.expression_tags.keys()), label="Expression Tags") selected_eye_tags_straight = gr.CheckboxGroup(choices=list(tags_module.eye_tags.keys()), label="Eye Tags") selected_hair_style_tags_straight = gr.CheckboxGroup(choices=list(tags_module.hair_style_tags.keys()), label="Hair Style Tags") selected_position_tags_straight = gr.CheckboxGroup(choices=list(tags_module.position_tags.keys()), label="Position Tags") selected_fetish_tags_straight = gr.CheckboxGroup(choices=list(tags_module.fetish_tags.keys()), label="Fetish Tags") selected_location_tags_straight = gr.CheckboxGroup(choices=list(tags_module.location_tags.keys()), label="Location Tags") selected_camera_tags_straight = gr.CheckboxGroup(choices=list(tags_module.camera_tags.keys()), label="Camera Tags") selected_atmosphere_tags_straight = gr.CheckboxGroup(choices=list(tags_module.atmosphere_tags.keys()), label="Atmosphere Tags") tabs.select(lambda: "Straight", inputs=None, outputs=active_tab) # Repeat the above block for the "Gay" and "Lesbian" tabs. # Advanced Settings with gr.Accordion("Advanced Settings", open=False): negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter negative prompt") seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0) randomize_seed = gr.Checkbox(label="Randomize Seed", value=True) with gr.Row(): width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) with gr.Row(): guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=7) num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=1, maximum=50, step=1, value=35) run_button = gr.Button("Run") run_button.click( infer, inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, selected_participant_tags_straight, selected_tribe_tags_straight, selected_role_tags_straight, selected_skin_tone_tags_straight, selected_body_type_tags_straight, selected_tattoo_tags_straight, selected_piercing_tags_straight, selected_expression_tags_straight, selected_eye_tags_straight, selected_hair_style_tags_straight, selected_position_tags_straight, selected_fetish_tags_straight, selected_location_tags_straight, selected_camera_tags_straight, selected_atmosphere_tags_straight, active_tab], outputs=[result, seed, prompt_info] ) demo.queue().launch()