Yntec commited on
Commit
d3f2a30
1 Parent(s): 7c66541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -2,6 +2,13 @@ import gradio as gr
2
  from random import randint
3
  from all_models import models
4
 
 
 
 
 
 
 
 
5
 
6
 
7
  def load_fn(models):
@@ -11,9 +18,10 @@ def load_fn(models):
11
  for model in models:
12
  if model not in models_load.keys():
13
  try:
14
- m = gr.load(f'models/{model}')
15
  except Exception as error:
16
- m = gr.Interface(lambda txt: None, ['text'], ['image'])
 
17
  models_load.update({model: m})
18
 
19
 
@@ -21,18 +29,17 @@ load_fn(models)
21
 
22
 
23
  num_models = 6
24
- default_models = models[:num_models]
25
-
26
 
 
 
27
 
28
  def extend_choices(choices):
29
- return choices + (num_models - len(choices)) * ['NA']
30
 
31
 
32
  def update_imgbox(choices):
33
- choices_plus = extend_choices(choices)
34
- return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
35
-
36
 
37
  def gen_fn(model_str, prompt):
38
  if model_str == 'NA':
@@ -44,10 +51,10 @@ def gen_fn(model_str, prompt):
44
 
45
  with gr.Blocks() as demo:
46
  with gr.Tab('Toy World'):
47
- txt_input = gr.Textbox(label = 'Your prompt:', lines=4).style(container=False,min_width=1200)
48
  gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
49
- stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
50
- gen_button.click(lambda s: gr.update(interactive = True), None, stop_button)
51
  gr.HTML(
52
  """
53
  <div style="text-align: center; max-width: 1200px; margin: 0 auto;">
@@ -65,17 +72,19 @@ with gr.Blocks() as demo:
65
  current_models = [gr.Textbox(m, visible = False) for m in default_models]
66
 
67
  for m, o in zip(current_models, output):
68
- gen_event = gen_button.click(gen_fn, [m, txt_input], o)
69
- stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
 
70
  with gr.Accordion('Model selection'):
71
- model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 901 available! Untick the selected ones to select different ones!', value = default_models, multiselect = True, max_choices = num_models, interactive = True, filterable = False)
 
72
  model_choice.change(update_imgbox, model_choice, output)
73
  model_choice.change(extend_choices, model_choice, current_models)
74
  with gr.Row():
75
  gr.HTML(
76
  """
77
  <div class="footer">
78
- <p> Based on the <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>
79
  </p>
80
  """
81
  )
 
2
  from random import randint
3
  from all_models import models
4
 
5
+ from externalmod import gr_Interface_load
6
+
7
+ import asyncio
8
+ import os
9
+ from threading import RLock
10
+ lock = RLock()
11
+ HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
12
 
13
 
14
  def load_fn(models):
 
18
  for model in models:
19
  if model not in models_load.keys():
20
  try:
21
+ m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
22
  except Exception as error:
23
+ print(error)
24
+ m = gr.Interface(lambda: None, ['text'], ['image'])
25
  models_load.update({model: m})
26
 
27
 
 
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']
38
 
39
 
40
  def update_imgbox(choices):
41
+ choices_plus = extend_choices(choices[:num_models])
42
+ return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
 
43
 
44
  def gen_fn(model_str, prompt):
45
  if model_str == 'NA':
 
51
 
52
  with gr.Blocks() as demo:
53
  with gr.Tab('Toy World'):
54
+ txt_input = gr.Textbox(label='Your prompt:', lines=4)
55
  gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
56
+ #stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
57
+ gen_button.click(lambda s: gr.update(interactive = True), None)
58
  gr.HTML(
59
  """
60
  <div style="text-align: center; max-width: 1200px; margin: 0 auto;">
 
72
  current_models = [gr.Textbox(m, visible = False) for m in default_models]
73
 
74
  for m, o in zip(current_models, output):
75
+ gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn,
76
+ inputs=[m, txt_input], outputs=[o], concurrency_limit=None, queue=False)
77
+ #stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
78
  with gr.Accordion('Model selection'):
79
+ 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)
80
+ #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)
81
  model_choice.change(update_imgbox, model_choice, output)
82
  model_choice.change(extend_choices, model_choice, current_models)
83
  with gr.Row():
84
  gr.HTML(
85
  """
86
  <div class="footer">
87
+ <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>
88
  </p>
89
  """
90
  )