LeeveWasTaken commited on
Commit
3c0b2e4
1 Parent(s): 420fff6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -69
app.py CHANGED
@@ -1,82 +1,70 @@
1
  import gradio as gr
2
- from functools import partial
3
  from all_models import models
4
-
5
- NUM_MODELS = 6
6
- DEFAULT_MODELS = models[:NUM_MODELS]
7
-
8
- def load_model(model_name):
9
- try:
10
- return gr.load(f'models/{model_name}')
11
- except Exception:
12
- return gr.Interface(lambda txt: None, ['text'], ['image'])
13
-
14
- def load_models(model_list):
15
- return {model: load_model(model) for model in model_list}
16
-
17
- MODELS_LOADED = load_models(models)
18
-
19
  def extend_choices(choices):
20
- return choices + ['NA'] * (NUM_MODELS - len(choices))
21
-
22
  def update_imgbox(choices):
23
- choices_extended = extend_choices(choices)
24
- return [gr.Image(None, label=m, visible=(m != 'NA')) for m in choices_extended]
25
-
26
  def gen_fn(model_str, prompt, negative_prompt=None, image_style="Default"):
27
  if model_str == 'NA':
28
  return None
29
- modified_prompt = f"{prompt}, {image_style}" if image_style != "Default" else prompt
 
 
30
  if negative_prompt:
31
  modified_prompt += f", not {negative_prompt}"
32
- return MODELS_LOADED[model_str](modified_prompt)
33
-
34
- def create_interface():
35
- with gr.Blocks() as demo:
36
- with gr.Tab('The Dream'):
37
- txt_input = gr.Textbox(label='Your prompt:', lines=4)
38
- with gr.Accordion("Advanced Settings", open=False):
39
- neg_prompt = gr.Textbox(label='Negative prompt (Optional):', placeholder='Enter undesirable attributes here', lines=2)
40
- image_style = gr.Dropdown(label='Select Style', choices=["Default", "Realistic", "Portrait", "Anime"], value="Default")
41
-
42
- gen_button = gr.Button('Generate up to 6 images in up to 2 minutes total')
43
- stop_button = gr.Button('Stop', variant='secondary', interactive=False)
44
-
45
- gen_button.click(lambda: gr.update(interactive=True), None, stop_button, concurrency_limit=10)
46
-
47
- gr.HTML(
48
- """
49
- <div style="text-align: center; max-width: 1200px; margin: 0 auto;">
50
- <p style="margin-bottom: 10px; color: var(--body-text-color);">Scroll down to see more images and select models.</p>
51
  </div>
52
- """
53
- )
54
-
55
- with gr.Row():
56
- output = [gr.Image(label=m, min_width=460) for m in DEFAULT_MODELS]
57
- current_models = [gr.Textbox(m, visible=False) for m in DEFAULT_MODELS]
58
-
59
- for m, o in zip(current_models, output):
60
- gen_event = gen_button.click(partial(gen_fn, prompt=txt_input, negative_prompt=neg_prompt, image_style=image_style),
61
- inputs=[m], outputs=[o], concurrency_limit=10)
62
- stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
63
-
64
- with gr.Accordion('Model selection'):
65
- model_choice = gr.CheckboxGroup(models, label=f'Choose up to {NUM_MODELS} different models from the best available!',
66
- value=DEFAULT_MODELS, interactive=True)
67
- model_choice.change(update_imgbox, model_choice, output)
68
- model_choice.change(extend_choices, model_choice, current_models)
69
-
70
  gr.HTML(
71
- """
72
  <div class="footer">
73
- <p>Have fun!</p>
74
- </div>
75
- """
76
  )
77
-
78
- return demo
79
-
80
- if __name__ == "__main__":
81
- demo = create_interface()
82
- demo.launch(max_threads=200)
 
1
  import gradio as gr
2
+ from random import randint
3
  from all_models import models
4
+ def load_fn(models):
5
+ global models_load
6
+ models_load = {}
7
+ for model in models:
8
+ if model not in models_load.keys():
9
+ try:
10
+ m = gr.load(f'models/{model}')
11
+ except Exception as error:
12
+ m = gr.Interface(lambda txt: None, ['text'], ['image'])
13
+ models_load.update({model: m})
14
+ load_fn(models)
15
+ num_models = 6
16
+ default_models = models[:num_models]
 
 
17
  def extend_choices(choices):
18
+ return choices + (num_models - len(choices)) * ['NA']
 
19
  def update_imgbox(choices):
20
+ choices_plus = extend_choices(choices)
21
+ return [gr.Image(None, label=m, visible=(m != 'NA')) for m in choices_plus]
 
22
  def gen_fn(model_str, prompt, negative_prompt=None, image_style="Default"):
23
  if model_str == 'NA':
24
  return None
25
+ modified_prompt = prompt
26
+ if image_style != "Default":
27
+ modified_prompt += f", {image_style}"
28
  if negative_prompt:
29
  modified_prompt += f", not {negative_prompt}"
30
+ return models_load[model_str](modified_prompt)
31
+ with gr.Blocks() as demo:
32
+ with gr.Tab('The Dream'):
33
+ txt_input = gr.Textbox(label='Your prompt:', lines=4)
34
+ with gr.Accordion("Advanced Settings", open=False) as advanced_settings:
35
+ neg_prompt = gr.Textbox(label='Negative prompt (Optional):', placeholder='Enter undesirable attributes here', lines=2)
36
+ image_style = gr.Dropdown(label='Select Style', choices=["Default", "Realistic", "Portrait", "Anime"], value="Default")
37
+ gen_button = gr.Button('Generate up to 6 images in up to 2 minutes total')
38
+ stop_button = gr.Button('Stop', variant='secondary', interactive=False)
39
+ gen_button.click(lambda s: gr.update(interactive=True), None, stop_button, concurrency_limit=10)
40
+ gr.HTML(
41
+ """
42
+ <div style="text-align: center; max-width: 1200px; margin: 0 auto;">
43
+ <div>
44
+ <body>
45
+ <div class="center"><p style="margin-bottom: 10px; color: var(--body-text-color);">Scroll down to see more images and select models.</p>
 
 
 
46
  </div>
47
+ </body>
48
+ </div>
49
+ </div>
50
+ """
51
+ )
52
+ with gr.Row():
53
+ output = [gr.Image(label=m, min_width=460) for m in default_models]
54
+ current_models = [gr.Textbox(m, visible=False) for m in default_models]
55
+ for m, o in zip(current_models, output):
56
+ gen_event = gen_button.click(gen_fn, [m, txt_input, neg_prompt, image_style], o, concurrency_limit=10)
57
+ stop_button.click(lambda s: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
58
+ with gr.Accordion('Model selection'):
59
+ model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} different models from the best available!', value=default_models, interactive=True)
60
+ model_choice.change(update_imgbox, model_choice, output)
61
+ model_choice.change(extend_choices, model_choice, current_models)
62
+ with gr.Row():
 
 
63
  gr.HTML(
64
+ """
65
  <div class="footer">
66
+ <p>Have fun!
67
+ </p>
68
+ """
69
  )
70
+ demo.launch(max_threads=200)