DazDin commited on
Commit
79727f8
·
verified ·
1 Parent(s): 74ded54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +233 -117
app.py CHANGED
@@ -1,118 +1,234 @@
1
  import gradio as gr
2
- import time
3
-
4
- models =[
5
- "Keltezaa/NSFW_MASTER_FLUX",
6
- "pimpilikipilapi1/NSFW_master",
7
- "Keltezaa/Fingering",
8
- "DiegoJR1973/NSFW-TrioHMH-Flux",
9
- "prashanth970/flux-lora-uncensored",
10
- "black-forest-labs/FLUX.1-dev",
11
- ]
12
-
13
- model_functions = {}
14
- model_idx = 1
15
- for model_path in models:
16
- try:
17
- model_functions[model_idx] = gr.Interface.load(f"models/{model_path}", live=False, preprocess=True, postprocess=False)
18
- except Exception as error:
19
- def the_fn(txt):
20
- return None
21
- model_functions[model_idx] = gr.Interface(fn=the_fn, inputs=["text"], outputs=["image"])
22
- model_idx+=1
23
-
24
-
25
- def send_it_idx(idx):
26
- def send_it_fn(prompt):
27
- output = (model_functions.get(str(idx)) or model_functions.get(str(1)))(prompt)
28
- return output
29
- return send_it_fn
30
-
31
- def get_prompts(prompt_text):
32
- return prompt_text
33
-
34
- def clear_it(val):
35
- if int(val) != 0:
36
- val = 0
37
- else:
38
- val = 0
39
- return val
40
-
41
- def all_task_end(cnt,t_stamp):
42
- to = t_stamp + 360
43
- et = time.time()
44
- if et > to and t_stamp != 0:
45
- d = gr.update(value=0)
46
- tog = gr.update(value=1)
47
- else:
48
- if cnt != 0:
49
- d = gr.update(value=et)
50
- else:
51
- d = gr.update(value=0)
52
- tog = gr.update(value=0)
53
- return d, tog
54
-
55
- def all_task_start():
56
- print("\n\n\n\n\n\n\n")
57
- t = time.gmtime()
58
- t_stamp = time.time()
59
- current_time = time.strftime("%H:%M:%S", t)
60
- return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)
61
-
62
- def clear_fn():
63
- nn = len(models)
64
- return tuple([None, *[None for _ in range(nn)]])
65
-
66
-
67
-
68
- with gr.Blocks(title="SD Models") as my_interface:
69
- with gr.Column(scale=12):
70
- with gr.Row():
71
- with gr.Row(scale=6):
72
- primary_prompt=gr.Textbox(label="Prompt", value="")
73
- with gr.Row(scale=6):
74
- with gr.Row():
75
- run=gr.Button("Run",variant="primary")
76
- clear_btn=gr.Button("Clear")
77
- with gr.Row():
78
- sd_outputs = {}
79
- model_idx = 1
80
- for model_path in models:
81
- with gr.Column(scale=3, min_width=200):
82
- with gr.Box():
83
- sd_outputs[model_idx] = gr.Image(label=model_path)
84
- model_idx += 1
85
-
86
- with gr.Row(visible=False):
87
- start_box=gr.Number(interactive=False)
88
- end_box=gr.Number(interactive=False)
89
- tog_box=gr.Textbox(value=0,interactive=False)
90
-
91
- start_box.change(
92
- all_task_end,
93
- [start_box, end_box],
94
- [start_box, tog_box],
95
- every=1,
96
- show_progress=True)
97
-
98
- primary_prompt.submit(all_task_start, None, [start_box, end_box, tog_box])
99
- run.click(all_task_start, None, [start_box, end_box, tog_box])
100
- runs_dict = {}
101
- model_idx = 1
102
- for model_path in models:
103
- runs_dict[model_idx] = run.click(model_functions[model_idx], inputs=[primary_prompt], outputs=[sd_outputs[model_idx]])
104
- model_idx += 1
105
-
106
- clear_btn.click(
107
- clear_fn,
108
- None,
109
- [primary_prompt, *list(sd_outputs.values())],
110
- cancels=[*list(runs_dict.values())])
111
- tog_box.change(
112
- clear_it,
113
- tog_box,
114
- tog_box,
115
- cancels=[*list(runs_dict.values())])
116
-
117
- my_interface.queue(concurrency_count=100, status_update_rate=1)
118
- my_interface.launch(inline=True, show_api=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from random import randint
3
+ from all_models import models
4
+
5
+ def load_fn(models):
6
+ global models_load
7
+ models_load = {}
8
+ for model in models:
9
+ if model not in models_load.keys():
10
+ try:
11
+ m = gr.load(f'models/{model}')
12
+ except Exception as error:
13
+ m = gr.Interface(lambda txt: None, ['text'], ['image'])
14
+ models_load.update({model: m})
15
+
16
+ load_fn(models)
17
+
18
+ num_models = len(models)
19
+ default_models = models[:num_models]
20
+
21
+ def extend_choices(choices):
22
+ return choices + (num_models - len(choices)) * ['NA']
23
+
24
+ def update_imgbox(choices):
25
+ choices_plus = extend_choices(choices)
26
+ return [gr.Image(None, label=m, visible=(m != 'NA'), elem_id="custom_image") for m in choices_plus]
27
+
28
+ def gen_fn(model_str, prompt):
29
+ if model_str == 'NA':
30
+ return None
31
+ noise = str(randint(0, 9999999))
32
+ return models_load[model_str](f'{prompt} {noise}')
33
+
34
+ def make_me():
35
+ with gr.Row():
36
+ with gr.Column(scale=1):
37
+ txt_input = gr.Textbox(label='Your prompt:', lines=3, container=False, elem_id="custom_textbox", placeholder="Prompt")
38
+ with gr.Row():
39
+ gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
40
+ stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
41
+
42
+ def on_generate_click():
43
+ return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
44
+
45
+ def on_stop_click():
46
+ return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
47
+
48
+ gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
49
+ stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
50
+
51
+ with gr.Row():
52
+ output = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models]
53
+ current_models = [gr.Textbox(m, visible=False) for m in default_models]
54
+ for m, o in zip(current_models, output):
55
+ gen_event = gen_button.click(gen_fn, [m, txt_input], o)
56
+ stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
57
+
58
+ with gr.Accordion('Model selection', elem_id="custom_accordion"):
59
+ model_choice = gr.CheckboxGroup(models, label=f'{num_models} different models selected', value=default_models, interactive=True, elem_id="custom_checkbox_group")
60
+ model_choice.change(update_imgbox, model_choice, output)
61
+ model_choice.change(extend_choices, model_choice, current_models)
62
+
63
+ with gr.Row():
64
+ gr.HTML("")
65
+
66
+ custom_css = """
67
+ :root {
68
+ --body-background-fill: #2d3d4f;
69
+ }
70
+
71
+ body {
72
+ background-color: var(--body-background-fill) !important;
73
+ color: #2d3d4f;
74
+ margin: 0;
75
+ padding: 0;
76
+ font-family: Arial, sans-serif;
77
+ height: 100vh;
78
+ overflow-y: auto;
79
+ }
80
+
81
+ .gradio-container {
82
+ background-color: #2d3d4f;
83
+ color: #c5c6c7;
84
+ padding: 20px;
85
+ border-radius: 8px;
86
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
87
+ width: 100%;
88
+ max-width: 1200px;
89
+ margin: 20px auto;
90
+ display: block;
91
+ min-height: 100vh;
92
+ }
93
+
94
+ .app_title {
95
+ background-color: #2d3d4f;
96
+ color: #c5c6c7;
97
+ padding: 10px 20px;
98
+ border-bottom: 1px solid #3b4252;
99
+ text-align: center;
100
+ font-size: 24px;
101
+ font-weight: bold;
102
+ width: 100%;
103
+ box-sizing: border-box;
104
+ margin-bottom: 20px;
105
+ }
106
+
107
+ .custom_textbox {
108
+ background-color: #2d343f;
109
+ border: 1px solid #3b4252;
110
+ color: #7f8184;
111
+ padding: 10px;
112
+ border-radius: 4px;
113
+ margin-bottom: 10px;
114
+ width: 100%;
115
+ box-sizing: border-box;
116
+ }
117
+
118
+ .custom_gen_button {
119
+ background-color: #8b38ff;
120
+ border: 1px solid #ffffff;
121
+ color: blue;
122
+ padding: 15px 32px;
123
+ text-align: center;
124
+ text-decoration: none;
125
+ display: inline-block;
126
+ font-size: 16px;
127
+ margin: 4px 2px;
128
+ cursor: pointer;
129
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
130
+ transition: transform 0.2s, box-shadow 0.2s;
131
+ border-radius: 4px;
132
+ }
133
+ .custom_gen_button:hover {
134
+ transform: translateY(-2px);
135
+ box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
136
+ }
137
+ .custom_stop_button {
138
+ background-color: #6200ea;
139
+ border: 1px solid #ffffff;
140
+ color: blue;
141
+ padding: 15px 32px;
142
+ text-align: center;
143
+ text-decoration: none;
144
+ display: inline-block;
145
+ font-size: 16px;
146
+ margin: 4px 2px;
147
+ cursor: pointer;
148
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
149
+ transition: transform 0.2s, box-shadow 0.2s;
150
+ border-radius: 4px;
151
+ }
152
+ .custom_stop_button:hover {
153
+ transform: translateY(-2px);
154
+ box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
155
+ }
156
+
157
+ .custom_image {
158
+ border: 1px solid #3b4252;
159
+ background-color: #2d343f;
160
+ border-radius: 4px;
161
+ margin: 10px;
162
+ max-width: 100%;
163
+ box-sizing: border-box;
164
+ }
165
+
166
+ .custom_accordion {
167
+ background-color: #2d3d4f;
168
+ color: #7f8184;
169
+ border: 1px solid #3b4252;
170
+ border-radius: 4px;
171
+ margin-top: 20px;
172
+ width: 100%;
173
+ box-sizing: border-box;
174
+ transition: margin 0.2s ease;
175
+ }
176
+
177
+ .custom_accordion .gr-accordion-header {
178
+ background-color: #2d3d4f;
179
+ color: #7f8184;
180
+ padding: 10px 20px;
181
+ border-bottom: 1px solid #5b6270;
182
+ cursor: pointer;
183
+ font-size: 18px;
184
+ font-weight: bold;
185
+ height: 40px;
186
+ display: flex;
187
+ align-items: center;
188
+ }
189
+
190
+ .custom_accordion .gr-accordion-header:hover {
191
+ background-color: #2d3d4f;
192
+ }
193
+
194
+ .custom_accordion .gr-accordion-content {
195
+ padding: 10px 20px;
196
+ background-color: #2d3d4f;
197
+ border-top: 1px solid #5b6270;
198
+ max-height: 0;
199
+ overflow: hidden;
200
+ transition: max-height 0.2s ease;
201
+ }
202
+
203
+ .custom_accordion .gr-accordion-content.open {
204
+ max-height: 500px;
205
+ }
206
+
207
+ .custom_checkbox_group {
208
+ background-color: #2d343f;
209
+ border: 1px solid #3b4252;
210
+ color: #7f8184;
211
+ border-radius: 4px;
212
+ padding: 10px;
213
+ width: 100%;
214
+ box-sizing: border-box;
215
+ }
216
+
217
+ @media (max-width: 768px) {
218
+ .gradio-container {
219
+ width: 100%;
220
+ margin: 0;
221
+ padding: 10px;
222
+ }
223
+ .custom_textbox,.custom_image,.custom_checkbox_group {
224
+ width: 100%;
225
+ box-sizing: border-box;
226
+ }
227
+ }
228
+ """
229
+
230
+ with gr.Blocks(css=custom_css) as demo:
231
+ make_me()
232
+
233
+ demo.queue(concurrency_count=200)
234
+ demo.launch()