Spaces:
Sleeping
Sleeping
Upload app_test.py
Browse files- app_test.py +91 -0
app_test.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from random import randint
|
3 |
+
from all_models import models
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def load_fn(models):
|
8 |
+
global models_load
|
9 |
+
models_load = {}
|
10 |
+
|
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 |
+
|
20 |
+
load_fn(models)
|
21 |
+
|
22 |
+
|
23 |
+
num_models = 4
|
24 |
+
default_models = models[:num_models]
|
25 |
+
#current_models = default_models
|
26 |
+
|
27 |
+
|
28 |
+
'''
|
29 |
+
def make_update(choices):
|
30 |
+
global current_models
|
31 |
+
current_models = [c for c in choices]
|
32 |
+
current_models += (num_models - len(choices)) * ['NA']
|
33 |
+
return [gr.Image(None, label = m, visible = (m != 'NA')) for m in current_models]
|
34 |
+
'''
|
35 |
+
|
36 |
+
def extend_choices(choices):
|
37 |
+
return choices + (num_models - len(choices)) * ['NA']
|
38 |
+
|
39 |
+
|
40 |
+
def update_imgbox(choices):
|
41 |
+
choices_plus = extend_choices(choices)
|
42 |
+
return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
|
43 |
+
|
44 |
+
|
45 |
+
def gen_fn(model_str, prompt):
|
46 |
+
if model_str == 'NA':
|
47 |
+
return None
|
48 |
+
noise = str(randint(0, 99999999999))
|
49 |
+
return models_load[model_str](f'{prompt} {noise}')
|
50 |
+
|
51 |
+
|
52 |
+
def gen_fn2(choices, prompt):
|
53 |
+
choices_plus = extend_choices(choices)
|
54 |
+
return [gen_fn(m, prompt) for m in choices_plus]
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
with gr.Blocks() as demo:
|
59 |
+
with gr.Tab('Multiple models'):
|
60 |
+
with gr.Accordion('Model selection'):
|
61 |
+
model_choice = gr.Dropdown(models, label = f'Choose up to {num_models} different models', value = default_models, multiselect = True, max_choices = num_models, interactive = True)
|
62 |
+
|
63 |
+
txt_input = gr.Textbox(label = 'Prompt text')
|
64 |
+
gen_button = gr.Button('Generate')
|
65 |
+
|
66 |
+
with gr.Row():
|
67 |
+
output = [gr.Image(label = m) for m in default_models]
|
68 |
+
model_choice.change(update_imgbox, model_choice, output)
|
69 |
+
|
70 |
+
gen_button.click(gen_fn2, [model_choice, txt_input], output)
|
71 |
+
|
72 |
+
'''
|
73 |
+
output = gr.Gallery()
|
74 |
+
gen_button.click(lambda models, txt: [gen_fn(m, txt) for m in models], [model_choice, txt_input], output)
|
75 |
+
'''
|
76 |
+
|
77 |
+
with gr.Tab('Single model'):
|
78 |
+
model_choice2 = gr.Dropdown(models, label = 'Choose model', value = models[0], filterable = False)
|
79 |
+
txt_input2 = gr.Textbox(label = 'Prompt text')
|
80 |
+
gen_button2 = gr.Button('Generate')
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
num_images = 6
|
84 |
+
output2 = [gr.Image(label = '') for _ in range(num_images)]
|
85 |
+
|
86 |
+
for o in output2:
|
87 |
+
gen_button2.click(gen_fn, [model_choice2, txt_input2], o)
|
88 |
+
|
89 |
+
|
90 |
+
demo.queue(concurrency_count = 36)
|
91 |
+
demo.launch()
|