MrDrmm commited on
Commit
140aeb5
·
verified ·
1 Parent(s): 546128b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -72
app.py CHANGED
@@ -92,79 +92,120 @@ def gen_fn(model_str, prompt, width, height):
92
  # Функция основного интерфейса
93
  def make_me():
94
  with gr.Row():
95
- with gr.Column(scale=1):
96
- txt_input = gr.Textbox(
97
- label='Your prompt:',
98
- lines=3,
99
- container=False,
100
- elem_id="custom_textbox",
101
- placeholder="Enter your prompt here"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  )
103
- width_slider = gr.Slider(
104
- minimum=100, maximum=1000, step=50, value=250, label="Width"
105
- )
106
- height_slider = gr.Slider(
107
- minimum=100, maximum=1000, step=50, value=250, label="Height"
108
- )
109
- with gr.Row():
110
- gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
111
- stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
112
-
113
- def on_generate_click():
114
- return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
115
-
116
- def on_stop_click():
117
- return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
118
-
119
- gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
120
- stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
121
-
122
- # Делим вывод изображений на два ряда
123
- with gr.Row():
124
- half = len(default_models) // 2
125
- output_row1 = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models[:half]]
126
- output_row2 = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models[half:]]
127
- current_models_row1 = [gr.Textbox(m, visible=False) for m in default_models[:half]]
128
- current_models_row2 = [gr.Textbox(m, visible=False) for m in default_models[half:]]
129
-
130
- # Первый ряд изображений
131
- for m, o in zip(current_models_row1, output_row1):
132
- gen_event = gen_button.click(gen_fn, [m, txt_input], o)
133
- stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
134
-
135
- # Второй ряд изображений
136
- for m, o in zip(current_models_row2, output_row2):
137
- gen_event = gen_button.click(gen_fn, [m, txt_input], o)
138
- stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
139
-
140
- # Разделяем чекбоксы на два ряда
141
- with gr.Accordion('Model selection', elem_id="custom_accordion"):
142
- half = len(models) // 2
143
- model_choice_row1 = models[:half]
144
- model_choice_row2 = models[half:]
145
-
146
- model_choice1 = gr.CheckboxGroup(
147
- model_choice_row1,
148
- label=f'{len(model_choice_row1)} models selected',
149
- value=default_models[:half],
150
- interactive=True,
151
- elem_id="custom_checkbox_group"
152
- )
153
- model_choice2 = gr.CheckboxGroup(
154
- model_choice_row2,
155
- label=f'{len(model_choice_row2)} models selected',
156
- value=default_models[half:],
157
- interactive=True,
158
- elem_id="custom_checkbox_group"
159
- )
160
-
161
- # Обновляем первый ряд
162
- model_choice1.change(update_imgbox, model_choice1, output_row1)
163
- model_choice1.change(extend_choices, model_choice1, current_models_row1)
164
-
165
- # Обновляем второй ряд
166
- model_choice2.change(update_imgbox, model_choice2, output_row2)
167
- model_choice2.change(extend_choices, model_choice2, current_models_row2)
168
 
169
  with gr.Row():
170
  gr.HTML("")
 
92
  # Функция основного интерфейса
93
  def make_me():
94
  with gr.Row():
