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

Update app.py

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