Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,206 +1,165 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
from all_models import models
|
| 4 |
from externalmod import gr_Interface_load
|
| 5 |
-
import
|
| 6 |
-
import os
|
| 7 |
-
from threading import RLock
|
| 8 |
-
lock = RLock()
|
| 9 |
-
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
if model not in models_load.keys():
|
| 17 |
-
try:
|
| 18 |
-
m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
|
| 19 |
-
except Exception as error:
|
| 20 |
-
print(error)
|
| 21 |
-
m = gr.Interface(lambda: None, ['text'], ['image'])
|
| 22 |
-
models_load.update({model: m})
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
num_models = 6
|
| 29 |
-
max_images = 6
|
| 30 |
-
inference_timeout = 300
|
| 31 |
-
default_models = models[:num_models]
|
| 32 |
-
MAX_SEED = 2**32-1
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def extend_choices(choices):
|
| 36 |
-
return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def update_imgbox(choices):
|
| 40 |
-
choices_plus = extend_choices(choices[:num_models])
|
| 41 |
-
return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def random_choices():
|
| 45 |
-
import random
|
| 46 |
-
random.seed()
|
| 47 |
-
return random.choices(models, k = num_models)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# https://huggingface.co/docs/api-inference/detailed_parameters
|
| 51 |
-
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
| 52 |
-
async def infer(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1, timeout=inference_timeout):
|
| 53 |
-
from pathlib import Path
|
| 54 |
-
kwargs = {}
|
| 55 |
-
if height is not None and height >= 256: kwargs["height"] = height
|
| 56 |
-
if width is not None and width >= 256: kwargs["width"] = width
|
| 57 |
-
if steps is not None and steps >= 1: kwargs["num_inference_steps"] = steps
|
| 58 |
-
if cfg is not None and cfg > 0: cfg = kwargs["guidance_scale"] = cfg
|
| 59 |
-
noise = ""
|
| 60 |
-
if seed >= 0: kwargs["seed"] = seed
|
| 61 |
-
else:
|
| 62 |
-
rand = randint(1, 500)
|
| 63 |
-
for i in range(rand):
|
| 64 |
-
noise += " "
|
| 65 |
-
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
|
| 66 |
-
prompt=f'{prompt} {noise}', negative_prompt=nprompt, **kwargs, token=HF_TOKEN))
|
| 67 |
-
await asyncio.sleep(0)
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
-
except
|
| 71 |
-
print(
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
return
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
.
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
with gr.
|
| 162 |
-
with gr.Column(scale=
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
from all_models import models
|
| 6 |
from externalmod import gr_Interface_load
|
| 7 |
+
from prompt_extend import extend_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
text_gen1 = extend_prompt
|
| 10 |
+
current_model = models[0]
|
| 11 |
|
| 12 |
+
#text_gen1=gr.Interface.load("spaces/phenomenon1981/MagicPrompt-Stable-Diffusion")
|
| 13 |
+
#text_gen1=gr.Interface.load("spaces/Yntec/prompt-extend")
|
| 14 |
+
#text_gen1=gr.Interface.load("spaces/daspartho/prompt-extend")
|
| 15 |
+
#text_gen1=gr.Interface.load("spaces/Omnibus/MagicPrompt-Stable-Diffusion_link")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
models2 = []
|
| 18 |
+
for model in models:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
+
models2.append(gr_Interface_load(f'models/{model}'))
|
| 21 |
+
except Exception as error:
|
| 22 |
+
print(error)
|
| 23 |
+
models2.append(gr.Interface(lambda: None, ['text'], ['image']))
|
| 24 |
+
|
| 25 |
+
def text_it1(inputs,text_gen1=text_gen1):
|
| 26 |
+
go_t1=text_gen1(inputs)
|
| 27 |
+
return(go_t1)
|
| 28 |
+
|
| 29 |
+
def set_model(current_model):
|
| 30 |
+
current_model = models[current_model]
|
| 31 |
+
return gr.update(label=(f"{current_model}"))
|
| 32 |
+
|
| 33 |
+
def send_it1(inputs, model_choice): #negative_prompt,
|
| 34 |
+
proc1=models2[model_choice]
|
| 35 |
+
output1=proc1(inputs)
|
| 36 |
+
#negative_prompt=negative_prompt
|
| 37 |
+
return(output1)
|
| 38 |
+
|
| 39 |
+
css=""""""
|
| 40 |
+
|
| 41 |
+
with gr.Blocks(css=css) as myface:
|
| 42 |
+
gr.HTML("""
|
| 43 |
+
<div style="text-align: center; max-width: 1200px; margin: 0 auto;">
|
| 44 |
+
<div>
|
| 45 |
+
<style>
|
| 46 |
+
h1 {
|
| 47 |
+
font-size: 6em;
|
| 48 |
+
color: #ffc99f;
|
| 49 |
+
margin-top: 30px;
|
| 50 |
+
margin-bottom: 30px;
|
| 51 |
+
text-shadow: 3px 3px 0 rgba(0, 0, 0, 1) !important;
|
| 52 |
+
}
|
| 53 |
+
h3 {
|
| 54 |
+
color: #ffc99f; !important;
|
| 55 |
+
}
|
| 56 |
+
h4 {
|
| 57 |
+
display: inline-block;
|
| 58 |
+
color: #ffffff !important;
|
| 59 |
+
}
|
| 60 |
+
.wrapper img {
|
| 61 |
+
font-size: 98% !important;
|
| 62 |
+
white-space: nowrap !important;
|
| 63 |
+
text-align: center !important;
|
| 64 |
+
display: inline-block !important;
|
| 65 |
+
color: #ffffff !important;
|
| 66 |
+
}
|
| 67 |
+
.wrapper {
|
| 68 |
+
color: #ffffff !important;
|
| 69 |
+
}
|
| 70 |
+
.gradio-container {
|
| 71 |
+
background-image: linear-gradient(#254150, #1e2f40, #182634) !important;
|
| 72 |
+
color: #ffaa66 !important;
|
| 73 |
+
font-family: 'IBM Plex Sans', sans-serif !important;
|
| 74 |
+
}
|
| 75 |
+
.text-gray-500 {
|
| 76 |
+
color: #ffc99f !important;
|
| 77 |
+
}
|
| 78 |
+
.gr-box {
|
| 79 |
+
background-image: linear-gradient(#182634, #1e2f40, #254150) !important;
|
| 80 |
+
border-top-color: #000000 !important;
|
| 81 |
+
border-right-color: #ffffff !important;
|
| 82 |
+
border-bottom-color: #ffffff !important;
|
| 83 |
+
border-left-color: #000000 !important;
|
| 84 |
+
}
|
| 85 |
+
.gr-input {
|
| 86 |
+
color: #ffc99f; !important;
|
| 87 |
+
background-color: #254150 !important;
|
| 88 |
+
}
|
| 89 |
+
:root {
|
| 90 |
+
--neutral-100: #000000 !important;
|
| 91 |
+
}
|
| 92 |
+
</style>
|
| 93 |
+
<body>
|
| 94 |
+
<div class="center"><h1>Blitz Diffusion</h1>
|
| 95 |
+
</div>
|
| 96 |
+
</body>
|
| 97 |
+
</div>
|
| 98 |
+
<p style="margin-bottom: 1px; color: #ffaa66;">
|
| 99 |
+
<h3>899 Stable Diffusion models, but why? For your enjoyment!</h3></p>
|
| 100 |
+
<br><div class="wrapper">9.3 <img src="https://huggingface.co/Yntec/DucHaitenLofi/resolve/main/NEW.webp" alt="NEW!" style="width:32px;height:16px;">This has become a legacy backup copy of old <u><a href="https://huggingface.co/spaces/Yntec/ToyWorld">ToyWorld</a></u>'s UI! Newer models added dailty over there! 25 new models since last update!</div>
|
| 101 |
+
<p style="margin-bottom: 1px; font-size: 98%">
|
| 102 |
+
<br><h4>If a model is already loaded each new image takes less than <b>10</b> seconds to generate!</h4></p>
|
| 103 |
+
<p style="margin-bottom: 1px; color: #ffffff;">
|
| 104 |
+
<br><div class="wrapper">Generate 6 images from 1 prompt at the <u><a href="https://huggingface.co/spaces/Yntec/PrintingPress">PrintingPress</a></u>, and use 6 different models at <u><a href="https://huggingface.co/spaces/Yntec/diffusion80xx">Huggingface Diffusion!</a></u>!
|
| 105 |
+
</p></p>
|
| 106 |
+
</div>
|
| 107 |
+
""")
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Column(scale=100):
|
| 110 |
+
#Model selection dropdown
|
| 111 |
+
model_name1 = gr.Dropdown(label="Select Model", choices=[m for m in models], type="index", value=current_model, interactive=True)
|
| 112 |
+
with gr.Row():
|
| 113 |
+
with gr.Column(scale=100):
|
| 114 |
+
magic1 = gr.Textbox(label="Your Prompt", lines=4) #Positive
|
| 115 |
+
#with gr.Column(scale=100):
|
| 116 |
+
#negative_prompt=gr.Textbox(label="Negative Prompt", lines=1)
|
| 117 |
+
gr.HTML("""<style> .gr-button {
|
| 118 |
+
color: #ffffff !important;
|
| 119 |
+
text-shadow: 1px 1px 0 rgba(0, 0, 0, 1) !important;
|
| 120 |
+
background-image: linear-gradient(#76635a, #d2a489) !important;
|
| 121 |
+
border-radius: 24px !important;
|
| 122 |
+
border: solid 1px !important;
|
| 123 |
+
border-top-color: #ffc99f !important;
|
| 124 |
+
border-right-color: #000000 !important;
|
| 125 |
+
border-bottom-color: #000000 !important;
|
| 126 |
+
border-left-color: #ffc99f !important;
|
| 127 |
+
padding: 6px 30px;
|
| 128 |
+
}
|
| 129 |
+
.gr-button:active {
|
| 130 |
+
color: #ffc99f !important;
|
| 131 |
+
font-size: 98% !important;
|
| 132 |
+
text-shadow: 0px 0px 0 rgba(0, 0, 0, 1) !important;
|
| 133 |
+
background-image: linear-gradient(#d2a489, #76635a) !important;
|
| 134 |
+
border-top-color: #000000 !important;
|
| 135 |
+
border-right-color: #ffffff !important;
|
| 136 |
+
border-bottom-color: #ffffff !important;
|
| 137 |
+
border-left-color: #000000 !important;
|
| 138 |
+
}
|
| 139 |
+
.gr-button:hover {
|
| 140 |
+
filter: brightness(130%);
|
| 141 |
+
}
|
| 142 |
+
</style>""")
|
| 143 |
+
run = gr.Button("Generate Image")
|
| 144 |
+
with gr.Row():
|
| 145 |
+
with gr.Column():
|
| 146 |
+
output1 = gr.Image(label=(f"{current_model}"))
|
| 147 |
+
|
| 148 |
+
with gr.Row():
|
| 149 |
+
with gr.Column(scale=50):
|
| 150 |
+
input_text=gr.Textbox(label="Use this box to extend an idea automagically, by typing some words and clicking Extend Idea", lines=2)
|
| 151 |
+
see_prompts=gr.Button("Extend Idea -> overwrite the contents of the `Your Prompt´ box above")
|
| 152 |
+
use_short=gr.Button("Copy the contents of this box to the `Your Prompt´ box above")
|
| 153 |
+
def short_prompt(inputs):
|
| 154 |
+
return(inputs)
|
| 155 |
+
|
| 156 |
+
model_name1.change(set_model, inputs=model_name1, outputs=[output1])
|
| 157 |
+
|
| 158 |
+
run.click(send_it1, inputs=[magic1, model_name1], outputs=[output1])
|
| 159 |
+
|
| 160 |
+
use_short.click(short_prompt,inputs=[input_text], outputs=magic1)
|
| 161 |
+
|
| 162 |
+
see_prompts.click(text_it1,inputs=[input_text], outputs=magic1)
|
| 163 |
+
|
| 164 |
+
myface.queue()
|
| 165 |
+
myface.launch(show_api=False)
|