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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -27
app.py CHANGED
@@ -42,6 +42,7 @@ def update_imgbox_gallery(choices):
42
 
43
 
44
  async def infer(model_str, prompt, timeout):
 
45
  noise = ""
46
  rand = randint(1, 500)
47
  for i in range(rand):
@@ -52,10 +53,14 @@ async def infer(model_str, prompt, timeout):
52
  result = await asyncio.wait_for(task, timeout=timeout)
53
  except (Exception, asyncio.TimeoutError) as e:
54
  print(e)
55
- print(f"Task timed-out: {model_str}")
56
  if not task.done(): task.cancel()
57
  result = None
58
- return result
 
 
 
 
59
 
60
 
61
  def gen_fn(model_str, prompt):
@@ -73,21 +78,6 @@ def gen_fn(model_str, prompt):
73
  return result
74
 
75
 
76
- def gen_fn_single_gallery(model_str, prompt):
77
- if model_str == 'NA':
78
- return None, gr.update(visible=True)
79
- try:
80
- loop = asyncio.new_event_loop()
81
- result = loop.run_until_complete(infer(model_str, prompt, timeout))
82
- except (Exception, asyncio.CancelledError) as e:
83
- print(e)
84
- print(f"Task aborted: {model_str}")
85
- result = None
86
- finally:
87
- loop.close()
88
- return result, gr.update(visible=True)
89
-
90
-
91
  def add_gallery(image, model_str, gallery):
92
  if gallery is None: gallery = []
93
  with lock:
@@ -113,25 +103,26 @@ def gen_fn_gallery(model_str, prompt, gallery):
113
 
114
 
115
  CSS="""
116
- .output { width=120px; height=120px; !important; }
117
- .gallery { width=100%; height=512px; max_height=512px !important; }
118
- .guide { text-align: center; max-width: 1200px; margin: 0 auto; !important; }
 
119
  """
120
 
121
- with gr.Blocks(fill_width=True, css=CSS) as demo:
122
  with gr.Tab('The Dream'):
123
  with gr.Column(scale=2):
124
  txt_input = gr.Textbox(label='Your prompt:', lines=4)
125
  with gr.Row():
126
  gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total', scale=2)
127
  stop_button = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
128
- gen_button.click(lambda: gr.update(interactive = True), None, stop_button)
129
  gr.Markdown("Scroll down to see more images and select models.", elem_classes="guide")
130
 
131
  with gr.Column(scale=1):
132
  with gr.Group():
133
  with gr.Row():
134
- output = [gr.Image(label=m, show_download_button=True, elem_classes="output", interactive=False, show_share_button=True, min_width=80, visible=True) for m in default_models]
135
  #output = [gr.Image(label=m, show_download_button=True, elem_classes="output", interactive=False, show_share_button=True) for m in default_models]
136
  #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]
137
  current_models = [gr.Textbox(m, visible=False) for m in default_models]
@@ -139,12 +130,11 @@ with gr.Blocks(fill_width=True, css=CSS) as demo:
139
  with gr.Column(scale=2):
140
  gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
141
  interactive=False, show_share_button=True, container=True, format="png",
142
- object_fit="cover", columns=2) # , height=768
143
-
144
  for m, o in zip(current_models, output):
145
  #gen_event = gen_button.click(gen_fn, [m, txt_input], o)
146
  #gen_event = gen_button.click(gen_fn_gallery, [m, txt_input, o], o)
147
- #gen_event = gen_button.click(gen_fn, [m, txt_input], [o])
148
  gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn, inputs=[m, txt_input], outputs=[o])
149
  o.change(add_gallery, [o, m, gallery], [gallery])
150
  stop_button.click(lambda: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
@@ -155,7 +145,40 @@ with gr.Blocks(fill_width=True, css=CSS) as demo:
155
  model_choice.change(update_imgbox, model_choice, output)
156
  #model_choice.change(update_imgbox_gallery, model_choice, output)
157
  model_choice.change(extend_choices, model_choice, current_models)
158
- gr.Markdown("Based on the [TestGen](https://huggingface.co/spaces/derwahnsinn/TestGen) Space by derwahnsinn, the [SpacIO](https://huggingface.co/spaces/RdnUser77/SpacIO_v1) Space by RdnUser77 and Omnibus's Maximum Multiplier!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  demo.queue()
161
  demo.launch()
 
42
 
43
 
44
  async def infer(model_str, prompt, timeout):
45
+ from PIL import Image
46
  noise = ""
47
  rand = randint(1, 500)
48
  for i in range(rand):
 
53
  result = await asyncio.wait_for(task, timeout=timeout)
54
  except (Exception, asyncio.TimeoutError) as e:
55
  print(e)
56
+ print(f"Task timed out: {model_str}")
57
  if not task.done(): task.cancel()
58
  result = None
59
+ if task.done() and result is not None:
60
+ with lock:
61
+ image = Image.open(result).convert('RGBA')
62
+ return image
63
+ return None
64
 
65
 
66
  def gen_fn(model_str, prompt):
 
78
  return result
79
 
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  def add_gallery(image, model_str, gallery):
82
  if gallery is None: gallery = []
83
  with lock:
 
103
 
104
 
105
  CSS="""
106
+ #container { max-width: 1200px; margin: 0 auto; !important; }
107
+ .output { width=112px; height=112px; !important; }
108
+ .gallery { width=100%; min_height=768px; !important; }
109
+ .guide { text-align: center; !important; }
110
  """
111
 
112
+ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
113
  with gr.Tab('The Dream'):
114
  with gr.Column(scale=2):
115
  txt_input = gr.Textbox(label='Your prompt:', lines=4)
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]
 
130
  with gr.Column(scale=2):
131
  gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
132
  interactive=False, show_share_button=True, container=True, format="png",
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])
 
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)
160
+ gen_button2.click(lambda: gr.update(interactive = True), None, stop_button2)
161
+
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",
170
+ interactive=False, show_share_button=True, container=True, format="png",
171
+ preview=True, object_fit="cover", columns=2, rows=2)
172
+
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
+
181
+ gr.Markdown("Based on the [TestGen](https://huggingface.co/spaces/derwahnsinn/TestGen) Space by derwahnsinn, the [SpacIO](https://huggingface.co/spaces/RdnUser77/SpacIO_v1) Space by RdnUser77 and Omnibus's Maximum Multiplier!")
182
 
183
  demo.queue()
184
  demo.launch()