art-multi / app.py
Brian Watson
Update app.py
cddbc8a
raw
history blame
3.43 kB
import gradio as gr
models = [
{"name": "Claudfuen 1", "url": "claudfuen/photorealistic-fuen-v1"},
{"name": "Deliberate", "url": "Masagin/Deliberate"},
{"name": "Seek Art Mega", "url": "coreco/seek.art_MEGA"},
{"name": "Realistic Vision 1.4", "url": "SG161222/Realistic_Vision_V1.4"},
{"name": "Dreamshaper", "url": "Lykon/DreamShaper"},
]
current_model = models[0]
text_gen = gr.Interface.load("spaces/Omnibus/MagicPrompt-Stable-Diffusion_link")
models2 = []
for model in models:
model_url = f"models/{model['url']}"
loaded_model = gr.Interface.load(model_url, live=True, preprocess=True)
models2.append(loaded_model)
def text_it(inputs, text_gen=text_gen):
return text_gen(inputs)
def set_model(current_model_name):
global current_model
current_model = next((m for m in models if m["name"] == current_model_name), None)
return gr.update(label=f"{current_model['name']}")
def send_it(inputs, model_choice):
proc = models2[model_choice]
return proc(inputs)
def make_dropdown_searchable(dropdown_id):
script = f"""
<script>
function makeDropdownSearchable(dropdownId) {{
const input = document.getElementById(dropdownId);
input.addEventListener("input", function() {{
const value = this.value.toLowerCase();
const options = document.querySelectorAll(`#${dropdownId} option`);
let found = false;
for (let i = 0; i < options.length; i++) {{
const option = options[i];
const text = option.text.toLowerCase();
const match = text.includes(value);
option.style.display = match ? "block" : "none";
if (match) {{
found = true;
}}
}}
if (value && !found) {{
const firstOption = options[0];
if (firstOption) {{
firstOption.selected = true;
}}
}}
}});
}}
makeDropdownSearchable("{dropdown_id}");
</script>
"""
return gr.HTML(script)
css = """"""
with gr.Interface(blocks=True, css=css) as iface:
make_dropdown_searchable(iface.inputs[1].component_id) # Make the second component searchable (model dropdown)
input_text = iface.inputs[0]
model_name1 = iface.inputs[1]
see_prompts = iface.buttons[0]
run = iface.buttons[1]
outputs = iface.outputs
iface.title = "Image Generation"
def generate_prompt(btn):
prompt_text = input_text.value
outputs[:4].clear()
outputs[4:8].clear()
for i in range(4):
magic_input = outputs[i].owner.inputs[0]
magic_input.value = text_it(prompt_text)
def generate_images(btn):
for i in range(4):
magic_input = outputs[i].owner.inputs[0]
magic_input_value = magic_input.value
model_choice = model_name1.value
proc = models2[model_choice]
outputs[i].update(send_it(magic_input_value, model_choice))
see_prompts.set_action(generate_prompt)
run.set_action(generate_images)
model_name1.choices = [m["name"] for m in models]
model_name1.default = current_model["name"]
model_name1.changed(set_model)
iface.launch(inline=True, show=True)