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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import random
|
3 |
+
import gradio as gr
|
4 |
+
#import modules
|
5 |
+
from pathlib import Path
|
6 |
+
#from modules import script_callbacks
|
7 |
+
#import modules.scripts as scripts
|
8 |
+
|
9 |
+
#result_prompt = ""
|
10 |
+
#base_dir = scripts.basedir()
|
11 |
+
base_dir = "."
|
12 |
+
dropdown_options_file = Path(base_dir, "json/dropdown_options.json")
|
13 |
+
category_data_file = Path(base_dir, "json/category_data.json")
|
14 |
+
style_data_file = Path(base_dir, "json/style_data.json")
|
15 |
+
prefix_data_file = Path(base_dir, "json/prefix_data.json")
|
16 |
+
lightning_data_file = Path(base_dir, "json/lightning_data.json")
|
17 |
+
lens_data_file = Path(base_dir, "json/lens_data.json")
|
18 |
+
|
19 |
+
|
20 |
+
class Model:
|
21 |
+
'''
|
22 |
+
Small strut to hold data for the text generator
|
23 |
+
'''
|
24 |
+
|
25 |
+
def __init__(self, name) -> None:
|
26 |
+
self.name = name
|
27 |
+
pass
|
28 |
+
|
29 |
+
|
30 |
+
def populate_dropdown_options():
|
31 |
+
path = dropdown_options_file
|
32 |
+
with open(path, 'r') as f:
|
33 |
+
data = json.load(f)
|
34 |
+
category_choices = data["category"]
|
35 |
+
style_choices = data["style"]
|
36 |
+
lightning_choices = data["lightning"]
|
37 |
+
lens_choices = data["lens"]
|
38 |
+
return tuple(category_choices), tuple(style_choices), tuple(lightning_choices), tuple(lens_choices),
|
39 |
+
|
40 |
+
|
41 |
+
'''
|
42 |
+
def add_to_prompt(*args):
|
43 |
+
prompt, use_default_negative_prompt = args
|
44 |
+
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"
|
45 |
+
if(use_default_negative_prompt):
|
46 |
+
return prompt, default_negative_prompt
|
47 |
+
else:
|
48 |
+
return prompt, ""
|
49 |
+
'''
|
50 |
+
def add_to_prompt(*args):
|
51 |
+
prompt, use_default_negative_prompt, base_prompt, negative_base_prompt = args
|
52 |
+
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"
|
53 |
+
if(use_default_negative_prompt):
|
54 |
+
return "{} {}".format(base_prompt ,prompt), default_negative_prompt
|
55 |
+
else:
|
56 |
+
return "{} {}".format(base_prompt ,prompt), ""
|
57 |
+
|
58 |
+
|
59 |
+
def get_random_prompt(data):
|
60 |
+
random_key = random.choice(list(data.keys()))
|
61 |
+
random_array = random.choice(data[random_key])
|
62 |
+
random_strings = random.sample(random_array, 3)
|
63 |
+
return random_strings
|
64 |
+
|
65 |
+
def get_correct_prompt(data, selected_dropdown):
|
66 |
+
correct_array = data[selected_dropdown]
|
67 |
+
random_array = random.choice(correct_array)
|
68 |
+
random_strings = random.sample(random_array, 3)
|
69 |
+
random_strings.insert(0, selected_dropdown)
|
70 |
+
|
71 |
+
return random_strings
|
72 |
+
|
73 |
+
def generate_prompt_output(*args):
|
74 |
+
#all imported files
|
75 |
+
prefix_path = prefix_data_file
|
76 |
+
category_path = category_data_file
|
77 |
+
style_path = style_data_file
|
78 |
+
lightning_path = lightning_data_file
|
79 |
+
lens_path = lens_data_file
|
80 |
+
|
81 |
+
#destructure args
|
82 |
+
category, style, lightning, lens, negative_prompt = args
|
83 |
+
|
84 |
+
# Convert variables to lowercase
|
85 |
+
category = category.lower()
|
86 |
+
style = style.lower()
|
87 |
+
lightning = lightning.lower()
|
88 |
+
lens = lens.lower()
|
89 |
+
|
90 |
+
# Open category_data.json and grab correct text
|
91 |
+
with open(prefix_path, 'r') as f:
|
92 |
+
prefix_data = json.load(f)
|
93 |
+
prefix_prompt = random.sample(prefix_data, 6)
|
94 |
+
modified_prefix_prompt = [f"(({item}))" for item in prefix_prompt]
|
95 |
+
|
96 |
+
|
97 |
+
# Open category_data.json and grab correct text
|
98 |
+
with open(category_path, 'r') as f2:
|
99 |
+
category_data = json.load(f2)
|
100 |
+
|
101 |
+
if category == "none":
|
102 |
+
category_prompt = ""
|
103 |
+
elif category == "random":
|
104 |
+
category_prompt = get_random_prompt(category_data)
|
105 |
+
else:
|
106 |
+
category_prompt = get_correct_prompt(category_data, category)
|
107 |
+
|
108 |
+
|
109 |
+
# Open style_data.json and grab correct text
|
110 |
+
with open(style_path, 'r') as f3:
|
111 |
+
style_data = json.load(f3)
|
112 |
+
|
113 |
+
if style == "none":
|
114 |
+
style_prompt = ""
|
115 |
+
elif style == "random":
|
116 |
+
style_prompt = get_random_prompt(style_data)
|
117 |
+
else:
|
118 |
+
style_prompt = get_correct_prompt(style_data, style)
|
119 |
+
|
120 |
+
# Open lightning_data.json and grab correct text
|
121 |
+
with open(lightning_path, 'r') as f4:
|
122 |
+
lightning_data = json.load(f4)
|
123 |
+
|
124 |
+
if lightning == "none":
|
125 |
+
lightning_prompt = ""
|
126 |
+
elif lightning == "random":
|
127 |
+
lightning_prompt = get_random_prompt(lightning_data)
|
128 |
+
else:
|
129 |
+
lightning_prompt = get_correct_prompt(lightning_data, lightning)
|
130 |
+
|
131 |
+
# Open lens_data.json and grab correct text
|
132 |
+
with open(lens_path, 'r') as f5:
|
133 |
+
lens_data = json.load(f5)
|
134 |
+
|
135 |
+
if lens == "none":
|
136 |
+
lens_prompt = ""
|
137 |
+
elif lens == "random":
|
138 |
+
lens_prompt = get_random_prompt(lens_data)
|
139 |
+
else:
|
140 |
+
lens_prompt = get_correct_prompt(lens_data, lens)
|
141 |
+
|
142 |
+
|
143 |
+
prompt_output = modified_prefix_prompt, category_prompt, style_prompt, lightning_prompt, lens_prompt
|
144 |
+
prompt_strings = []
|
145 |
+
|
146 |
+
for sublist in prompt_output:
|
147 |
+
# Join the sublist elements into a single string
|
148 |
+
prompt_string = ", ".join(str(item) for item in sublist)
|
149 |
+
if prompt_string: # Check if the prompt_string is not empty
|
150 |
+
prompt_strings.append(prompt_string)
|
151 |
+
|
152 |
+
# Join the non-empty prompt_strings
|
153 |
+
final_output = ", ".join(prompt_strings)
|
154 |
+
|
155 |
+
return final_output
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
def on_ui_tabs():
|
160 |
+
'''
|
161 |
+
# UI structure
|
162 |
+
txt2img_prompt = modules.ui.txt2img_paste_fields[0][0]
|
163 |
+
img2img_prompt = modules.ui.img2img_paste_fields[0][0]
|
164 |
+
|
165 |
+
txt2img_negative_prompt = modules.ui.txt2img_paste_fields[1][0]
|
166 |
+
img2img_negative_prompt = modules.ui.img2img_paste_fields[1][0]
|
167 |
+
'''
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
#with gr.Blocks(analytics_enabled=False) as prompt_generator:
|
172 |
+
with gr.Blocks(css = '''
|
173 |
+
.header img {
|
174 |
+
float: middle;
|
175 |
+
width: 33px;
|
176 |
+
height: 33px;
|
177 |
+
}
|
178 |
+
.header h1 {
|
179 |
+
top: 18px;
|
180 |
+
left: 10px;
|
181 |
+
}
|
182 |
+
'''
|
183 |
+
) as prompt_generator:
|
184 |
+
gr.HTML(
|
185 |
+
'''
|
186 |
+
<center>
|
187 |
+
<div class="header">
|
188 |
+
<h1 class = "logo"> <img src="https://huggingface.co/spaces/svjack/Next-Diffusion-Prompt-Generator/resolve/main/images/nextdiffusion_logo.png" alt="logo" /> Next Diffusion Prompt Generator Extension </h1>
|
189 |
+
</center>
|
190 |
+
''')
|
191 |
+
|
192 |
+
with gr.Tab("Prompt Generator"):
|
193 |
+
with gr.Row(): # Use Row to arrange two columns side by side
|
194 |
+
with gr.Column(): # Left column for dropdowns
|
195 |
+
category_choices, style_choices, lightning_choices, lens_choices = populate_dropdown_options()
|
196 |
+
|
197 |
+
with gr.Row():
|
198 |
+
gr.HTML('''<h2 id="input_header">Input 👇</h2>''')
|
199 |
+
with gr.Row():
|
200 |
+
# Create a dropdown to select
|
201 |
+
with gr.Row():
|
202 |
+
txt2img_prompt = gr.Textbox(label = "txt2img_prompt", interactive = True)
|
203 |
+
txt2img_negative_prompt = gr.Textbox(label = "txt2img_negative_prompt", interactive = True)
|
204 |
+
with gr.Row():
|
205 |
+
img2img_prompt = gr.Textbox(label = "img2img_prompt", interactive = True)
|
206 |
+
img2img_negative_prompt = gr.Textbox(label = "img2img_negative_prompt", interactive = True)
|
207 |
+
with gr.Row().style(equal_height=True): # Place dropdowns side by side
|
208 |
+
category_dropdown = gr.Dropdown(
|
209 |
+
choices=category_choices,
|
210 |
+
value=category_choices[1],
|
211 |
+
label="Category", show_label=True
|
212 |
+
)
|
213 |
+
|
214 |
+
style_dropdown = gr.Dropdown(
|
215 |
+
choices=style_choices,
|
216 |
+
value=style_choices[1],
|
217 |
+
label="Style", show_label=True
|
218 |
+
)
|
219 |
+
with gr.Row():
|
220 |
+
lightning_dropdown = gr.Dropdown(
|
221 |
+
choices=lightning_choices,
|
222 |
+
value=lightning_choices[1],
|
223 |
+
label="Lightning", show_label=True
|
224 |
+
)
|
225 |
+
|
226 |
+
lens_dropdown = gr.Dropdown(
|
227 |
+
choices=lens_choices,
|
228 |
+
value=lens_choices[1],
|
229 |
+
label="Lens", show_label=True
|
230 |
+
)
|
231 |
+
with gr.Row():
|
232 |
+
gr.HTML('''
|
233 |
+
<hr class="rounded" id="divider">
|
234 |
+
''')
|
235 |
+
with gr.Row():
|
236 |
+
gr.HTML('''<h2 id="input_header">Links</h2>''')
|
237 |
+
with gr.Row():
|
238 |
+
gr.HTML('''
|
239 |
+
<h3>Stable Diffusion Tutorials⚡</h3>
|
240 |
+
<container>
|
241 |
+
<a href="https://nextdiffusion.ai" target="_blank">
|
242 |
+
<button id="website_button" class="external-link">Website</button>
|
243 |
+
</a>
|
244 |
+
<a href="https://www.youtube.com/channel/UCd9UIUkLnjE-Fj-CGFdU74Q?sub_confirmation=1" target="_blank">
|
245 |
+
<button id="youtube_button" class="external-link">YouTube</button>
|
246 |
+
</a>
|
247 |
+
</container>
|
248 |
+
''')
|
249 |
+
|
250 |
+
|
251 |
+
with gr.Column(): # Right column for result_textbox and generate_button
|
252 |
+
# Add a Textbox to display the generated text
|
253 |
+
with gr.Row():
|
254 |
+
gr.HTML('''<h2 id="output_header">Output 👋</h2>''')
|
255 |
+
result_textbox = gr.Textbox(label="Generated Prompt", lines=3)
|
256 |
+
use_default_negative_prompt = gr.Checkbox(label="Include Negative Prompt", value=True, interactive=True, elem_id="negative_prompt_checkbox")
|
257 |
+
setattr(use_default_negative_prompt,"do_not_save_to_config",True)
|
258 |
+
with gr.Row():
|
259 |
+
generate_button = gr.Button(value="Generate", elem_id="generate_button")
|
260 |
+
clear_button = gr.Button(value="Clear")
|
261 |
+
with gr.Row():
|
262 |
+
txt2img = gr.Button("Send to txt2img")
|
263 |
+
img2img = gr.Button("Send to img2img")
|
264 |
+
# Create a button to trigger text generation
|
265 |
+
#txt2img.click(add_to_prompt, inputs=[result_textbox, use_default_negative_prompt], outputs=[txt2img_prompt, txt2img_negative_prompt ]).then(None, _js='switch_to_txt2img',inputs=None, outputs=None)
|
266 |
+
#img2img.click(add_to_prompt, inputs=[result_textbox, use_default_negative_prompt], outputs=[img2img_prompt, img2img_negative_prompt]).then(None, _js='switch_to_img2img',inputs=None, outputs=None)
|
267 |
+
txt2img.click(add_to_prompt, inputs=[result_textbox, use_default_negative_prompt, txt2img_prompt, txt2img_negative_prompt], outputs=[txt2img_prompt, txt2img_negative_prompt ])
|
268 |
+
img2img.click(add_to_prompt, inputs=[result_textbox, use_default_negative_prompt, img2img_prompt, img2img_negative_prompt], outputs=[img2img_prompt, img2img_negative_prompt])
|
269 |
+
#generate_button = gr.Button(value="Generate", elem_id="generate_button")
|
270 |
+
#btn_refresh.click(None, _js="window.location.reload()")
|
271 |
+
clear_button.click(lambda x: [""] * 5, None,
|
272 |
+
[result_textbox, txt2img_prompt, txt2img_negative_prompt,
|
273 |
+
img2img_prompt, img2img_negative_prompt])
|
274 |
+
|
275 |
+
# Register the callback for the Generate button
|
276 |
+
generate_button.click(fn=generate_prompt_output, inputs=[category_dropdown, style_dropdown, lightning_dropdown, lens_dropdown, use_default_negative_prompt], outputs=[result_textbox])
|
277 |
+
|
278 |
+
gr.Examples(
|
279 |
+
[
|
280 |
+
["A lovely cat", "low quality, blur", "Animal", "Drawing", "Bloom light", "F/14"],
|
281 |
+
["A cute blue boy", "low quality, blur", "Anime", "3D style", "None", "Random"],
|
282 |
+
],
|
283 |
+
inputs = [txt2img_prompt, txt2img_negative_prompt, category_dropdown, style_dropdown, lightning_dropdown, lens_dropdown]
|
284 |
+
)
|
285 |
+
|
286 |
+
#return (prompt_generator, "Next Diffusion ⚡", "Next Diffusion ⚡"),
|
287 |
+
return prompt_generator
|
288 |
+
|
289 |
+
#script_callbacks.on_ui_tabs(on_ui_tabs)
|
290 |
+
|
291 |
+
with on_ui_tabs() as demo:
|
292 |
+
pass
|
293 |
+
|
294 |
+
demo.launch()
|