John6666 commited on
Commit
e7360d0
β€’
1 Parent(s): 3849f04

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -33
app.py CHANGED
@@ -6,6 +6,7 @@ import asyncio
6
  from threading import RLock
7
  lock = RLock()
8
 
 
9
  def load_fn(models):
10
  global models_load
11
  models_load = {}
@@ -24,8 +25,10 @@ load_fn(models)
24
 
25
 
26
  num_models = 6
27
- default_models = models[:num_models]
28
  timeout = 60
 
 
29
 
30
  def extend_choices(choices):
31
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
@@ -85,23 +88,6 @@ def add_gallery(image, model_str, gallery):
85
  return gallery
86
 
87
 
88
- def gen_fn_gallery(model_str, prompt, gallery):
89
- if gallery is None: gallery = []
90
- if model_str == 'NA':
91
- yield gallery
92
- try:
93
- loop = asyncio.new_event_loop()
94
- result = loop.run_until_complete(infer(model_str, prompt, timeout))
95
- with lock:
96
- if result: gallery.insert(0, result)
97
- except (Exception, asyncio.CancelledError) as e:
98
- print(e)
99
- print(f"Task aborted: {model_str}")
100
- finally:
101
- loop.close()
102
- yield gallery
103
-
104
-
105
  CSS="""
106
  #container { max-width: 1200px; margin: 0 auto; !important; }
107
  .output { width=112px; height=112px; !important; }
@@ -116,15 +102,15 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
116
  with gr.Row():
117
  gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total', scale=2)
118
  stop_button = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
119
- gen_button.click(lambda: gr.update(interactive = True), None, stop_button)
120
  gr.Markdown("Scroll down to see more images and select models.", elem_classes="guide")
121
 
122
  with gr.Column(scale=1):
123
  with gr.Group():
124
  with gr.Row():
125
- output = [gr.Image(label=m, show_download_button=True, elem_classes="output", interactive=False, min_width=80, show_share_button=False, visible=True) for m in default_models]
126
- #output = [gr.Image(label=m, show_download_button=True, elem_classes="output", interactive=False, show_share_button=True) for m in default_models]
127
- #output = [gr.Gallery(label=m, show_download_button=True, elem_classes="output", interactive=False, show_share_button=True, container=True, format="png", object_fit="cover") for m in default_models]
128
  current_models = [gr.Textbox(m, visible=False) for m in default_models]
129
 
130
  with gr.Column(scale=2):
@@ -133,27 +119,21 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
133
  preview=True, object_fit="cover", columns=2, rows=2)
134
 
135
  for m, o in zip(current_models, output):
136
- #gen_event = gen_button.click(gen_fn, [m, txt_input], o)
137
- #gen_event = gen_button.click(gen_fn_gallery, [m, txt_input, o], o)
138
  gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn, inputs=[m, txt_input], outputs=[o])
139
  o.change(add_gallery, [o, m, gallery], [gallery])
140
- stop_button.click(lambda: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
141
 
142
  with gr.Column(scale=4):
143
  with gr.Accordion('Model selection'):
144
  model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value=default_models, interactive=True)
145
  model_choice.change(update_imgbox, model_choice, output)
146
- #model_choice.change(update_imgbox_gallery, model_choice, output)
147
  model_choice.change(extend_choices, model_choice, current_models)
148
 
149
  with gr.Tab('Single model'):
150
  with gr.Column(scale=2):
151
  model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0])
152
  txt_input2 = gr.Textbox(label='Your prompt:', lines=4)
153
-
154
- max_images = 6
155
  num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
156
-
157
  with gr.Row():
158
  gen_button2 = gr.Button('Generate', scale=2)
159
  stop_button2 = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
@@ -162,8 +142,9 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
162
  with gr.Column(scale=1):
163
  with gr.Group():
164
  with gr.Row():
165
- output2 = [gr.Image(label = '', show_download_button=True, elem_classes="output", interactive=False, min_width=80, visible=True, show_share_button=False, show_label=False) for _ in range(max_images)]
166
- #output2 = [gr.Image(label = '') for _ in range(max_images)]
 
167
 
168
  with gr.Column(scale=2):
169
  gallery2 = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
@@ -173,8 +154,9 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
173
  for i, o in enumerate(output2):
174
  img_i = gr.Number(i, visible = False)
175
  num_images.change(lambda i, n: gr.update(visible = (i < n)), [img_i, num_images], o)
176
- #gen_event2 = gen_button2.click(lambda i, n, m, t: gen_fn(m, t) if (i < n) else None, [img_i, num_images, model_choice2, txt_input2], o)
177
- gen_event2 = gr.on(triggers=[gen_button2.click, txt_input2.submit], fn=lambda i, n, m, t: gen_fn(m, t) if (i < n) else None, inputs=[img_i, num_images, model_choice2, txt_input2], outputs=[o])
 
178
  o.change(add_gallery, [o, model_choice2, gallery2], [gallery2])
179
  stop_button2.click(lambda: gr.update(interactive = False), None, stop_button2, cancels = [gen_event2])
180
 
 
6
  from threading import RLock
7
  lock = RLock()
8
 
9
+
10
  def load_fn(models):
11
  global models_load
12
  models_load = {}
 
25
 
26
 
27
  num_models = 6
28
+ max_images = 6
29
  timeout = 60
30
+ default_models = models[:num_models]
31
+
32
 
33
  def extend_choices(choices):
34
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
 
88
  return gallery
89
 
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  CSS="""
92
  #container { max-width: 1200px; margin: 0 auto; !important; }
