Spaces:
Runtime error
Runtime error
File size: 9,553 Bytes
b5b4980 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import gradio as gr
from prompt_generator import PromptGenerator
from huggingface_inference_node import HuggingFaceInferenceNode
from caption_models import florence_caption, qwen_caption
import random
from prompt_generator import ARTFORM, PHOTO_TYPE, FEMALE_BODY_TYPES, MALE_BODY_TYPES, FEMALE_DEFAULT_TAGS, MALE_DEFAULT_TAGS, ROLES, HAIRSTYLES, FEMALE_CLOTHING, MALE_CLOTHING, PLACE, LIGHTING, COMPOSITION, POSE, BACKGROUND, FEMALE_ADDITIONAL_DETAILS, MALE_ADDITIONAL_DETAILS, PHOTOGRAPHY_STYLES, DEVICE, PHOTOGRAPHER, ARTIST, DIGITAL_ARTFORM
title = """<h1 align="center">FLUX Prompt Generator</h1>
<p><center>
<a href="https://x.com/gokayfem" target="_blank">[X gokaygokay]</a>
<a href="https://github.com/gokayfem" target="_blank">[Github gokayfem]</a>
<a href="https://github.com/dagthomas/comfyui_dagthomas" target="_blank">[comfyui_dagthomas]</a>
<p align="center">Create long prompts from images or simple words. Enhance your short prompts with prompt enhancer.</p>
</center></p>
"""
def create_interface():
prompt_generator = PromptGenerator()
huggingface_node = HuggingFaceInferenceNode()
with gr.Blocks(theme='bethecloud/storj_theme') as demo:
gr.HTML(title)
with gr.Row():
with gr.Column(scale=2):
with gr.Accordion("Basic Settings"):
custom = gr.Textbox(label="Custom Input Prompt (optional)")
subject = gr.Textbox(label="Subject (optional)")
gender = gr.Radio(["female", "male"], label="Gender", value="female")
global_option = gr.Radio(
["Disabled", "Random", "No Figure Rand"],
label="Set all options to:",
value="Disabled"
)
with gr.Accordion("Artform and Photo Type", open=False):
artform = gr.Dropdown(["disabled", "random"] + ARTFORM, label="Artform", value="disabled")
photo_type = gr.Dropdown(["disabled", "random"] + PHOTO_TYPE, label="Photo Type", value="disabled")
with gr.Accordion("Character Details", open=False):
body_types = gr.Dropdown(["disabled", "random"] + FEMALE_BODY_TYPES + MALE_BODY_TYPES, label="Body Types", value="disabled")
default_tags = gr.Dropdown(["disabled", "random"] + FEMALE_DEFAULT_TAGS + MALE_DEFAULT_TAGS, label="Default Tags", value="disabled")
roles = gr.Dropdown(["disabled", "random"] + ROLES, label="Roles", value="disabled")
hairstyles = gr.Dropdown(["disabled", "random"] + HAIRSTYLES, label="Hairstyles", value="disabled")
clothing = gr.Dropdown(["disabled", "random"] + FEMALE_CLOTHING + MALE_CLOTHING, label="Clothing", value="disabled")
with gr.Accordion("Scene Details", open=False):
place = gr.Dropdown(["disabled", "random"] + PLACE, label="Place", value="disabled")
lighting = gr.Dropdown(["disabled", "random"] + LIGHTING, label="Lighting", value="disabled")
composition = gr.Dropdown(["disabled", "random"] + COMPOSITION, label="Composition", value="disabled")
pose = gr.Dropdown(["disabled", "random"] + POSE, label="Pose", value="disabled")
background = gr.Dropdown(["disabled", "random"] + BACKGROUND, label="Background", value="disabled")
with gr.Accordion("Style and Artist", open=False):
additional_details = gr.Dropdown(["disabled", "random"] + FEMALE_ADDITIONAL_DETAILS + MALE_ADDITIONAL_DETAILS, label="Additional Details", value="disabled")
photography_styles = gr.Dropdown(["disabled", "random"] + PHOTOGRAPHY_STYLES, label="Photography Styles", value="disabled")
device = gr.Dropdown(["disabled", "random"] + DEVICE, label="Device", value="disabled")
photographer = gr.Dropdown(["disabled", "random"] + PHOTOGRAPHER, label="Photographer", value="disabled")
artist = gr.Dropdown(["disabled", "random"] + ARTIST, label="Artist", value="disabled")
digital_artform = gr.Dropdown(["disabled", "random"] + DIGITAL_ARTFORM, label="Digital Artform", value="disabled")
generate_button = gr.Button("Generate Prompt")
with gr.Column(scale=2):
with gr.Accordion("Image and Caption", open=False):
input_image = gr.Image(label="Input Image (optional)")
caption_output = gr.Textbox(label="Generated Caption", lines=3)
caption_model = gr.Radio(["Florence-2", "Qwen2-VL"], label="Caption Model", value="Florence-2")
create_caption_button = gr.Button("Create Caption")
add_caption_button = gr.Button("Add Caption to Prompt")
with gr.Accordion("Prompt Generation", open=True):
output = gr.Textbox(label="Generated Prompt / Input Text", lines=4)
t5xxl_output = gr.Textbox(label="T5XXL Output", visible=True)
clip_l_output = gr.Textbox(label="CLIP L Output", visible=True)
clip_g_output = gr.Textbox(label="CLIP G Output", visible=True)
with gr.Column(scale=2):
with gr.Accordion("Prompt Generation with LLM", open=False):
happy_talk = gr.Checkbox(label="Happy Talk", value=True)
compress = gr.Checkbox(label="Compress", value=True)
compression_level = gr.Radio(["soft", "medium", "hard"], label="Compression Level", value="hard")
poster = gr.Checkbox(label="Poster", value=False)
custom_base_prompt = gr.Textbox(label="Custom Base Prompt", lines=5)
generate_text_button = gr.Button("Generate Prompt with LLM (Llama 3.1 70B)")
text_output = gr.Textbox(label="Generated Text", lines=10)
def create_caption(image, model):
if image is not None:
if model == "Florence-2":
return florence_caption(image)
elif model == "Qwen2-VL":
return qwen_caption(image)
return ""
create_caption_button.click(
create_caption,
inputs=[input_image, caption_model],
outputs=[caption_output]
)
def generate_prompt_with_dynamic_seed(*args):
dynamic_seed = random.randint(0, 1000000)
result = prompt_generator.generate_prompt(dynamic_seed, *args)
return [dynamic_seed] + list(result)
generate_button.click(
generate_prompt_with_dynamic_seed,
inputs=[custom, subject, gender, artform, photo_type, body_types, default_tags, roles, hairstyles,
additional_details, photography_styles, device, photographer, artist, digital_artform,
place, lighting, clothing, composition, pose, background, input_image],
outputs=[gr.Number(label="Used Seed", visible=True), output, gr.Number(visible=False), t5xxl_output, clip_l_output, clip_g_output]
)
add_caption_button.click(
prompt_generator.add_caption_to_prompt,
inputs=[output, caption_output],
outputs=[output]
)
generate_text_button.click(
huggingface_node.generate,
inputs=[output, happy_talk, compress, compression_level, poster, custom_base_prompt],
outputs=text_output
)
def update_all_options(choice):
updates = {}
if choice == "Disabled":
for dropdown in [
artform, photo_type, body_types, default_tags, roles, hairstyles, clothing,
place, lighting, composition, pose, background, additional_details,
photography_styles, device, photographer, artist, digital_artform
]:
updates[dropdown] = gr.update(value="disabled")
elif choice == "Random":
for dropdown in [
artform, photo_type, body_types, default_tags, roles, hairstyles, clothing,
place, lighting, composition, pose, background, additional_details,
photography_styles, device, photographer, artist, digital_artform
]:
updates[dropdown] = gr.update(value="random")
else: # No Figure Random
for dropdown in [photo_type, body_types, default_tags, roles, hairstyles, clothing, pose, additional_details]:
updates[dropdown] = gr.update(value="disabled")
for dropdown in [artform, place, lighting, composition, background, photography_styles, device, photographer, artist, digital_artform]:
updates[dropdown] = gr.update(value="random")
return updates
global_option.change(
update_all_options,
inputs=[global_option],
outputs=[
artform, photo_type, body_types, default_tags, roles, hairstyles, clothing,
place, lighting, composition, pose, background, additional_details,
photography_styles, device, photographer, artist, digital_artform
]
)
return demo |