95
+ with gr.Column(scale=10):
96
+ with gr.Group():
97
+ prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
98
+ with gr.Accordion("Advanced options", open=False):
99
+ neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
100
+ with gr.Row():
101
+ width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
102
+ height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
103
+ steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
104
+ with gr.Row():
105
+ cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
106
+ seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
107
+ seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary")
108
+ recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
109
+ with gr.Row():
110
+ positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
111
+ positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
112
+ negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
113
+ negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
114
+ with gr.Row():
115
+ image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=2)
116
+ trans_prompt = gr.Button(value="Translate 📝", variant="secondary", size="sm", scale=2)
117
+ clear_prompt = gr.Button(value="Clear 🗑️", variant="secondary", size="sm", scale=1)
118
+ with gr.Row():
119
+ run_button = gr.Button("Generate Image", variant="primary", scale=8)
120
+ random_button = gr.Button("Random Model 🎲", variant="secondary", scale=3)
121
+ stop_button = gr.Button('Stop', interactive=False, variant="stop", scale=1)
122
+
123
+ with gr.Group():
124
+ model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
125
+ model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
126
+ with gr.Column(scale=10):
127
+ with gr.Group():
128
+ with gr.Row():
129
+ output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
130
+ show_download_button=True, show_share_button=False, show_label=False,
131
+ interactive=False, min_width=80, visible=True, width=112, height=112) for _ in range(max_images)]
132
+ with gr.Group():
133
+ results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
134
+ container=True, format="png", object_fit="cover", columns=2, rows=2)
135
+ image_files = gr.Files(label="Download", interactive=False)
136
+ clear_results = gr.Button("Clear Gallery / Download 🗑️", variant="secondary")
137
+ with gr.Column():
138
+ examples = gr.Examples(
139
+ examples = [
140
+ ["souryuu asuka langley, 1girl, neon genesis evangelion, plugsuit, pilot suit, red bodysuit, sitting, crossing legs, black eye patch, cat hat, throne, symmetrical, looking down, from bottom, looking at viewer, outdoors"],
141
+ ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
142
+ ["kafuu chino, 1girl, solo"],
143
+ ["1girl"],
144
+ ["beautiful sunset"],
145
+ ],
146
+ inputs=[prompt],
147
+ cache_examples=False,
148
  )
149
+ with gr.Tab("PNG Info"):
150
+ def extract_exif_data(image):
151
+ if image is None: return ""
152
+ try:
153
+ metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
154
+ for key in metadata_keys:
155
+ if key in image.info:
156
+ return image.info[key]
157
+ return str(image.info)
158
+ except Exception as e:
159
+ return f"Error extracting metadata: {str(e)}"
160
+ with gr.Row():
161
+ with gr.Column():
162
+ image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
163
+ with gr.Column():
164
+ result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
165
+
166
+ image_metadata.change(
167
+ fn=extract_exif_data,
168
+ inputs=[image_metadata],
169
+ outputs=[result_metadata],
170
+ )
171
+ gr.Markdown(
172
+ f"""This demo was created in reference to the following demos.<br>
173
+ [Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood),
174
+ [Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL),
175
+ [Yntec/Diffusion80XX](https://huggingface.co/spaces/Yntec/Diffusion80XX).
176
+ """
177
+ )
178
+ gr.DuplicateButton(value="Duplicate Space")
179
+ gr.Markdown(f"Just a few edits to *model.py* are all it takes to complete your own collection.")
180
+
181
+ gr.on(triggers=[run_button.click, prompt.submit, random_button.click], fn=lambda: gr.update(interactive=True), inputs=None, outputs=stop_button, show_api=False)
182
+ model_name.change(change_model, [model_name], [model_info], queue=True, show_api=True)\
183
+ .success(warm_model, [model_name], None, queue=True, show_api=True)
184
+ for i, o in enumerate(output):
185
+ img_i = gr.Number(i, visible=False)
186
+ image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=True)
187
+ gen_event = gr.on(triggers=[run_button.click, prompt.submit],
188
+ fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4: infer_fn(m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4) if (i < n) else None,
189
+ inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
190
+ positive_prefix, positive_suffix, negative_prefix, negative_suffix],
191
+ outputs=[o], queue=True, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
192
+ gen_event2 = gr.on(triggers=[random_button.click],
193
+ fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4: infer_rand_fn(m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4) if (i < n) else None,
194
+ inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
195
+ positive_prefix, positive_suffix, negative_prefix, negative_suffix],
196
+ outputs=[o], queue=True, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
197
+ o.change(save_gallery, [o, results], [results, image_files], show_api=False)
198
+ stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
199
+
200
+ clear_prompt.click(lambda: (None, None), None, [prompt, neg_prompt], queue=True, show_api=True)
201
+ clear_results.click(lambda: (None, None), None, [results, image_files], queue=True, show_api=True)
202
+ recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
203
+ [positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=True, show_api=True)
204
+ seed_rand.click(randomize_seed, None, [seed], queue=True, show_api=True)
205
+ trans_prompt.click(translate_to_en, [prompt], [prompt], queue=True, show_api=True)\
206
+ .then(translate_to_en, [neg_prompt], [neg_prompt], queue=True, show_api=True)
207
+
208
+
 
 
 
 
 
209
 
210
  with gr.Row():
211
  gr.HTML("")