John6666 commited on
Commit
89bcb96
โ€ข
1 Parent(s): 93242f4

Upload 13 files

Browse files
Files changed (5) hide show
  1. README.md +1 -1
  2. app.py +119 -89
  3. multit2i.py +57 -27
  4. requirements.txt +2 -5
  5. tagger/tagger.py +21 -21
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: ๐Ÿ–ผ๏ธ๐Ÿ“…
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.39.0
8
  app_file: app.py
9
  pinned: false
10
  short_description: Text-to-Image
 
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
10
  short_description: Text-to-Image
app.py CHANGED
@@ -3,7 +3,7 @@ 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,
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)
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
@@ -22,97 +22,124 @@ css = """
22
  """
23
 
24
  with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
25
- with gr.Row():
26
- with gr.Column(scale=10):
27
- with gr.Group():
28
- with gr.Accordion("Prompt from Image File", open=False):
29
- tagger_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
30
- with gr.Accordion(label="Advanced options", open=False):
 
 
 
 
 
 
 
 
 
 
 
31
  with gr.Row():
32
- tagger_general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
33
- tagger_character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
34
- tagger_tag_type = gr.Radio(label="Convert tags to", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
35
  with gr.Row():
36
- tagger_recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
37
- tagger_keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
38
- tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
39
- tagger_generate_from_image = gr.Button(value="Generate Tags from Image", variant="secondary")
40
- with gr.Accordion("Prompt Transformer", open=False):
41
- with gr.Row():
42
- v2_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="sfw")
43
- 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)
44
- v2_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="long")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  with gr.Row():
46
- 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")
47
- v2_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
48
- v2_tag_type = gr.Radio(label="Tag Type", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru", visible=False)
49
- v2_model = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
50
- v2_copy = gr.Button(value="Copy to clipboard", variant="secondary", size="sm", interactive=False)
51
  with gr.Row():
52
- v2_character = gr.Textbox(label="Character", placeholder="hatsune miku", scale=2)
53
- v2_series = gr.Textbox(label="Series", placeholder="vocaloid", scale=2)
54
- random_prompt = gr.Button(value="Extend Prompt ๐ŸŽฒ", variant="secondary", size="sm", scale=1)
55
- clear_prompt = gr.Button(value="Clear Prompt ๐Ÿ—‘๏ธ", variant="secondary", size="sm", scale=1)
56
- prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
57
- with gr.Accordion("Advanced options", open=False):
58
- neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
59
- with gr.Row():
60
- width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
61
- height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
62
  with gr.Row():
63
- steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
64
- cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
65
- seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
66
- recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
67
- with gr.Row():
68
- positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
69
- positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
70
- negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
71
- negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=1)
74
- with gr.Row():
75
- run_button = gr.Button("Generate Image", variant="primary", scale=6)
76
- random_button = gr.Button("Random Model ๐ŸŽฒ", variant="secondary", scale=3)
77
- stop_button = gr.Button('Stop', variant="stop", interactive=False, scale=1)
78
- with gr.Group():
79
- model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
80
- model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
81
- with gr.Column(scale=10):
82
- with gr.Group():
83
- with gr.Row():
84
- output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
85
- show_download_button=True, show_share_button=False, show_label=False,
86
- interactive=False, min_width=80, visible=True) for _ in range(max_images)]
87
- with gr.Group():
88
- results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
89
- container=True, format="png", object_fit="cover", columns=2, rows=2)
90
- image_files = gr.Files(label="Download", interactive=False)
91
- clear_results = gr.Button("Clear Gallery / Download ๐Ÿ—‘๏ธ", variant="secondary")
92
- with gr.Column():
93
- examples = gr.Examples(
94
- examples = [
95
- ["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"],
96
- ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
97
- ["kafuu chino, 1girl, solo"],
98
- ["1girl"],
99
- ["beautiful sunset"],
100
- ],
101
- inputs=[prompt],
102
- )
103
- gr.Markdown(
104
- f"""This demo was created in reference to the following demos.<br>
105
- [Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood),
106
- [Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL),
107
- [Yntec/Diffusion80XX](https://huggingface.co/spaces/Yntec/Diffusion80XX).
108
- """
109
- )
110
- gr.DuplicateButton(value="Duplicate Space")
111
- gr.Markdown(f"Just a few edits to *model.py* are all it takes to complete your own collection.")
112
 
