Spaces:
Running
Running
LeeveWasTaken
commited on
Commit
•
420fff6
1
Parent(s):
bdf3876
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,82 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
from all_models import models
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
models_load = {}
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
15 |
-
models_load.update({model: m})
|
16 |
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
-
default_models = models[:num_models]
|
21 |
|
22 |
def extend_choices(choices):
|
23 |
-
return choices + (
|
24 |
|
25 |
def update_imgbox(choices):
|
26 |
-
|
27 |
-
return [gr.Image(None, label=m, visible=(m != 'NA')) for m in
|
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
|
38 |
-
|
39 |
-
|
40 |
-
with gr.
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
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 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
with gr.Row():
|
78 |
gr.HTML(
|
79 |
-
|
80 |
<div class="footer">
|
81 |
-
|
82 |
-
</
|
83 |
-
|
84 |
)
|
|
|
|
|
85 |
|
86 |
-
|
|
|
|
|
|
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)
|