import json import random import gradio as gr import modules from pathlib import Path from modules import script_callbacks import modules.scripts as scripts result_prompt = "" base_dir = scripts.basedir() dropdown_options_file = Path(base_dir, "json/dropdown_options.json") category_data_file = Path(base_dir, "json/category_data.json") style_data_file = Path(base_dir, "json/style_data.json") prefix_data_file = Path(base_dir, "json/prefix_data.json") lightning_data_file = Path(base_dir, "json/lightning_data.json") lens_data_file = Path(base_dir, "json/lens_data.json") class Model: ''' Small strut to hold data for the text generator ''' def __init__(self, name) -> None: self.name = name pass def populate_dropdown_options(): path = dropdown_options_file with open(path, 'r') as f: data = json.load(f) category_choices = data["category"] style_choices = data["style"] lightning_choices = data["lightning"] lens_choices = data["lens"] return tuple(category_choices), tuple(style_choices), tuple(lightning_choices), tuple(lens_choices), def add_to_prompt(*args): prompt, use_default_negative_prompt = args default_negative_prompt = "(worst quality:1.2), (low quality:1.2), (lowres:1.1), (monochrome:1.1), (greyscale), multiple views, comic, sketch, (((bad anatomy))), (((deformed))), (((disfigured))), watermark, multiple_views, mutation hands, mutation fingers, extra fingers, missing fingers, watermark" if(use_default_negative_prompt): return prompt, default_negative_prompt else: return prompt, "" def get_random_prompt(data): random_key = random.choice(list(data.keys())) random_array = random.choice(data[random_key]) random_strings = random.sample(random_array, 3) return random_strings def get_correct_prompt(data, selected_dropdown): correct_array = data[selected_dropdown] random_array = random.choice(correct_array) random_strings = random.sample(random_array, 3) random_strings.insert(0, selected_dropdown) return random_strings def generate_prompt_output(*args): #all imported files prefix_path = prefix_data_file category_path = category_data_file style_path = style_data_file lightning_path = lightning_data_file lens_path = lens_data_file #destructure args category, style, lightning, lens, negative_prompt = args # Convert variables to lowercase category = category.lower() style = style.lower() lightning = lightning.lower() lens = lens.lower() # Open category_data.json and grab correct text with open(prefix_path, 'r') as f: prefix_data = json.load(f) prefix_prompt = random.sample(prefix_data, 6) modified_prefix_prompt = [f"(({item}))" for item in prefix_prompt] # Open category_data.json and grab correct text with open(category_path, 'r') as f2: category_data = json.load(f2) if category == "none": category_prompt = "" elif category == "random": category_prompt = get_random_prompt(category_data) else: category_prompt = get_correct_prompt(category_data, category) # Open style_data.json and grab correct text with open(style_path, 'r') as f3: style_data = json.load(f3) if style == "none": style_prompt = "" elif style == "random": style_prompt = get_random_prompt(style_data) else: style_prompt = get_correct_prompt(style_data, style) # Open lightning_data.json and grab correct text with open(lightning_path, 'r') as f4: lightning_data = json.load(f4) if lightning == "none": lightning_prompt = "" elif lightning == "random": lightning_prompt = get_random_prompt(lightning_data) else: lightning_prompt = get_correct_prompt(lightning_data, lightning) # Open lens_data.json and grab correct text with open(lens_path, 'r') as f5: lens_data = json.load(f5) if lens == "none": lens_prompt = "" elif lens == "random": lens_prompt = get_random_prompt(lens_data) else: lens_prompt = get_correct_prompt(lens_data, lens) prompt_output = modified_prefix_prompt, category_prompt, style_prompt, lightning_prompt, lens_prompt prompt_strings = [] for sublist in prompt_output: # Join the sublist elements into a single string prompt_string = ", ".join(str(item) for item in sublist) if prompt_string: # Check if the prompt_string is not empty prompt_strings.append(prompt_string) # Join the non-empty prompt_strings final_output = ", ".join(prompt_strings) return final_output def on_ui_tabs(): # UI structure txt2img_prompt = modules.ui.txt2img_paste_fields[0][0] img2img_prompt = modules.ui.img2img_paste_fields[0][0] txt2img_negative_prompt = modules.ui.txt2img_paste_fields[1][0] img2img_negative_prompt = modules.ui.img2img_paste_fields[1][0] with gr.Blocks(analytics_enabled=False) as prompt_generator: with gr.Tab("Prompt Generator"): with gr.Row(): # Use Row to arrange two columns side by side with gr.Column(): # Left column for dropdowns category_choices, style_choices, lightning_choices, lens_choices = populate_dropdown_options() with gr.Row(): gr.HTML('''