93
  .output { width=112px; height=112px; !important; }
 
102
  with gr.Row():
103
  gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total', scale=2)
104
  stop_button = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
105
+ gen_button.click(lambda: gr.update(interactive=True), None, stop_button)
106
  gr.Markdown("Scroll down to see more images and select models.", elem_classes="guide")
107
 
108
  with gr.Column(scale=1):
109
  with gr.Group():
110
  with gr.Row():
111
+ output = [gr.Image(label=m, show_download_button=True, elem_classes="output",
112
+ interactive=False, min_width=80, show_share_button=False, format=".png",
113
+ visible=True) for m in default_models]
114
  current_models = [gr.Textbox(m, visible=False) for m in default_models]
115
 
116
  with gr.Column(scale=2):
 
119
  preview=True, object_fit="cover", columns=2, rows=2)
120
 
121
  for m, o in zip(current_models, output):
 
 
122
  gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn, inputs=[m, txt_input], outputs=[o])
123
  o.change(add_gallery, [o, m, gallery], [gallery])
124
+ stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels = [gen_event])
125
 
126
  with gr.Column(scale=4):
127
  with gr.Accordion('Model selection'):
128
  model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value=default_models, interactive=True)
129
  model_choice.change(update_imgbox, model_choice, output)
 
130
  model_choice.change(extend_choices, model_choice, current_models)
131
 
132
  with gr.Tab('Single model'):
133
  with gr.Column(scale=2):
134
  model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0])
135
  txt_input2 = gr.Textbox(label='Your prompt:', lines=4)
 
 
136
  num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
 
137
  with gr.Row():
138
  gen_button2 = gr.Button('Generate', scale=2)
139
  stop_button2 = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
 
142
  with gr.Column(scale=1):
143
  with gr.Group():
144
  with gr.Row():
145
+ output2 = [gr.Image(label='', show_download_button=True, elem_classes="output",
146
+ interactive=False, min_width=80, visible=True, format=".png",
147
+ show_share_button=False, show_label=False) for _ in range(max_images)]
148
 
149
  with gr.Column(scale=2):
150
  gallery2 = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
 
154
  for i, o in enumerate(output2):
155
  img_i = gr.Number(i, visible = False)
156
  num_images.change(lambda i, n: gr.update(visible = (i < n)), [img_i, num_images], o)
157
+ gen_event2 = gr.on(triggers=[gen_button2.click, txt_input2.submit],
158
+ fn=lambda i, n, m, t: gen_fn(m, t) if (i < n) else None,
159
+ inputs=[img_i, num_images, model_choice2, txt_input2], outputs=[o])
160
  o.change(add_gallery, [o, model_choice2, gallery2], [gallery2])
161
  stop_button2.click(lambda: gr.update(interactive = False), None, stop_button2, cancels = [gen_event2])
162