Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from random import randint
|
| 3 |
+
from all_models import models
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def get_current_time():
|
| 8 |
+
now = datetime.now()
|
| 9 |
+
current_time = now2.strftime("%Y-%m-%d %H:%M:%S")
|
| 10 |
+
ki = f'{current_time}'
|
| 11 |
+
return ki
|
| 12 |
+
|
| 13 |
+
def load_fn(models):
|
| 14 |
+
global models_load
|
| 15 |
+
models_load = {}
|
| 16 |
+
for model in models:
|
| 17 |
+
if model not in models_load.keys():
|
| 18 |
+
try:
|
| 19 |
+
m = gr.load(f'models/{model}')
|
| 20 |
+
except Exception as error:
|
| 21 |
+
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
| 22 |
+
models_load.update({model: m})
|
| 23 |
+
|
| 24 |
+
load_fn(models)
|
| 25 |
+
|
| 26 |
+
num_models = len(models)
|
| 27 |
+
default_models = models[:num_models]
|
| 28 |
+
|
| 29 |
+
def extend_choices(choices):
|
| 30 |
+
return choices + (num_models - len(choices)) * ['NA']
|
| 31 |
+
|
| 32 |
+
def update_imgbox(choices):
|
| 33 |
+
choices_plus = extend_choices(choices)
|
| 34 |
+
return [gr.Image(None, label=m, visible=(m != 'NA')) for m in choices_plus]
|
| 35 |
+
|
| 36 |
+
def gen_fn(model_str, prompt):
|
| 37 |
+
if model_str == 'NA':
|
| 38 |
+
return None
|
| 39 |
+
noise = str(randint(0, 9999))
|
| 40 |
+
return models_load[model_str](f'{prompt} {noise}')
|
| 41 |
+
|
| 42 |
+
def make_me():
|
| 43 |
+
with gr.Row():
|
| 44 |
+
txt_input = gr.Textbox(label='Your prompt:', lines=3, width=800, max_height=100)
|
| 45 |
+
|
| 46 |
+
gen_button = gr.Button('Generate images', width=30, height=30)
|
| 47 |
+
stop_button = gr.Button('Stop', variant='secondary', interactive=False, width=30, height=30)
|
| 48 |
+
gen_button.click(lambda s: gr.update(interactive=True), None, stop_button)
|
| 49 |
+
gr.HTML("""
|
| 50 |
+
<div style="text-align: center; max-width: 100%; margin: 0 auto;">
|
| 51 |
+
<body>
|
| 52 |
+
</body>
|
| 53 |
+
</div>
|
| 54 |
+
""")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
output = [gr.Image(label=m, min_width=250, height=250) for m in default_models]
|
| 57 |
+
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
| 58 |
+
for m, o in zip(current_models, output):
|
| 59 |
+
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
| 60 |
+
stop_button.click(lambda s: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
|
| 61 |
+
with gr.Accordion('Model selection'):
|
| 62 |
+
model_choice = gr.CheckboxGroup(models, label=f' {num_models} different models selected', value=default_models, multiselect=True, max_choices=num_models, interactive=True, filterable=False)
|
| 63 |
+
model_choice.change(update_imgbox, model_choice, output)
|
| 64 |
+
model_choice.change(extend_choices, model_choice, current_models)
|
| 65 |
+
with gr.Row():
|
| 66 |
+
gr.HTML()
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
js_code = """
|
| 71 |
+
|
| 72 |
+
console.log('ghgh');
|
| 73 |
+
"""
|
| 74 |
+
css = """
|
| 75 |
+
body {
|
| 76 |
+
background-color: black;
|
| 77 |
+
color: white; /* Optional: Set text color to white for better visibility */
|
| 78 |
+
}
|
| 79 |
+
div.float.svelte-1mwvhlq {
|
| 80 |
+
position: absolute;
|
| 81 |
+
top: var(--block-label-margin);
|
| 82 |
+
left: var(--block-label-margin);
|
| 83 |
+
background: none;
|
| 84 |
+
border: none;
|
| 85 |
+
}
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
with gr.Blocks(css=css) as demo:
|
| 89 |
+
gr.Markdown("<script>" + js_code + "</script>")
|
| 90 |
+
make_me()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
demo.queue(concurrency_count=100)
|
| 95 |
+
demo.launch()
|