113
- 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)
114
  model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)\
115
- .success(warm_model, [model_name], None, queue=True, show_api=False)
116
  for i, o in enumerate(output):
117
  img_i = gr.Number(i, visible=False)
118
  image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
@@ -120,19 +147,22 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
120
  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,
121
  inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
122
  positive_prefix, positive_suffix, negative_prefix, negative_suffix],
123
- outputs=[o], queue=True, show_api=False)
124
  gen_event2 = gr.on(triggers=[random_button.click],
125
  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,
126
  inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
127
  positive_prefix, positive_suffix, negative_prefix, negative_suffix],
128
- outputs=[o], queue=True, show_api=False)
129
  o.change(save_gallery, [o, results], [results, image_files], show_api=False)
130
- stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
131
 
132
- clear_prompt.click(lambda: None, None, [prompt], queue=False, show_api=False)
133
  clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
134
  recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
135
  [positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
 
 
 
136
 
137
  random_prompt.click(
138
  v2_random_prompt, [prompt, v2_series, v2_character, v2_rating, v2_aspect_ratio, v2_length,
@@ -151,5 +181,5 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
151
  ).success(insert_recom_prompt, [prompt, neg_prompt, tagger_recom_prompt], [prompt, neg_prompt], queue=False, show_api=False,
152
  ).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
153
 
154
- demo.queue(default_concurrency_limit=200, max_size=200)
155
  demo.launch(max_threads=400)
 
3
  from multit2i import (load_models, infer_fn, infer_rand_fn, save_gallery,
4
  change_model, warm_model, get_model_info_md, loaded_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
 
22
  """
23
 
24
  with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
25
+ with gr.Tab("Image Generator"):
26
+ with gr.Row():
27
+ with gr.Column(scale=10):
28
+ with gr.Group():
29
+ with gr.Accordion("Prompt from Image File", open=False):
30
+ tagger_image = gr.Image(label="Input image", type="pil", format="png", sources=["upload", "clipboard"], height=256)
31
+ with gr.Accordion(label="Advanced options", open=False):
32
+ with gr.Row():
33
+ tagger_general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
34
+ tagger_character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
35
+ tagger_tag_type = gr.Radio(label="Convert tags to", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
36
+ with gr.Row():
37
+ tagger_recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
38
+ tagger_keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
39
+ tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
40
+ tagger_generate_from_image = gr.Button(value="Generate Tags from Image", variant="secondary")
41
+ with gr.Accordion("Prompt Transformer", open=False):
42
  with gr.Row():
43
+ v2_character = gr.Textbox(label="Character", placeholder="hatsune miku", scale=2)
44
+ v2_series = gr.Textbox(label="Series", placeholder="vocaloid", scale=2)
 
45
  with gr.Row():
46
+ v2_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="sfw")
47
+ 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)
48
+ v2_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="long")
49
+ with gr.Row():
50
+ 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")
51
+ v2_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
52
+ v2_tag_type = gr.Radio(label="Tag Type", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru", visible=False)
53
+ v2_model = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
54
+ v2_copy = gr.Button(value="Copy to clipboard", variant="secondary", size="sm", interactive=False)
55
+ random_prompt = gr.Button(value="Extend ๐ŸŽฒ", variant="secondary")
56
+ prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
57
+ with gr.Accordion("Advanced options", open=False):
58
+ neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
59
+ with gr.Row():
60
+ width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
61
+ height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
62
+ steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
63
+ with gr.Row():
64
+ cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
65
+ seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
66
+ seed_rand = gr.Button("Randomize Seed ๐ŸŽฒ", size="sm", variant="secondary")
67
+ recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
68
+ with gr.Row():
69
+ positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
70
+ positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
71
+ negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
72
+ negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
73
  with gr.Row():
74
+ image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=2)
75
+ trans_prompt = gr.Button(value="Translate ๐Ÿ“", variant="secondary", size="sm", scale=2)
76
+ clear_prompt = gr.Button(value="Clear ๐Ÿ—‘๏ธ", variant="secondary", size="sm", scale=1)
77
+
 
78
  with gr.Row():
79
+ run_button = gr.Button("Generate Image", variant="primary", scale=6)
80
+ random_button = gr.Button("Random Model ๐ŸŽฒ", variant="secondary", scale=3)
81
+ #stop_button = gr.Button('Stop', variant="stop", interactive=False, scale=1)
82
+ with gr.Group():
83
+ model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
84
+ model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
85
+ with gr.Column(scale=10):
86
+ with gr.Group():
 
 
87
  with gr.Row():
88
+ output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
89
+ show_download_button=True, show_share_button=False, show_label=False,
90
+ interactive=False, min_width=80, visible=True, width=112, height=112) for _ in range(max_images)]
91
+ with gr.Group():
92
+ results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
93
+ container=True, format="png", object_fit="cover", columns=2, rows=2)
94
+ image_files = gr.Files(label="Download", interactive=False)
95
+ clear_results = gr.Button("Clear Gallery / Download ๐Ÿ—‘๏ธ", variant="secondary")
96
+ with gr.Column():
97
+ examples = gr.Examples(
98
+ examples = [
99
+ ["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"],
100
+ ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
101
+ ["kafuu chino, 1girl, solo"],
102
+ ["1girl"],
103
+ ["beautiful sunset"],
104
+ ],
105
+ inputs=[prompt],
106
+ cache_examples=False,
107
+ )
108
+ with gr.Tab("PNG Info"):
109
+ def extract_exif_data(image):
110
+ if image is None: return ""
111
+ try:
112
+ metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
113
+ for key in metadata_keys:
114
+ if key in image.info:
115
+ return image.info[key]
116
+ return str(image.info)
117
+ except Exception as e:
118
+ return f"Error extracting metadata: {str(e)}"
119
+ with gr.Row():
120
+ with gr.Column():
121
+ image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
122
+ with gr.Column():
123
+ result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
124
 
125
+ image_metadata.change(
126
+ fn=extract_exif_data,
127
+ inputs=[image_metadata],
128
+ outputs=[result_metadata],
129
+ )
130
+ gr.Markdown(
131
+ f"""This demo was created in reference to the following demos.<br>
132
+ [Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood),
133
+ [Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL),
134
+ [Yntec/Diffusion80XX](https://huggingface.co/spaces/Yntec/Diffusion80XX).
135
+ """
136
+ )
137
+ gr.DuplicateButton(value="Duplicate Space")
138
+ gr.Markdown(f"Just a few edits to *model.py* are all it takes to complete your own collection.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
+ #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)
141
  model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)\
142
+ .success(warm_model, [model_name], None, queue=False, show_api=False)
143
  for i, o in enumerate(output):
144
  img_i = gr.Number(i, visible=False)
145
  image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
 
147
  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,
148
  inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
149
  positive_prefix, positive_suffix, negative_prefix, negative_suffix],
150
+ outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
151
  gen_event2 = gr.on(triggers=[random_button.click],
152
  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,
153
  inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
154
  positive_prefix, positive_suffix, negative_prefix, negative_suffix],
155
+ outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
156
  o.change(save_gallery, [o, results], [results, image_files], show_api=False)
157
+ #stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
158
 
159
+ clear_prompt.click(lambda: (None, None, None, None), None, [prompt, neg_prompt, v2_character, v2_series], queue=False, show_api=False)
160
  clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
161
  recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
162
  [positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
163
+ seed_rand.click(randomize_seed, None, [seed], queue=False, show_api=False)
164
+ trans_prompt.click(translate_to_en, [prompt], [prompt], queue=False, show_api=False)\
165
+ .then(translate_to_en, [neg_prompt], [neg_prompt], queue=False, show_api=False)
166
 
167
  random_prompt.click(
168
  v2_random_prompt, [prompt, v2_series, v2_character, v2_rating, v2_aspect_ratio, v2_length,
 
181
  ).success(insert_recom_prompt, [prompt, neg_prompt, tagger_recom_prompt], [prompt, neg_prompt], queue=False, show_api=False,
182
  ).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
183
 
184
+ #demo.queue(default_concurrency_limit=200, max_size=200)
185
  demo.launch(max_threads=400)
multit2i.py CHANGED
@@ -60,7 +60,7 @@ def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="l
60
  limit = limit * 20 if check_status and force_gpu else limit * 5
61
  models = []
62
  try:
63
- model_infos = api.list_models(author=author, task="text-to-image",
64
  tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit)
65
  except Exception as e:
66
  print(f"Error: Failed to list models.")
@@ -110,7 +110,7 @@ def get_t2i_model_info_dict(repo_id: str):
110
 
111
 
112
  def rename_image(image_path: str | None, model_name: str, save_path: str | None = None):
113
- from PIL import Image
114
  from datetime import datetime, timezone, timedelta
115
  if image_path is None: return None
116
  dt_now = datetime.now(timezone(timedelta(hours=9)))
@@ -118,7 +118,7 @@ def rename_image(image_path: str | None, model_name: str, save_path: str | None
118
  try:
119
  if Path(image_path).exists():
120
  png_path = "image.png"
121
- Image.open(image_path).convert('RGBA').save(png_path, "PNG")
122
  if save_path is not None:
123
  new_path = str(Path(png_path).resolve().rename(Path(save_path).resolve()))
124
  else:
@@ -363,16 +363,16 @@ def warm_model(model_name: str):
363
 
364
  # https://huggingface.co/docs/api-inference/detailed_parameters
365
  # https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
366
- def infer_body(client: InferenceClient | gr.Interface | object, prompt: str, neg_prompt: str | None = None,
367
- height: int | None = None, width: int | None = None,
368
- steps: int | None = None, cfg: int | None = None, seed: int = -1):
369
  png_path = "image.png"
370
  kwargs = {}
371
- if height is not None and height >= 256: kwargs["height"] = height
372
- if width is not None and width >= 256: kwargs["width"] = width
373
- if steps is not None and steps >= 1: kwargs["num_inference_steps"] = steps
374
- if cfg is not None and cfg > 0: cfg = kwargs["guidance_scale"] = cfg
375
- if seed >= 0: kwargs["seed"] = seed
 
376
  try:
377
  if isinstance(client, InferenceClient):
378
  image = client.text_to_image(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
@@ -380,26 +380,18 @@ def infer_body(client: InferenceClient | gr.Interface | object, prompt: str, neg
380
  image = client.fn(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
381
  else: return None
382
  if isinstance(image, tuple): return None
383
- image.save(png_path)
384
- return str(Path(png_path).resolve())
385
  except Exception as e:
386
  print(e)
387
  raise Exception() from e
388
 
389
 
390
- async def infer(model_name: str, prompt: str, neg_prompt: str | None = None,
391
- height: int | None = None, width: int | None = None,
392
- steps: int | None = None, cfg: int | None = None, seed: int = -1,
393
  save_path: str | None = None, timeout: float = inference_timeout):
394
- import random
395
- noise = ""
396
- if seed < 0:
397
- rand = random.randint(1, 500)
398
- for i in range(rand):
399
- noise += " "
400
  model = load_model(model_name)
401
  if not model: return None
402
- task = asyncio.create_task(asyncio.to_thread(infer_body, model, f"{prompt} {noise}", neg_prompt,
403
  height, width, steps, cfg, seed))
404
  await asyncio.sleep(0)
405
  try:
@@ -423,8 +415,8 @@ async def infer(model_name: str, prompt: str, neg_prompt: str | None = None,
423
 
424
 
425
  # https://github.com/aio-libs/pytest-aiohttp/issues/8 # also AsyncInferenceClient is buggy.
426
- def infer_fn(model_name: str, prompt: str, neg_prompt: str | None = None, height: int | None = None,
427
- width: int | None = None, steps: int | None = None, cfg: int | None = None, seed: int = -1,
428
  pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
429
  if model_name == 'NA':
430
  return None
@@ -446,8 +438,8 @@ def infer_fn(model_name: str, prompt: str, neg_prompt: str | None = None, height
446
  return result
447
 
448
 
449
- def infer_rand_fn(model_name_dummy: str, prompt: str, neg_prompt: str | None = None, height: int | None = None,
450
- width: int | None = None, steps: int | None = None, cfg: int | None = None, seed: int = -1,
451
  pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
452
  import random
453
  if model_name_dummy == 'NA':
@@ -470,3 +462,41 @@ def infer_rand_fn(model_name_dummy: str, prompt: str, neg_prompt: str | None = N
470
  finally:
471
  loop.close()
472
  return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  limit = limit * 20 if check_status and force_gpu else limit * 5
61
  models = []
62
  try:
63
+ model_infos = api.list_models(author=author, #task="text-to-image",
64
  tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit)
65
  except Exception as e:
66
  print(f"Error: Failed to list models.")
 
110
 
111
 
112
  def rename_image(image_path: str | None, model_name: str, save_path: str | None = None):
113
+ import shutil
114
  from datetime import datetime, timezone, timedelta
115
  if image_path is None: return None
116
  dt_now = datetime.now(timezone(timedelta(hours=9)))
 
118
  try:
119
  if Path(image_path).exists():
120
  png_path = "image.png"
121
+ if str(Path(image_path).resolve()) != str(Path(png_path).resolve()): shutil.copy(image_path, png_path)
122
  if save_path is not None:
123
  new_path = str(Path(png_path).resolve().rename(Path(save_path).resolve()))
124
  else:
 
363
 
364
  # https://huggingface.co/docs/api-inference/detailed_parameters
365
  # https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
366
+ def infer_body(client: InferenceClient | gr.Interface | object, model_str: str, prompt: str, neg_prompt: str = "",
367
+ height: int = 0, width: int = 0, steps: int = 0, cfg: int = 0, seed: int = -1):
 
368
  png_path = "image.png"
369
  kwargs = {}
370
+ if height > 0: kwargs["height"] = height
371
+ if width > 0: kwargs["width"] = width
372
+ if steps > 0: kwargs["num_inference_steps"] = steps
373
+ if cfg > 0: cfg = kwargs["guidance_scale"] = cfg
374
+ if seed == -1: kwargs["seed"] = randomize_seed()
375
+ else: kwargs["seed"] = seed
376
  try:
377
  if isinstance(client, InferenceClient):
378
  image = client.text_to_image(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
 
380
  image = client.fn(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
381
  else: return None
382
  if isinstance(image, tuple): return None
383
+ return save_image(image, png_path, model_str, prompt, neg_prompt, height, width, steps, cfg, seed)
 
384
  except Exception as e:
385
  print(e)
386
  raise Exception() from e
387
 
388
 
389
+ async def infer(model_name: str, prompt: str, neg_prompt: str ="", height: int = 0, width: int = 0,
390
+ steps: int = 0, cfg: int = 0, seed: int = -1,
 
391
  save_path: str | None = None, timeout: float = inference_timeout):
 
 
 
 
 
 
392
  model = load_model(model_name)
393
  if not model: return None
394
+ task = asyncio.create_task(asyncio.to_thread(infer_body, model, model_name, prompt, neg_prompt,
395
  height, width, steps, cfg, seed))
396
  await asyncio.sleep(0)
397
  try:
 
415
 
416
 
417
  # https://github.com/aio-libs/pytest-aiohttp/issues/8 # also AsyncInferenceClient is buggy.
418
+ def infer_fn(model_name: str, prompt: str, neg_prompt: str = "", height: int = 0, width: int = 0,
419
+ steps: int = 0, cfg: int = 0, seed: int = -1,
420
  pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
421
  if model_name == 'NA':
422
  return None
 
438
  return result
439
 
440
 
441
+ def infer_rand_fn(model_name_dummy: str, prompt: str, neg_prompt: str = "", height: int = 0, width: int = 0,
442
+ steps: int = 0, cfg: int = 0, seed: int = -1,
443
  pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
444
  import random
445
  if model_name_dummy == 'NA':
 
462
  finally:
463
  loop.close()
464
  return result
465
+
466
+
467
+ def save_image(image, savefile, modelname, prompt, nprompt, height=0, width=0, steps=0, cfg=0, seed=-1):
468
+ from PIL import Image, PngImagePlugin
469
+ import json
470
+ try:
471
+ metadata = {"prompt": prompt, "negative_prompt": nprompt, "Model": {"Model": modelname.split("/")[-1]}}
472
+ if steps > 0: metadata["num_inference_steps"] = steps
473
+ if cfg > 0: metadata["guidance_scale"] = cfg
474
+ if seed != -1: metadata["seed"] = seed
475
+ if width > 0 and height > 0: metadata["resolution"] = f"{width} x {height}"
476
+ metadata_str = json.dumps(metadata)
477
+ info = PngImagePlugin.PngInfo()
478
+ info.add_text("metadata", metadata_str)
479
+ image.save(savefile, "PNG", pnginfo=info)
480
+ return str(Path(savefile).resolve())
481
+ except Exception as e:
482
+ print(f"Failed to save image file: {e}")
483
+ raise Exception(f"Failed to save image file:") from e
484
+
485
+
486
+ def randomize_seed():
487
+ from random import seed, randint
488
+ MAX_SEED = 2**32-1
489
+ seed()
490
+ rseed = randint(0, MAX_SEED)
491
+ return rseed
492
+
493
+
494
+ from translatepy import Translator
495
+ translator = Translator()
496
+ def translate_to_en(input: str):
497
+ try:
498
+ output = str(translator.translate(input, 'English'))
499
+ except Exception as e:
500
+ output = input
501
+ print(e)
502
+ return output
requirements.txt CHANGED
@@ -2,11 +2,8 @@ huggingface_hub
2
  torch==2.2.0
3
  torchvision
4
  accelerate
5
- transformers
6
  optimum[onnxruntime]
7
- spaces
8
  dartrs
9
- httpx==0.13.3
10
- httpcore
11
- googletrans==4.0.0rc1
12
  timm
 
2
  torch==2.2.0
3
  torchvision
4
  accelerate
5
+ transformers==4.44.0
6
  optimum[onnxruntime]
 
7
  dartrs
8
+ translatepy
 
 
9
  timm
tagger/tagger.py CHANGED
@@ -2,10 +2,7 @@ import spaces
2
  from PIL import Image
3
  import torch
4
  import gradio as gr
5
- from transformers import (
6
- AutoImageProcessor,
7
- AutoModelForImageClassification,
8
- )
9
  from pathlib import Path
10
 
11
 
@@ -190,18 +187,16 @@ def convert_danbooru_to_e621_prompt(input_prompt: str = "", prompt_type: str = "
190
  return output_prompt
191
 
192
 
193
- def translate_prompt(prompt: str = ""):
194
- def translate_to_english(prompt):
195
- import httpcore
196
- setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
197
- from googletrans import Translator
198
- translator = Translator()
199
  try:
200
- translated_prompt = translator.translate(prompt, src='auto', dest='en').text
201
- return translated_prompt
202
  except Exception as e:
 
203
  print(e)
204
- return prompt
205
 
206
  def is_japanese(s):
207
  import unicodedata
@@ -223,18 +218,23 @@ def translate_prompt(prompt: str = ""):
223
  return ", ".join(outputs)
224
 
225
 
 
 
 
 
 
 
 
 
 
226
  def translate_prompt_to_ja(prompt: str = ""):
227
- def translate_to_japanese(prompt):
228
- import httpcore
229
- setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
230
- from googletrans import Translator
231
- translator = Translator()
232
  try:
233
- translated_prompt = translator.translate(prompt, src='en', dest='ja').text
234
- return translated_prompt
235
  except Exception as e:
 
236
  print(e)
237
- return prompt
238
 
239
  def is_japanese(s):
240
  import unicodedata
 
2
  from PIL import Image
3
  import torch
4
  import gradio as gr
5
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
 
 
 
6
  from pathlib import Path
7
 
8
 
 
187
  return output_prompt
188
 
189
 
190
+ from translatepy import Translator
191
+ translator = Translator()
192
+ def translate_prompt_old(prompt: str = ""):
193
+ def translate_to_english(input: str):
 
 
194
  try:
195
+ output = str(translator.translate(input, 'English'))
 
196
  except Exception as e:
197
+ output = input
198
  print(e)
199
+ return output
200
 
201
  def is_japanese(s):
202
  import unicodedata
 
218
  return ", ".join(outputs)
219
 
220
 
221
+ def translate_prompt(input: str):
222
+ try:
223
+ output = str(translator.translate(input, 'English'))
224
+ except Exception as e:
225
+ output = input
226
+ print(e)
227
+ return output
228
+
229
+
230
  def translate_prompt_to_ja(prompt: str = ""):
231
+ def translate_to_japanese(input: str):
 
 
 
 
232
  try:
233
+ output = str(translator.translate(input, 'Japanese'))
 
234
  except Exception as e:
235
+ output = input
236
  print(e)
237
+ return output
238
 
239
  def is_japanese(s):
240
  import unicodedata