Yntec commited on
Commit
8f77e09
·
verified ·
1 Parent(s): 829b857

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -3
app.py CHANGED
@@ -29,9 +29,10 @@ load_fn(models)
29
 
30
 
31
  num_models = 6
32
-
33
  default_models = models[:num_models]
34
  inference_timeout = 600
 
35
 
36
  def extend_choices(choices):
37
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
@@ -47,7 +48,42 @@ def gen_fn(model_str, prompt):
47
  noise = str('') #str(randint(0, 99999999999))
48
  return models_load[model_str](f'{prompt} {noise}')
49
 
50
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  with gr.Blocks() as demo:
53
  with gr.Tab('Toy World'):
@@ -84,7 +120,45 @@ with gr.Blocks() as demo:
84
  gr.HTML(
85
  """
86
  <div class="footer">
87
- <p> Use 901 models up to six at a time at <a href="https://huggingface.co/spaces/Yntec/ToyWorld">ToyWorld</a></a>!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  </p>
89
  """
90
  )
 
29
 
30
 
31
  num_models = 6
32
+ MAX_SEED = 3999999999
33
  default_models = models[:num_models]
34
  inference_timeout = 600
35
+ starting_seed = randint(1941, 2024)
36
 
37
  def extend_choices(choices):
38
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
 
48
  noise = str('') #str(randint(0, 99999999999))
49
  return models_load[model_str](f'{prompt} {noise}')
50
 
51
+ async def infer(model_str, prompt, seed=1, timeout=inference_timeout):
52
+ from pathlib import Path
53
+ kwargs = {}
54
+ noise = ""
55
+ kwargs["seed"] = seed
56
+ task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
57
+ prompt=f'{prompt} {noise}', **kwargs, token=HF_TOKEN))
58
+ await asyncio.sleep(0)
59
+ try:
60
+ result = await asyncio.wait_for(task, timeout=timeout)
61
+ except (Exception, asyncio.TimeoutError) as e:
62
+ print(e)
63
+ print(f"Task timed out: {model_str}")
64
+ if not task.done(): task.cancel()
65
+ result = None
66
+ if task.done() and result is not None:
67
+ with lock:
68
+ png_path = "image.png"
69
+ result.save(png_path)
70
+ image = str(Path(png_path).resolve())
71
+ return image
72
+ return None
73
+
74
+ def gen_fnseed(model_str, prompt, seed=1):
75
+ if model_str == 'NA':
76
+ return None
77
+ try:
78
+ loop = asyncio.new_event_loop()
79
+ result = loop.run_until_complete(infer(model_str, prompt, seed, inference_timeout))
80
+ except (Exception, asyncio.CancelledError) as e:
81
+ print(e)
82
+ print(f"Task aborted: {model_str}")
83
+ result = None
84
+ finally:
85
+ loop.close()
86
+ return result
87
 
88
  with gr.Blocks() as demo:
89
  with gr.Tab('Toy World'):
 
120
  gr.HTML(
121
  """
122
  <div class="footer">
123
+ <p> Based on the <a href="https://huggingface.co/spaces/John6666/hfd_test_nostopbutton">Huggingface NoStopButton</a> Space by John6666, <a href="https://huggingface.co/spaces/derwahnsinn/TestGen">TestGen</a> Space by derwahnsinn, the <a href="https://huggingface.co/spaces/RdnUser77/SpacIO_v1">SpacIO</a> Space by RdnUser77 and Omnibus's Maximum Multiplier! For 6 images with the same model check out the <a href="https://huggingface.co/spaces/Yntec/PrintingPress">Printing Press</a>, for the classic UI with prompt enhancer try <a href="https://huggingface.co/spaces/Yntec/blitz_diffusion">Blitz Diffusion!</a>
124
+ </p>
125
+ """
126
+ with gr.Tab(' """<img src="https://huggingface.co/Yntec/DucHaitenLofi/resolve/main/NEW.webp" alt="NEW!" style="width:32px;height:16px;">""" Use seeds!'):
127
+ txt_inputseed = gr.Textbox(label='Your prompt:', lines=4)
128
+ gen_buttonseed = gr.Button('Generate up to 6 images with the same seed in up to 3 minutes total')
129
+ seed = gr.Slider(label="Use a seed to replicate the same image later (maximum 3999999999)", minimum=0, maximum=MAX_SEED, step=1, value=starting_seed, scale=3)
130
+ #stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
131
+ gen_buttonseed.click(lambda s: gr.update(interactive = True), None)
132
+ gr.HTML(
133
+ """
134
+ <div style="text-align: center; max-width: 1200px; margin: 0 auto;">
135
+ <div>
136
+ <body>
137
+ <div class="center"><p style="margin-bottom: 10px; color: #000000;">Scroll down to see more images and select models.</p>
138
+ </div>
139
+ </body>
140
+ </div>
141
+ </div>
142
+ """
143
+ )
144
+ with gr.Row():
145
+ output = [gr.Image(label = m, min_width=480) for m in default_models]
146
+ current_models = [gr.Textbox(m, visible = False) for m in default_models]
147
+
148
+ for m, o in zip(current_models, output):
149
+ gen_eventseed = gr.on(triggers=[gen_buttonseed.click, txt_inputseed.submit], fn=gen_fnseed,
150
+ inputs=[m, txt_inputseed, seed], outputs=[o], concurrency_limit=None, queue=False)
151
+ #stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
152
+ with gr.Accordion('Model selection'):
153
+ model_choice = gr.CheckboxGroup(models, label = f'Choose up to {int(num_models)} different models from the {len(models)} available!', value=default_models, interactive=True)
154
+ #model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 2 available! Untick them to only use one!', value = default_models, multiselect = True, max_choices = num_models, interactive = True, filterable = False)
155
+ model_choice.change(update_imgbox, model_choice, output)
156
+ model_choice.change(extend_choices, model_choice, current_models)
157
+ with gr.Row():
158
+ gr.HTML(
159
+ """
160
+ <div class="footer">
161
+ <p> Based on the <a href="https://huggingface.co/spaces/John6666/hfd_test_nostopbutton">Huggingface NoStopButton</a> Space by John6666, <a href="https://huggingface.co/spaces/derwahnsinn/TestGen">TestGen</a> Space by derwahnsinn, the <a href="https://huggingface.co/spaces/RdnUser77/SpacIO_v1">SpacIO</a> Space by RdnUser77 and Omnibus's Maximum Multiplier! For 6 images with the same model check out the <a href="https://huggingface.co/spaces/Yntec/PrintingPress">Printing Press</a>, for the classic UI with prompt enhancer try <a href="https://huggingface.co/spaces/Yntec/blitz_diffusion">Blitz Diffusion!</a>
162
  </p>
163
  """
164
  )