Davit12 commited on
Commit
1f2b770
·
verified ·
1 Parent(s): c6694f4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -129
app.py CHANGED
@@ -1,130 +1,85 @@
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 = 32
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, height, width):
26
- choices_plus = extend_choices(choices)
27
- return [gr.Image(None, label=m, visible=(m != 'NA'), height=height, width=width) for m in choices_plus]
28
-
29
- def gen_fn(model_str, prompt, height, width, use_ratio):
30
- if model_str == 'NA':
31
- return None
32
- noise = str(randint(0, 99999999999))
33
-
34
- # Wenn 16:10 Verhältnis gewünscht, Höhe anpassen
35
- if use_ratio:
36
- height = int(width * 10 / 16) # 16:10 Verhältnis
37
-
38
- # Größe in den Prompt einfügen
39
- size_prompt = f"{prompt}, {width}x{height} resolution {noise}"
40
- return models_load[model_str](size_prompt)
41
-
42
- with gr.Blocks() as demo:
43
- with gr.Tab('Multiple models'):
44
- with gr.Accordion('Model selection'):
45
- model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} different models', value=default_models, multiselect=True, max_choices=num_models, interactive=True, filterable=False)
46
-
47
- with gr.Row():
48
- img_width = gr.Slider(100, 1200, value=1024, step=2, label='Image Width (px)')
49
- img_height = gr.Slider(100, 1200, value=640, step=2, label='Image Height (px)')
50
- use_ratio = gr.Checkbox(label='Use 16:10 ratio', value=True)
51
-
52
- # Höhe aktualisieren wenn Ratio aktiviert
53
- img_width.change(
54
- lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
55
- [img_width, use_ratio],
56
- img_height
57
- )
58
- use_ratio.change(
59
- lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
60
- [img_width, use_ratio],
61
- img_height
62
- )
63
-
64
- txt_input = gr.Textbox(label='Prompt text')
65
- gen_button = gr.Button('Generate')
66
- stop_button = gr.Button('Stop', variant='secondary', interactive=False)
67
- gen_button.click(lambda s: gr.update(interactive=True), None, stop_button)
68
-
69
- with gr.Row():
70
- output = [gr.Image(label=m, height=640, width=1024) for m in default_models] # Default 16:10
71
- current_models = [gr.Textbox(m, visible=False) for m in default_models]
72
-
73
- model_choice.change(update_imgbox, [model_choice, img_height, img_width], output)
74
- model_choice.change(extend_choices, model_choice, current_models)
75
- img_height.change(update_imgbox, [model_choice, img_height, img_width], output)
76
- img_width.change(update_imgbox, [model_choice, img_height, img_width], output)
77
-
78
- for m, o in zip(current_models, output):
79
- gen_event = gen_button.click(
80
- gen_fn,
81
- [m, txt_input, img_height, img_width, use_ratio],
82
- o,
83
- queue=False
84
- )
85
-
86
- with gr.Tab('Single model'):
87
- model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0], filterable=False)
88
- txt_input2 = gr.Textbox(label='Prompt text')
89
-
90
- with gr.Row():
91
- img_width2 = gr.Slider(100, 1200, value=1024, step=2, label='Image Width (px)')
92
- img_height2 = gr.Slider(100, 1200, value=640, step=2, label='Image Height (px)')
93
- use_ratio2 = gr.Checkbox(label='Use 16:10 ratio', value=True)
94
-
95
- # Höhe aktualisieren wenn Ratio aktiviert
96
- img_width2.change(
97
- lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
98
- [img_width2, use_ratio2],
99
- img_height2
100
- )
101
- use_ratio2.change(
102
- lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
103
- [img_width2, use_ratio2],
104
- img_height2
105
- )
106
-
107
- max_images = 16
108
- num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
109
-
110
- gen_button2 = gr.Button('Generate')
111
- stop_button2 = gr.Button('Stop', variant='secondary', interactive=False)
112
- gen_button2.click(lambda s: gr.update(interactive=True), None, stop_button2)
113
-
114
- with gr.Row():
115
- output2 = [gr.Image(label='', height=640, width=1024) for _ in range(max_images)]
116
-
117
- for i, o in enumerate(output2):
118
- img_i = gr.Number(i, visible=False)
119
- num_images.change(lambda i, n: gr.update(visible=(i < n)), [img_i, num_images], o)
120
- img_height2.change(lambda h: gr.update(height=h), img_height2, o)
121
- img_width2.change(lambda w: gr.update(width=w), img_width2, o)
122
- gen_event2 = gen_button2.click(
123
- lambda i, n, m, t, h, w, r: gen_fn(m, t, h, w, r) if (i < n) else None,
124
- [img_i, num_images, model_choice2, txt_input2, img_height2, img_width2, use_ratio2],
125
- o
126
- )
127
- stop_button2.click(lambda s: gr.update(interactive=False), None, stop_button2, cancels=[gen_event2])
128
-
129
- demo.queue(concurrency_count=36)
130
  demo.launch()
 
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
+
18
+ load_fn(models)
19
+
20
+
21
+ num_models = 32
22
+ default_models = models[:num_models]
23
+
24
+
25
+ def extend_choices(choices):
26
+ return choices + (num_models - len(choices)) * ['NA']
27
+
28
+
29
+ def update_imgbox(choices):
30
+ choices_plus = extend_choices(choices)
31
+ return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
32
+
33
+
34
+ def gen_fn(model_str, prompt):
35
+ if model_str == 'NA':
36
+ return None
37
+ noise = str(randint(0, 99999999999))
38
+ return models_load[model_str](f'{prompt} {noise}')
39
+
40
+
41
+ with gr.Blocks() as demo:
42
+ with gr.Tab('Multiple models'):
43
+ with gr.Accordion('Model selection'):
44
+ model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models', value = default_models, multiselect = True, max_choices = num_models, interactive = True, filterable = False)
45
+
46
+
47
+ txt_input = gr.Textbox(label = 'Prompt text')
48
+ gen_button = gr.Button('Generate')
49
+ stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
50
+ gen_button.click(lambda s: gr.update(interactive = True), None, stop_button)
51
+
52
+ with gr.Row():
53
+ output = [gr.Image(label = m) for m in default_models]
54
+ current_models = [gr.Textbox(m, visible = False) for m in default_models]
55
+
56
+ model_choice.change(update_imgbox, model_choice, output)
57
+ model_choice.change(extend_choices, model_choice, current_models)
58
+
59
+ for m, o in zip(current_models, output):
60
+ gen_event = gen_button.click(gen_fn, [m, txt_input], o, queue=False)
61
+
62
+
63
+ with gr.Tab('Single model'):
64
+ model_choice2 = gr.Dropdown(models, label = 'Choose model', value = models[0], filterable = False)
65
+ txt_input2 = gr.Textbox(label = 'Prompt text')
66
+
67
+ max_images = 16
68
+ num_images = gr.Slider(1, max_images, value = max_images, step = 1, label = 'Number of images')
69
+
70
+ gen_button2 = gr.Button('Generate')
71
+ stop_button2 = gr.Button('Stop', variant = 'secondary', interactive = False)
72
+ gen_button2.click(lambda s: gr.update(interactive = True), None, stop_button2)
73
+
74
+ with gr.Row():
75
+ output2 = [gr.Image(label = '') for _ in range(max_images)]
76
+
77
+ for i, o in enumerate(output2):
78
+ img_i = gr.Number(i, visible = False)
79
+ num_images.change(lambda i, n: gr.update(visible = (i < n)), [img_i, num_images], o)
80
+ 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)
81
+ stop_button2.click(lambda s: gr.update(interactive = False), None, stop_button2, cancels = [gen_event2])
82
+
83
+
84
+ demo.queue(concurrency_count = 36)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  demo.launch()