charliebaby2023 commited on
Commit
462d27c
·
verified ·
1 Parent(s): 3dfaa7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -186
app.py CHANGED
@@ -1,186 +1,179 @@
1
- import gradio as gr
2
- from model import models
3
- from multit2i import (load_models, infer_fn, infer_rand_fn, save_gallery,
4
- change_model, warm_model, get_model_info_md, loaded_models, warm_models,
5
- get_positive_prefix, get_positive_suffix, get_negative_prefix, get_negative_suffix,
6
- get_recom_prompt_type, set_recom_prompt_preset, get_tag_type, randomize_seed, translate_to_en)
7
- from tagger.tagger import (predict_tags_wd, remove_specific_prompt, convert_danbooru_to_e621_prompt,
8
- insert_recom_prompt, compose_prompt_to_copy)
9
- from tagger.fl2sd3longcap import predict_tags_fl2_sd3
10
- from tagger.v2 import V2_ALL_MODELS, v2_random_prompt
11
- from tagger.utils import (V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS,
12
- V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
13
-
14
- max_images = 6
15
- MAX_SEED = 2**32-1
16
- load_models(models)
17
- warm_models(models[0:max_images])
18
-
19
- css = """
20
- .title { font-size: 3em; align-items: center; text-align: center; }
21
- .info { align-items: center; text-align: center; }
22
- .model_info { text-align: center; }
23
- .output { width=112px; height=112px; max_width=112px; max_height=112px; !important; }
24
- .gallery { min_width=512px; min_height=512px; max_height=1024px; !important; }
25
- """
26
-
27
- with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
28
- with gr.Tab("Image Generator"):
29
- with gr.Row():
30
- with gr.Column(scale=10):
31
- with gr.Group():
32
- with gr.Accordion("Prompt from Image File", open=False):
33
- tagger_image = gr.Image(label="Input image", type="pil", format="png", sources=["upload", "clipboard"], height=256)
34
- with gr.Accordion(label="Advanced options", open=False):
35
- with gr.Row(equal_height=True):
36
- tagger_general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
37
- tagger_character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
38
- tagger_tag_type = gr.Radio(label="Convert tags to", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
39
- with gr.Row(equal_height=True):
40
- tagger_recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
41
- tagger_keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
42
- tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
43
- tagger_generate_from_image = gr.Button(value="Generate Tags from Image", variant="secondary")
44
- with gr.Accordion("Prompt Transformer", open=False):
45
- with gr.Row(equal_height=True):
46
- v2_character = gr.Textbox(label="Character", placeholder="hatsune miku", scale=2)
47
- v2_series = gr.Textbox(label="Series", placeholder="vocaloid", scale=2)
48
- with gr.Row(equal_height=True):
49
- v2_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="sfw")
50
- v2_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square", visible=False)
51
- v2_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="long")
52
- with gr.Row(equal_height=True):
53
- v2_identity = gr.Radio(label="Keep identity", info="How strictly to keep the identity of the character or subject. If you specify the detail of subject in the prompt, you should choose `strict`. Otherwise, choose `none` or `lax`. `none` is very creative but sometimes ignores the input prompt.", choices=list(V2_IDENTITY_OPTIONS), value="lax")
54
- v2_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
55
- v2_tag_type = gr.Radio(label="Tag Type", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru", visible=False)
56
- v2_model = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
57
- v2_copy = gr.Button(value="Copy to clipboard", variant="secondary", size="sm", interactive=False)
58
- random_prompt = gr.Button(value="Extend 🎲", variant="secondary")
59
- prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
60
- with gr.Accordion("Advanced options", open=False):
61
- neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
62
- with gr.Row(equal_height=True):
63
- width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
64
- height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
65
- steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
66
- with gr.Row(equal_height=True):
67
- cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
68
- seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
69
- seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary")
70
- recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
71
- with gr.Row(equal_height=True):
72
- positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
73
- positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
74
- negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
75
- negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
76
- with gr.Row(equal_height=True):
77
- image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=2)
78
- trans_prompt = gr.Button(value="Translate 📝", variant="secondary", size="sm", scale=2)
79
- clear_prompt = gr.Button(value="Clear 🗑️", variant="secondary", size="sm", scale=1)
80
-
81
- with gr.Row(equal_height=True):
82
- run_button = gr.Button("Generate Image", variant="primary", scale=6)
83
- random_button = gr.Button("Random Model 🎲", variant="secondary", scale=3)
84
- #stop_button = gr.Button('Stop', variant="stop", interactive=False, scale=1)
85
- with gr.Group():
86
- model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
87
- model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
88
- with gr.Column(scale=10):
89
- with gr.Group():
90
- with gr.Row():
91
- output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
92
- show_download_button=True, show_share_button=False, show_label=False,
93
- interactive=False, min_width=80, visible=True, width=112, height=112) for _ in range(max_images)]
94
- with gr.Group():
95
- results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
96
- container=True, format="png", object_fit="cover", columns=2, rows=2)
97
- image_files = gr.Files(label="Download", interactive=False)
98
- clear_results = gr.Button("Clear Gallery / Download 🗑️", variant="secondary")
99
- with gr.Column():
100
- examples = gr.Examples(
101
- examples = [
102
- ["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"],
103
- ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
104
- ["kafuu chino, 1girl, solo"],
105
- ["1girl"],
106
- ["beautiful sunset"],
107
- ],
108
- inputs=[prompt],
109
- cache_examples=False,
110
- )
111
- with gr.Tab("PNG Info"):
112
- def extract_exif_data(image):
113
- if image is None: return ""
114
- try:
115
- metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
116
- for key in metadata_keys:
117
- if key in image.info:
118
- return image.info[key]
119
- return str(image.info)
120
- except Exception as e:
121
- return f"Error extracting metadata: {str(e)}"
122
- with gr.Row():
123
- with gr.Column():
124
- image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
125
- with gr.Column():
126
- result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
127
- image_metadata.change(
128
- fn=extract_exif_data,
129
- inputs=[image_metadata],
130
- outputs=[result_metadata],
131
- )
132
- gr.Markdown(
133
- f"""This demo was created in reference to the following demos.<br>
134
- [Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood),
135
- [Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL),
136
- [Yntec/Diffusion80XX](https://huggingface.co/spaces/Yntec/Diffusion80XX).
137
- """, elem_classes="info")
138
- gr.DuplicateButton(value="Duplicate Space")
139
- gr.Markdown(f"Just a few edits to *model.py* are all it takes to complete your own collection.", elem_classes="info")
140
-
141
- #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)
142
- model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)\
143
- .success(warm_model, [model_name], None, queue=False, show_api=False)
144
- for i, o in enumerate(output):
145
- img_i = gr.Number(i, visible=False)
146
- image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
147
- gen_event = gr.on(triggers=[run_button.click, prompt.submit],
148
- 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,
149
- inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
150
- positive_prefix, positive_suffix, negative_prefix, negative_suffix],
151
- outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
152
- gen_event2 = gr.on(triggers=[random_button.click],
153
- 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,
154
- inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
155
- positive_prefix, positive_suffix, negative_prefix, negative_suffix],
156
- outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
157
- o.change(save_gallery, [o, results], [results, image_files], show_api=False)
158
- #stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
159
-
160
- clear_prompt.click(lambda: (None, None, None, None), None, [prompt, neg_prompt, v2_character, v2_series], queue=False, show_api=False)
161
- clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
162
- recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
163
- [positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
164
- seed_rand.click(randomize_seed, None, [seed], queue=False, show_api=False)
165
- trans_prompt.click(translate_to_en, [prompt], [prompt], queue=False, show_api=False)\
166
- .then(translate_to_en, [neg_prompt], [neg_prompt], queue=False, show_api=False)
167
-
168
- random_prompt.click(
169
- v2_random_prompt, [prompt, v2_series, v2_character, v2_rating, v2_aspect_ratio, v2_length,
170
- v2_identity, v2_ban_tags, v2_model], [prompt, v2_series, v2_character], show_api=False,
171
- ).success(get_tag_type, [positive_prefix, positive_suffix, negative_prefix, negative_suffix], [v2_tag_type], queue=False, show_api=False
172
- ).success(convert_danbooru_to_e621_prompt, [prompt, v2_tag_type], [prompt], queue=False, show_api=False)
173
- tagger_generate_from_image.click(lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
174
- ).success(
175
- predict_tags_wd,
176
- [tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
177
- [v2_series, v2_character, prompt, v2_copy],
178
- show_api=False,
179
- ).success(predict_tags_fl2_sd3, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
180
- ).success(remove_specific_prompt, [prompt, tagger_keep_tags], [prompt], queue=False, show_api=False,
181
- ).success(convert_danbooru_to_e621_prompt, [prompt, tagger_tag_type], [prompt], queue=False, show_api=False,
182
- ).success(insert_recom_prompt, [prompt, neg_prompt, tagger_recom_prompt], [prompt, neg_prompt], queue=False, show_api=False,
183
- ).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
184
-
185
- #demo.queue(default_concurrency_limit=200, max_size=200)
186
- demo.launch(max_threads=400, ssr_mode=False)
 
1
+ import gradio as gr
2
+ from model import models
3
+ from multit2i import (load_models, infer_fn, infer_rand_fn, save_gallery,
4
+ change_model, warm_model, get_model_info_md, loaded_models, warm_models,
5
+ get_positive_prefix, get_positive_suffix, get_negative_prefix, get_negative_suffix,
6
+ get_recom_prompt_type, set_recom_prompt_preset, get_tag_type, randomize_seed, translate_to_en)
7
+ from tagger.tagger import (predict_tags_wd, remove_specific_prompt, convert_danbooru_to_e621_prompt,
8
+ insert_recom_prompt, compose_prompt_to_copy)
9
+ from tagger.fl2sd3longcap import predict_tags_fl2_sd3
10
+ from tagger.v2 import V2_ALL_MODELS, v2_random_prompt
11
+ from tagger.utils import (V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS,
12
+ V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
13
+
14
+ max_images = 6
15
+ MAX_SEED = 2**32-1
16
+ load_models(models)
17
+ warm_models(models[0:max_images])
18
+
19
+ css = """
20
+ .title { font-size: 3em; align-items: center; text-align: center; }
21
+ .info { align-items: center; text-align: center; }
22
+ .model_info { text-align: center; }
23
+ .output { width=112px; height=112px; max_width=112px; max_height=112px; !important; }
24
+ .gallery { min_width=512px; min_height=512px; max_height=1024px; !important; }
25
+ """
26
+
27
+ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
28
+ with gr.Tab("Image Generator"):
29
+ with gr.Row():
30
+ with gr.Column(scale=10):
31
+ with gr.Group():
32
+ with gr.Accordion("Prompt from Image File", open=False):
33
+ tagger_image = gr.Image(label="Input image", type="pil", format="png", sources=["upload", "clipboard"], height=256)
34
+ with gr.Accordion(label="Advanced options", open=False):
35
+ with gr.Row(equal_height=True):
36
+ tagger_general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
37
+ tagger_character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
38
+ tagger_tag_type = gr.Radio(label="Convert tags to", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
39
+ with gr.Row(equal_height=True):
40
+ tagger_recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
41
+ tagger_keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
42
+ tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
43
+ tagger_generate_from_image = gr.Button(value="Generate Tags from Image", variant="secondary")
44
+ with gr.Accordion("Prompt Transformer", open=False):
45
+ with gr.Row(equal_height=True):
46
+ v2_character = gr.Textbox(label="Character", placeholder="hatsune miku", scale=2)
47
+ v2_series = gr.Textbox(label="Series", placeholder="vocaloid", scale=2)
48
+ with gr.Row(equal_height=True):
49
+ v2_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="sfw")
50
+ v2_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square", visible=False)
51
+ v2_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="long")
52
+ with gr.Row(equal_height=True):
53
+ v2_identity = gr.Radio(label="Keep identity", info="How strictly to keep the identity of the character or subject. If you specify the detail of subject in the prompt, you should choose `strict`. Otherwise, choose `none` or `lax`. `none` is very creative but sometimes ignores the input prompt.", choices=list(V2_IDENTITY_OPTIONS), value="lax")
54
+ v2_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
55
+ v2_tag_type = gr.Radio(label="Tag Type", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru", visible=False)
56
+ v2_model = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
57
+ v2_copy = gr.Button(value="Copy to clipboard", variant="secondary", size="sm", interactive=False)
58
+ random_prompt = gr.Button(value="Extend 🎲", variant="secondary")
59
+ prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
60
+ with gr.Accordion("Advanced options", open=False):
61
+ neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
62
+ with gr.Row(equal_height=True):
63
+ width = gr.Slider(label="Width", maximum=1216, step=32, value=0)
64
+ height = gr.Slider(label="Height", maximum=1216, step=32, value=0)
65
+ steps = gr.Slider(label="Number of inference steps", maximum=100, step=1, value=0)
66
+ with gr.Row(equal_height=True):
67
+ cfg = gr.Slider(label="Guidance scale", maximum=30.0, step=0.1, value=0)
68
+ seed = gr.Slider(label="Seed", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
69
+ seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary")
70
+ recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
71
+ with gr.Row(equal_height=True):
72
+ positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
73
+ positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
74
+ negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
75
+ negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
76
+ with gr.Row(equal_height=True):
77
+ image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=2)
78
+ trans_prompt = gr.Button(value="Translate 📝", variant="secondary", size="sm", scale=2)
79
+ clear_prompt = gr.Button(value="Clear 🗑️", variant="secondary", size="sm", scale=1)
80
+
81
+ with gr.Row(equal_height=True):
82
+ run_button = gr.Button("Generate Image", variant="primary", scale=6)
83
+ random_button = gr.Button("Random Model 🎲", variant="secondary", scale=3)
84
+ #stop_button = gr.Button('Stop', variant="stop", interactive=False, scale=1)
85
+ with gr.Group():
86
+ model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
87
+ model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
88
+ with gr.Column(scale=10):
89
+ with gr.Group():
90
+ with gr.Row():
91
+ output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
92
+ show_download_button=True, show_share_button=False, show_label=False,
93
+ interactive=False, min_width=80, visible=True, width=112, height=112) for _ in range(max_images)]
94
+ with gr.Group():
95
+ results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
96
+ container=True, format="png", object_fit="cover", columns=2, rows=2)
97
+ image_files = gr.Files(label="Download", interactive=False)
98
+ clear_results = gr.Button("Clear Gallery / Download 🗑️", variant="secondary")
99
+ with gr.Column():
100
+ examples = gr.Examples(
101
+ examples = [
102
+ ["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"],
103
+ ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
104
+ ["kafuu chino, 1girl, solo"],
105
+ ["1girl"],
106
+ ["beautiful sunset"],
107
+ ],
108
+ inputs=[prompt],
109
+ cache_examples=False,
110
+ )
111
+ with gr.Tab("PNG Info"):
112
+ def extract_exif_data(image):
113
+ if image is None: return ""
114
+ try:
115
+ metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
116
+ for key in metadata_keys:
117
+ if key in image.info:
118
+ return image.info[key]
119
+ return str(image.info)
120
+ except Exception as e:
121
+ return f"Error extracting metadata: {str(e)}"
122
+ with gr.Row():
123
+ with gr.Column():
124
+ image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
125
+ with gr.Column():
126
+ result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
127
+ image_metadata.change(
128
+ fn=extract_exif_data,
129
+ inputs=[image_metadata],
130
+ outputs=[result_metadata],
131
+ )
132
+ gr.DuplicateButton(value="Duplicate Space")
133
+
134
+ #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)
135
+ model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)\
136
+ .success(warm_model, [model_name], None, queue=False, show_api=False)
137
+ for i, o in enumerate(output):
138
+ img_i = gr.Number(i, visible=False)
139
+ image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
140
+ gen_event = gr.on(triggers=[run_button.click, prompt.submit],
141
+ 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,
142
+ inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
143
+ positive_prefix, positive_suffix, negative_prefix, negative_suffix],
144
+ outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
145
+ gen_event2 = gr.on(triggers=[random_button.click],
146
+ 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,
147
+ inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
148
+ positive_prefix, positive_suffix, negative_prefix, negative_suffix],
149
+ outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
150
+ o.change(save_gallery, [o, results], [results, image_files], show_api=False)
151
+ #stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
152
+
153
+ clear_prompt.click(lambda: (None, None, None, None), None, [prompt, neg_prompt, v2_character, v2_series], queue=False, show_api=False)
154
+ clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
155
+ recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
156
+ [positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
157
+ seed_rand.click(randomize_seed, None, [seed], queue=False, show_api=False)
158
+ trans_prompt.click(translate_to_en, [prompt], [prompt], queue=False, show_api=False)\
159
+ .then(translate_to_en, [neg_prompt], [neg_prompt], queue=False, show_api=False)
160
+
161
+ random_prompt.click(
162
+ v2_random_prompt, [prompt, v2_series, v2_character, v2_rating, v2_aspect_ratio, v2_length,
163
+ v2_identity, v2_ban_tags, v2_model], [prompt, v2_series, v2_character], show_api=False,
164
+ ).success(get_tag_type, [positive_prefix, positive_suffix, negative_prefix, negative_suffix], [v2_tag_type], queue=False, show_api=False
165
+ ).success(convert_danbooru_to_e621_prompt, [prompt, v2_tag_type], [prompt], queue=False, show_api=False)
166
+ tagger_generate_from_image.click(lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
167
+ ).success(
168
+ predict_tags_wd,
169
+ [tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
170
+ [v2_series, v2_character, prompt, v2_copy],
171
+ show_api=False,
172
+ ).success(predict_tags_fl2_sd3, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
173
+ ).success(remove_specific_prompt, [prompt, tagger_keep_tags], [prompt], queue=False, show_api=False,
174
+ ).success(convert_danbooru_to_e621_prompt, [prompt, tagger_tag_type], [prompt], queue=False, show_api=False,
175
+ ).success(insert_recom_prompt, [prompt, neg_prompt, tagger_recom_prompt], [prompt, neg_prompt], queue=False, show_api=False,
176
+ ).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
177
+
178
+ #demo.queue(default_concurrency_limit=200, max_size=200)
179
+ demo.launch(max_threads=400, ssr_mode=False)