MrDrmm commited on
Commit
11b92e1
·
verified ·
1 Parent(s): e3109cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -134
app.py CHANGED
@@ -1,135 +1,135 @@
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,
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
-
8
- max_images = 6
9
- MAX_SEED = 2**32-1
10
- load_models(models)
11
-
12
- css = """
13
- .model_info { text-align: center; }
14
- .output { width=112px; height=112px; max_width=112px; max_height=112px; !important; }
15
- .gallery { min_width=512px; min_height=512px; max_height=1024px; !important; }
16
- """
17
-
18
- with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
19
- with gr.Tab("Image Generator"):
20
- with gr.Row():
21
- with gr.Column(scale=10):
22
- with gr.Group():
23
- prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
24
- with gr.Accordion("Advanced options", open=False):
25
- neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
26
- with gr.Row():
27
- width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
28
- height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
29
- steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
30
- with gr.Row():
31
- cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
32
- seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
33
- seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary")
34
- recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
35
- with gr.Row():
36
- positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
37
- positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
38
- negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
39
- negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
40
- with gr.Row():
41
- image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=2)
42
- trans_prompt = gr.Button(value="Translate 📝", variant="secondary", size="sm", scale=2)
43
- clear_prompt = gr.Button(value="Clear 🗑️", variant="secondary", size="sm", scale=1)
44
- with gr.Row():
45
- run_button = gr.Button("Generate Image", variant="primary", scale=6)
46
- random_button = gr.Button("Random Model 🎲", variant="secondary", scale=3)
47
- #stop_button = gr.Button('Stop', interactive=False, variant="stop", scale=1)
48
- with gr.Group():
49
- model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
50
- model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
51
- with gr.Column(scale=10):
52
- with gr.Group():
53
- with gr.Row():
54
- output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
55
- show_download_button=True, show_share_button=False, show_label=False,
56
- interactive=False, min_width=80, visible=True, width=112, height=112) for _ in range(max_images)]
57
- with gr.Group():
58
- results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
59
- container=True, format="png", object_fit="cover", columns=2, rows=2)
60
- image_files = gr.Files(label="Download", interactive=False)
61
- clear_results = gr.Button("Clear Gallery / Download 🗑️", variant="secondary")
62
- with gr.Column():
63
- examples = gr.Examples(
64
- examples = [
65
- ["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"],
66
- ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
67
- ["kafuu chino, 1girl, solo"],
68
- ["1girl"],
69
- ["beautiful sunset"],
70
- ],
71
- inputs=[prompt],
72
- cache_examples=False,
73
- )
74
- with gr.Tab("PNG Info"):
75
- def extract_exif_data(image):
76
- if image is None: return ""
77
- try:
78
- metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
79
- for key in metadata_keys:
80
- if key in image.info:
81
- return image.info[key]
82
- return str(image.info)
83
- except Exception as e:
84
- return f"Error extracting metadata: {str(e)}"
85
- with gr.Row():
86
- with gr.Column():
87
- image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
88
- with gr.Column():
89
- result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
90
-
91
- image_metadata.change(
92
- fn=extract_exif_data,
93
- inputs=[image_metadata],
94
- outputs=[result_metadata],
95
- )
96
- gr.Markdown(
97
- f"""This demo was created in reference to the following demos.<br>
98
- [Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood),
99
- [Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL),
100
- [Yntec/Diffusion80XX](https://huggingface.co/spaces/Yntec/Diffusion80XX).
101
- """
102
- )
103
- gr.DuplicateButton(value="Duplicate Space")
104
- gr.Markdown(f"Just a few edits to *model.py* are all it takes to complete your own collection.")
105
-
106
- #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)
107
- model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)\
108
- .success(warm_model, [model_name], None, queue=False, show_api=False)
109
- for i, o in enumerate(output):
110
- img_i = gr.Number(i, visible=False)
111
- image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
112
- gen_event = gr.on(triggers=[run_button.click, prompt.submit],
113
- 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,
114
- inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
115
- positive_prefix, positive_suffix, negative_prefix, negative_suffix],
116
- outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
117
- gen_event2 = gr.on(triggers=[random_button.click],
118
- 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,
119
- inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
120
- positive_prefix, positive_suffix, negative_prefix, negative_suffix],
121
- outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
122
- o.change(save_gallery, [o, results], [results, image_files], show_api=False)
123
- #stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
124
-
125
- clear_prompt.click(lambda: (None, None), None, [prompt, neg_prompt], queue=False, show_api=False)
126
- clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
127
- recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
128
- [positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
129
- seed_rand.click(randomize_seed, None, [seed], queue=False, show_api=False)
130
- trans_prompt.click(translate_to_en, [prompt], [prompt], queue=False, show_api=False)\
131
- .then(translate_to_en, [neg_prompt], [neg_prompt], queue=False, show_api=False)
132
-
133
- #demo.queue(default_concurrency_limit=200, max_size=200)
134
- demo.launch(max_threads=400, ssr_mode=False)
135
  # https://github.com/gradio-app/gradio/issues/6339
 
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,
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
+
8
+ max_images = 6
9
+ MAX_SEED = 2**32-1
10
+ load_models(models)
11
+
12
+ css = """
13
+ .model_info { text-align: center; }
14
+ .output { width=112px; height=112px; max_width=112px; max_height=112px; !important; }
15
+ .gallery { min_width=512px; min_height=512px; max_height=1024px; !important; }
16
+ """
17
+
18
+ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
19
+ with gr.Tab("Image Generator"):
20
+ with gr.Row():
21
+ with gr.Column(scale=10):
22
+ with gr.Group():
23
+ prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
24
+ with gr.Accordion("Advanced options", open=False):
25
+ neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
26
+ with gr.Row():
27
+ width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=4216, step=32, value=0)
28
+ height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=4216, step=32, value=0)
29
+ steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
30
+ with gr.Row():
31
+ cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
32
+ seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
33
+ seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary")
34
+ recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
35
+ with gr.Row():
36
+ positive_prefix = gr.CheckboxGroup(label="Use Positive Prefix", choices=get_positive_prefix(), value=[])
37
+ positive_suffix = gr.CheckboxGroup(label="Use Positive Suffix", choices=get_positive_suffix(), value=["Common"])
38
+ negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
39
+ negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
40
+ with gr.Row():
41
+ image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=2)
42
+ trans_prompt = gr.Button(value="Translate 📝", variant="secondary", size="sm", scale=2)
43
+ clear_prompt = gr.Button(value="Clear 🗑️", variant="secondary", size="sm", scale=1)
44
+ with gr.Row():
45
+ run_button = gr.Button("Generate Image", variant="primary", scale=6)
46
+ random_button = gr.Button("Random Model 🎲", variant="secondary", scale=3)
47
+ #stop_button = gr.Button('Stop', interactive=False, variant="stop", scale=1)
48
+ with gr.Group():
49
+ model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
50
+ model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_classes="model_info")
51
+ with gr.Column(scale=10):
52
+ with gr.Group():
53
+ with gr.Row():
54
+ output = [gr.Image(label='', elem_classes="output", type="filepath", format="png",
55
+ show_download_button=True, show_share_button=False, show_label=False,
56
+ interactive=False, min_width=80, visible=True, width=112, height=112) for _ in range(max_images)]
57
+ with gr.Group():
58
+ results = gr.Gallery(label="Gallery", elem_classes="gallery", interactive=False, show_download_button=True, show_share_button=False,
59
+ container=True, format="png", object_fit="cover", columns=2, rows=2)
60
+ image_files = gr.Files(label="Download", interactive=False)
61
+ clear_results = gr.Button("Clear Gallery / Download 🗑️", variant="secondary")
62
+ with gr.Column():
63
+ examples = gr.Examples(
64
+ examples = [
65
+ ["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"],
66
+ ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
67
+ ["kafuu chino, 1girl, solo"],
68
+ ["1girl"],
69
+ ["beautiful sunset"],
70
+ ],
71
+ inputs=[prompt],
72
+ cache_examples=False,
73
+ )
74
+ with gr.Tab("PNG Info"):
75
+ def extract_exif_data(image):
76
+ if image is None: return ""
77
+ try:
78
+ metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
79
+ for key in metadata_keys:
80
+ if key in image.info:
81
+ return image.info[key]
82
+ return str(image.info)
83
+ except Exception as e:
84
+ return f"Error extracting metadata: {str(e)}"
85
+ with gr.Row():
86
+ with gr.Column():
87
+ image_metadata = gr.Image(label="Image with metadata", type="pil", sources=["upload"])
88
+ with gr.Column():
89
+ result_metadata = gr.Textbox(label="Metadata", show_label=True, show_copy_button=True, interactive=False, container=True, max_lines=99)
90
+
91
+ image_metadata.change(
92
+ fn=extract_exif_data,
93
+ inputs=[image_metadata],
94
+ outputs=[result_metadata],
95
+ )
96
+ gr.Markdown(
97
+ f"""This demo was created in reference to the following demos.<br>
98
+ [Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood),
99
+ [Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL),
100
+ [Yntec/Diffusion80XX](https://huggingface.co/spaces/Yntec/Diffusion80XX).
101
+ """
102
+ )
103
+ gr.DuplicateButton(value="Duplicate Space")
104
+ gr.Markdown(f"Just a few edits to *model.py* are all it takes to complete your own collection.")
105
+
106
+ #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)
107
+ model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)\
108
+ .success(warm_model, [model_name], None, queue=False, show_api=False)
109
+ for i, o in enumerate(output):
110
+ img_i = gr.Number(i, visible=False)
111
+ image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
112
+ gen_event = gr.on(triggers=[run_button.click, prompt.submit],
113
+ 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,
114
+ inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
115
+ positive_prefix, positive_suffix, negative_prefix, negative_suffix],
116
+ outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
117
+ gen_event2 = gr.on(triggers=[random_button.click],
118
+ 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,
119
+ inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
120
+ positive_prefix, positive_suffix, negative_prefix, negative_suffix],
121
+ outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
122
+ o.change(save_gallery, [o, results], [results, image_files], show_api=False)
123
+ #stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
124
+
125
+ clear_prompt.click(lambda: (None, None), None, [prompt, neg_prompt], queue=False, show_api=False)
126
+ clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
127
+ recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
128
+ [positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
129
+ seed_rand.click(randomize_seed, None, [seed], queue=False, show_api=False)
130
+ trans_prompt.click(translate_to_en, [prompt], [prompt], queue=False, show_api=False)\
131
+ .then(translate_to_en, [neg_prompt], [neg_prompt], queue=False, show_api=False)
132
+
133
+ #demo.queue(default_concurrency_limit=200, max_size=200)
134
+ demo.launch(max_threads=400, ssr_mode=False)
135
  # https://github.com/gradio-app/gradio/issues/6339