Upload 2 files
Browse files- app.py +7 -5
- multit2i.py +27 -14
app.py
CHANGED
@@ -48,7 +48,9 @@ load_models(models, 10)
|
|
48 |
#load_models(models, 20) # Fetching 20 models at the same time. default: 5
|
49 |
|
50 |
|
51 |
-
css = """
|
|
|
|
|
52 |
|
53 |
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", css=css) as demo:
|
54 |
with gr.Column():
|
@@ -61,13 +63,13 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", css=css) as demo:
|
|
61 |
negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"], visible=False)
|
62 |
with gr.Group():
|
63 |
model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0])
|
64 |
-
model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]))
|
65 |
prompt = gr.Text(label="Prompt", lines=1, max_lines=8, placeholder="1girl, solo, ...")
|
66 |
neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="", visible=False)
|
67 |
with gr.Row():
|
68 |
-
run_button = gr.Button("Generate Image", scale=
|
69 |
-
random_button = gr.Button("Random Model π²", scale=
|
70 |
-
image_num = gr.Number(label="
|
71 |
results = gr.Gallery(label="Gallery", interactive=False, show_download_button=True, show_share_button=False,
|
72 |
container=True, format="png", object_fit="contain")
|
73 |
image_files = gr.Files(label="Download", interactive=False)
|
|
|
48 |
#load_models(models, 20) # Fetching 20 models at the same time. default: 5
|
49 |
|
50 |
|
51 |
+
css = """
|
52 |
+
#model_info { text-align: center; display:block; }
|
53 |
+
"""
|
54 |
|
55 |
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", css=css) as demo:
|
56 |
with gr.Column():
|
|
|
63 |
negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"], visible=False)
|
64 |
with gr.Group():
|
65 |
model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0])
|
66 |
+
model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]), elem_id="model_info")
|
67 |
prompt = gr.Text(label="Prompt", lines=1, max_lines=8, placeholder="1girl, solo, ...")
|
68 |
neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="", visible=False)
|
69 |
with gr.Row():
|
70 |
+
run_button = gr.Button("Generate Image", scale=6)
|
71 |
+
random_button = gr.Button("Random Model π²", scale=3)
|
72 |
+
image_num = gr.Number(label="Count", minimum=1, maximum=16, value=1, step=1, interactive=True, scale=1)
|
73 |
results = gr.Gallery(label="Gallery", interactive=False, show_download_button=True, show_share_button=False,
|
74 |
container=True, format="png", object_fit="contain")
|
75 |
image_files = gr.Files(label="Download", interactive=False)
|
multit2i.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import asyncio
|
|
|
3 |
from pathlib import Path
|
4 |
|
5 |
|
|
|
6 |
loaded_models = {}
|
7 |
model_info_dict = {}
|
8 |
|
@@ -59,7 +61,8 @@ def get_t2i_model_info_dict(repo_id: str):
|
|
59 |
if model.private or model.gated: return info
|
60 |
try:
|
61 |
tags = model.tags
|
62 |
-
except Exception:
|
|
|
63 |
return info
|
64 |
if not 'diffusers' in model.tags: return info
|
65 |
if 'diffusers:StableDiffusionXLPipeline' in tags: info["ver"] = "SDXL"
|
@@ -74,7 +77,7 @@ def get_t2i_model_info_dict(repo_id: str):
|
|
74 |
info["last_modified"] = model.last_modified.strftime("lastmod: %Y-%m-%d")
|
75 |
un_tags = ['text-to-image', 'stable-diffusion', 'stable-diffusion-api', 'safetensors', 'stable-diffusion-xl']
|
76 |
descs = [info["ver"]] + list_sub(info["tags"], un_tags) + [f'DLs: {info["downloads"]}'] + [f'β€: {info["likes"]}'] + [info["last_modified"]]
|
77 |
-
info["md"] = f'
|
78 |
return info
|
79 |
|
80 |
|
@@ -110,32 +113,35 @@ def load_model(model_name: str):
|
|
110 |
global model_info_dict
|
111 |
if model_name in loaded_models.keys(): return loaded_models[model_name]
|
112 |
try:
|
113 |
-
|
|
|
114 |
print(f"Loaded: {model_name}")
|
115 |
except Exception as e:
|
116 |
-
|
|
|
117 |
print(f"Failed to load: {model_name}")
|
118 |
print(e)
|
119 |
return None
|
120 |
try:
|
121 |
-
|
|
|
122 |
except Exception as e:
|
123 |
-
|
|
|
124 |
print(e)
|
125 |
return loaded_models[model_name]
|
126 |
|
127 |
|
128 |
-
async def async_load_models(models: list, limit: int=5):
|
129 |
-
from tqdm.asyncio import tqdm_asyncio
|
130 |
sem = asyncio.Semaphore(limit)
|
131 |
async def async_load_model(model: str):
|
132 |
async with sem:
|
133 |
try:
|
134 |
-
return load_model
|
135 |
except Exception as e:
|
136 |
print(e)
|
137 |
tasks = [asyncio.create_task(async_load_model(model)) for model in models]
|
138 |
-
return await
|
139 |
|
140 |
|
141 |
def load_models(models: list, limit: int=5):
|
@@ -271,20 +277,23 @@ def infer(prompt: str, neg_prompt: str, model_name: str):
|
|
271 |
|
272 |
async def infer_multi(prompt: str, neg_prompt: str, results: list, image_num: float, model_name: str,
|
273 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], progress=gr.Progress(track_tqdm=True)):
|
274 |
-
import
|
275 |
image_num = int(image_num)
|
276 |
images = results if results else []
|
277 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
278 |
tasks = [asyncio.to_thread(infer, prompt, neg_prompt, model_name) for i in range(image_num)]
|
279 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
|
|
|
280 |
for result in results:
|
281 |
-
|
|
|
282 |
yield images
|
283 |
|
284 |
|
285 |
async def infer_multi_random(prompt: str, neg_prompt: str, results: list, image_num: float,
|
286 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], progress=gr.Progress(track_tqdm=True)):
|
287 |
-
import
|
288 |
import random
|
289 |
image_num = int(image_num)
|
290 |
images = results if results else []
|
@@ -293,6 +302,10 @@ async def infer_multi_random(prompt: str, neg_prompt: str, results: list, image_
|
|
293 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
294 |
tasks = [asyncio.to_thread(infer, prompt, neg_prompt, model_name) for model_name in model_names]
|
295 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
|
|
|
296 |
for result in results:
|
297 |
-
|
|
|
298 |
yield images
|
|
|
|
1 |
import gradio as gr
|
2 |
import asyncio
|
3 |
+
from threading import RLock, Thread
|
4 |
from pathlib import Path
|
5 |
|
6 |
|
7 |
+
lock = RLock()
|
8 |
loaded_models = {}
|
9 |
model_info_dict = {}
|
10 |
|
|
|
61 |
if model.private or model.gated: return info
|
62 |
try:
|
63 |
tags = model.tags
|
64 |
+
except Exception as e:
|
65 |
+
print(e)
|
66 |
return info
|
67 |
if not 'diffusers' in model.tags: return info
|
68 |
if 'diffusers:StableDiffusionXLPipeline' in tags: info["ver"] = "SDXL"
|
|
|
77 |
info["last_modified"] = model.last_modified.strftime("lastmod: %Y-%m-%d")
|
78 |
un_tags = ['text-to-image', 'stable-diffusion', 'stable-diffusion-api', 'safetensors', 'stable-diffusion-xl']
|
79 |
descs = [info["ver"]] + list_sub(info["tags"], un_tags) + [f'DLs: {info["downloads"]}'] + [f'β€: {info["likes"]}'] + [info["last_modified"]]
|
80 |
+
info["md"] = f'Model Info: {", ".join(descs)} [Model Repo]({info["url"]})'
|
81 |
return info
|
82 |
|
83 |
|
|
|
113 |
global model_info_dict
|
114 |
if model_name in loaded_models.keys(): return loaded_models[model_name]
|
115 |
try:
|
116 |
+
with lock:
|
117 |
+
loaded_models[model_name] = gr.load(f'models/{model_name}')
|
118 |
print(f"Loaded: {model_name}")
|
119 |
except Exception as e:
|
120 |
+
with lock:
|
121 |
+
if model_name in loaded_models.keys(): del loaded_models[model_name]
|
122 |
print(f"Failed to load: {model_name}")
|
123 |
print(e)
|
124 |
return None
|
125 |
try:
|
126 |
+
with lock:
|
127 |
+
model_info_dict[model_name] = get_t2i_model_info_dict(model_name)
|
128 |
except Exception as e:
|
129 |
+
with lock:
|
130 |
+
if model_name in model_info_dict.keys(): del model_info_dict[model_name]
|
131 |
print(e)
|
132 |
return loaded_models[model_name]
|
133 |
|
134 |
|
135 |
+
async def async_load_models(models: list, limit: int=5, wait=10):
|
|
|
136 |
sem = asyncio.Semaphore(limit)
|
137 |
async def async_load_model(model: str):
|
138 |
async with sem:
|
139 |
try:
|
140 |
+
return await asyncio.to_thread(load_model, model)
|
141 |
except Exception as e:
|
142 |
print(e)
|
143 |
tasks = [asyncio.create_task(async_load_model(model)) for model in models]
|
144 |
+
return await asyncio.gather(*tasks, return_exceptions=True)
|
145 |
|
146 |
|
147 |
def load_models(models: list, limit: int=5):
|
|
|
277 |
|
278 |
async def infer_multi(prompt: str, neg_prompt: str, results: list, image_num: float, model_name: str,
|
279 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], progress=gr.Progress(track_tqdm=True)):
|
280 |
+
#from tqdm.asyncio import tqdm_asyncio
|
281 |
image_num = int(image_num)
|
282 |
images = results if results else []
|
283 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
284 |
tasks = [asyncio.to_thread(infer, prompt, neg_prompt, model_name) for i in range(image_num)]
|
285 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
286 |
+
#results = await tqdm_asyncio.gather(*tasks)
|
287 |
+
if not results: results = []
|
288 |
for result in results:
|
289 |
+
with lock:
|
290 |
+
if result and result[1]: images.append(result)
|
291 |
yield images
|
292 |
|
293 |
|
294 |
async def infer_multi_random(prompt: str, neg_prompt: str, results: list, image_num: float,
|
295 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], progress=gr.Progress(track_tqdm=True)):
|
296 |
+
#from tqdm.asyncio import tqdm_asyncio
|
297 |
import random
|
298 |
image_num = int(image_num)
|
299 |
images = results if results else []
|
|
|
302 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
303 |
tasks = [asyncio.to_thread(infer, prompt, neg_prompt, model_name) for model_name in model_names]
|
304 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
305 |
+
#await tqdm_asyncio.gather(*tasks)
|
306 |
+
if not results: results = []
|
307 |
for result in results:
|
308 |
+
with lock:
|
309 |
+
if result and result[1]: images.append(result)
|
310 |
yield images
|
311 |
+
|