Spaces:
Running
Running
Upload 2 files
Browse files- app.py +10 -157
- multit2i.py +180 -0
app.py
CHANGED
@@ -1,164 +1,17 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
def list_uniq(l):
|
14 |
-
return sorted(set(l), key=l.index)
|
15 |
-
|
16 |
-
|
17 |
-
def is_repo_name(s):
|
18 |
-
import re
|
19 |
-
return re.fullmatch(r'^[^/]+?/[^/]+?$', s)
|
20 |
-
|
21 |
-
|
22 |
-
def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="last_modified", limit: int=30):
|
23 |
-
from huggingface_hub import HfApi
|
24 |
-
api = HfApi()
|
25 |
-
default_tags = ["diffusers"]
|
26 |
-
models = []
|
27 |
-
try:
|
28 |
-
model_infos = api.list_models(author=author, task="text-to-image", pipeline_tag="text-to-image",
|
29 |
-
tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit * 5)
|
30 |
-
except Exception as e:
|
31 |
-
print(f"Error: Failed to list models.")
|
32 |
-
print(e)
|
33 |
-
return models
|
34 |
-
for model in model_infos:
|
35 |
-
if not model.private and not model.gated:
|
36 |
-
if not_tag and not_tag in model.tags: continue
|
37 |
-
models.append(model.id)
|
38 |
-
if len(models) == limit: break
|
39 |
-
return models
|
40 |
|
41 |
|
42 |
models = find_model_list("John6666", ["anime"], "", "downloads", 40)
|
43 |
-
|
44 |
-
|
45 |
-
def get_t2i_model_info_dict(repo_id: str):
|
46 |
-
from huggingface_hub import HfApi
|
47 |
-
api = HfApi()
|
48 |
-
info = {"md": "None"}
|
49 |
-
try:
|
50 |
-
if not is_repo_name(repo_id) or not api.repo_exists(repo_id=repo_id): return info
|
51 |
-
model = api.model_info(repo_id=repo_id)
|
52 |
-
except Exception as e:
|
53 |
-
print(f"Error: Failed to get {repo_id}'s info.")
|
54 |
-
print(e)
|
55 |
-
return info
|
56 |
-
if model.private or model.gated: return info
|
57 |
-
try:
|
58 |
-
tags = model.tags
|
59 |
-
except Exception:
|
60 |
-
return info
|
61 |
-
if not 'diffusers' in model.tags: return info
|
62 |
-
if 'diffusers:StableDiffusionXLPipeline' in tags: info["ver"] = "SDXL"
|
63 |
-
elif 'diffusers:StableDiffusionPipeline' in tags: info["ver"] = "SD1.5"
|
64 |
-
elif 'diffusers:StableDiffusion3Pipeline' in tags: info["ver"] = "SD3"
|
65 |
-
else: info["ver"] = "Other"
|
66 |
-
info["url"] = f"https://huggingface.co/{repo_id}/"
|
67 |
-
if model.card_data and model.card_data.tags:
|
68 |
-
info["tags"] = model.card_data.tags
|
69 |
-
info["downloads"] = model.downloads
|
70 |
-
info["likes"] = model.likes
|
71 |
-
info["last_modified"] = model.last_modified.strftime("lastmod: %Y-%m-%d")
|
72 |
-
un_tags = ['text-to-image', 'stable-diffusion', 'stable-diffusion-api', 'safetensors', 'stable-diffusion-xl']
|
73 |
-
descs = [info["ver"]] + list_sub(info["tags"], un_tags) + [f'DLs: {info["downloads"]}'] + [f'❤: {info["likes"]}'] + [info["last_modified"]]
|
74 |
-
info["md"] = f'Model Info: {", ".join(descs)} [Model Repo]({info["url"]})'
|
75 |
-
return info
|
76 |
-
|
77 |
-
|
78 |
-
def save_gallery_images(images, progress=gr.Progress(track_tqdm=True)):
|
79 |
-
from datetime import datetime, timezone, timedelta
|
80 |
-
progress(0, desc="Updating gallery...")
|
81 |
-
dt_now = datetime.now(timezone(timedelta(hours=9)))
|
82 |
-
basename = dt_now.strftime('%Y%m%d_%H%M%S_')
|
83 |
-
i = 1
|
84 |
-
if not images: return images
|
85 |
-
output_images = []
|
86 |
-
output_paths = []
|
87 |
-
for image in images:
|
88 |
-
filename = f'{image[1]}_{basename}{str(i)}.png'
|
89 |
-
i += 1
|
90 |
-
oldpath = Path(image[0])
|
91 |
-
newpath = oldpath
|
92 |
-
try:
|
93 |
-
if oldpath.stem == "image" and oldpath.exists():
|
94 |
-
newpath = oldpath.resolve().rename(Path(filename).resolve())
|
95 |
-
except Exception as e:
|
96 |
-
print(e)
|
97 |
-
pass
|
98 |
-
finally:
|
99 |
-
output_paths.append(str(newpath))
|
100 |
-
output_images.append((str(newpath), str(filename)))
|
101 |
-
progress(1, desc="Gallery updated.")
|
102 |
-
return gr.update(value=output_images), gr.update(value=output_paths)
|
103 |
-
|
104 |
-
|
105 |
-
def load_model(model_name: str):
|
106 |
-
if model_name in loaded_models.keys(): return loaded_models[model_name]
|
107 |
-
try:
|
108 |
-
loaded_models[model_name] = gr.load(f'models/{model_name}')
|
109 |
-
print(f"Loaded: {model_name}")
|
110 |
-
except Exception as e:
|
111 |
-
if model_name in loaded_models.keys(): del loaded_models[model_name]
|
112 |
-
print(f"Failed to load: {model_name}")
|
113 |
-
print(e)
|
114 |
-
return None
|
115 |
-
try:
|
116 |
-
model_info_dict[model_name] = get_t2i_model_info_dict(model_name)
|
117 |
-
except Exception as e:
|
118 |
-
if model_name in model_info_dict.keys(): del model_info_dict[model_name]
|
119 |
-
print(e)
|
120 |
-
return loaded_models[model_name]
|
121 |
-
|
122 |
-
|
123 |
-
for model in models:
|
124 |
-
load_model(model)
|
125 |
-
|
126 |
-
|
127 |
-
def get_model_info_md(model_name: str):
|
128 |
-
if model_name in model_info_dict.keys(): return model_info_dict[model_name].get("md", "")
|
129 |
-
|
130 |
-
|
131 |
-
def change_model(model_name: str):
|
132 |
-
load_model(model_name)
|
133 |
-
return get_model_info_md(model_name)
|
134 |
-
|
135 |
-
|
136 |
-
def infer(prompt: str, model_name: str, recom_prompt: bool, progress=gr.Progress(track_tqdm=True)):
|
137 |
-
from PIL import Image
|
138 |
-
import random
|
139 |
-
seed = ""
|
140 |
-
rand = random.randint(1, 500)
|
141 |
-
for i in range(rand):
|
142 |
-
seed += " "
|
143 |
-
rprompt = ", highly detailed, masterpiece, best quality, very aesthetic, absurdres, " if recom_prompt else ""
|
144 |
-
caption = model_name.split("/")[-1]
|
145 |
-
try:
|
146 |
-
model = load_model(model_name)
|
147 |
-
if not model: return (Image(), None)
|
148 |
-
image_path = model(prompt + rprompt + seed)
|
149 |
-
image = Image.open(image_path).convert('RGB')
|
150 |
-
except Exception as e:
|
151 |
-
print(e)
|
152 |
-
return (Image(), None)
|
153 |
-
return (image, caption)
|
154 |
-
|
155 |
-
|
156 |
-
def infer_multi(prompt: str, model_name: str, recom_prompt: bool, image_num: float, results: list, progress=gr.Progress(track_tqdm=True)):
|
157 |
-
image_num = int(image_num)
|
158 |
-
images = results if results else []
|
159 |
-
for i in range(image_num):
|
160 |
-
images.append(infer(prompt, model_name, recom_prompt))
|
161 |
-
yield images
|
162 |
|
163 |
|
164 |
css = """"""
|
|
|
1 |
import gradio as gr
|
2 |
+
from multit2i import (
|
3 |
+
load_models,
|
4 |
+
find_model_list,
|
5 |
+
infer_multi,
|
6 |
+
save_gallery_images,
|
7 |
+
change_model,
|
8 |
+
get_model_info_md,
|
9 |
+
loaded_models,
|
10 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
models = find_model_list("John6666", ["anime"], "", "downloads", 40)
|
14 |
+
load_models(models, 10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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+
|
9 |
+
|
10 |
+
def list_sub(a, b):
|
11 |
+
return [e for e in a if e not in b]
|
12 |
+
|
13 |
+
|
14 |
+
def list_uniq(l):
|
15 |
+
return sorted(set(l), key=l.index)
|
16 |
+
|
17 |
+
|
18 |
+
def is_repo_name(s):
|
19 |
+
import re
|
20 |
+
return re.fullmatch(r'^[^/]+?/[^/]+?$', s)
|
21 |
+
|
22 |
+
|
23 |
+
def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="last_modified", limit: int=30):
|
24 |
+
from huggingface_hub import HfApi
|
25 |
+
api = HfApi()
|
26 |
+
default_tags = ["diffusers"]
|
27 |
+
if not sort: sort = "last_modified"
|
28 |
+
models = []
|
29 |
+
try:
|
30 |
+
model_infos = api.list_models(author=author, task="text-to-image", pipeline_tag="text-to-image",
|
31 |
+
tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit * 5)
|
32 |
+
except Exception as e:
|
33 |
+
print(f"Error: Failed to list models.")
|
34 |
+
print(e)
|
35 |
+
return models
|
36 |
+
for model in model_infos:
|
37 |
+
if not model.private and not model.gated:
|
38 |
+
if not_tag and not_tag in model.tags: continue
|
39 |
+
models.append(model.id)
|
40 |
+
if len(models) == limit: break
|
41 |
+
return models
|
42 |
+
|
43 |
+
|
44 |
+
def get_t2i_model_info_dict(repo_id: str):
|
45 |
+
from huggingface_hub import HfApi
|
46 |
+
api = HfApi()
|
47 |
+
info = {"md": "None"}
|
48 |
+
try:
|
49 |
+
if not is_repo_name(repo_id) or not api.repo_exists(repo_id=repo_id): return info
|
50 |
+
model = api.model_info(repo_id=repo_id)
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error: Failed to get {repo_id}'s info.")
|
53 |
+
print(e)
|
54 |
+
return info
|
55 |
+
if model.private or model.gated: return info
|
56 |
+
try:
|
57 |
+
tags = model.tags
|
58 |
+
except Exception:
|
59 |
+
return info
|
60 |
+
if not 'diffusers' in model.tags: return info
|
61 |
+
if 'diffusers:StableDiffusionXLPipeline' in tags: info["ver"] = "SDXL"
|
62 |
+
elif 'diffusers:StableDiffusionPipeline' in tags: info["ver"] = "SD1.5"
|
63 |
+
elif 'diffusers:StableDiffusion3Pipeline' in tags: info["ver"] = "SD3"
|
64 |
+
else: info["ver"] = "Other"
|
65 |
+
info["url"] = f"https://huggingface.co/{repo_id}/"
|
66 |
+
if model.card_data and model.card_data.tags:
|
67 |
+
info["tags"] = model.card_data.tags
|
68 |
+
info["downloads"] = model.downloads
|
69 |
+
info["likes"] = model.likes
|
70 |
+
info["last_modified"] = model.last_modified.strftime("lastmod: %Y-%m-%d")
|
71 |
+
un_tags = ['text-to-image', 'stable-diffusion', 'stable-diffusion-api', 'safetensors', 'stable-diffusion-xl']
|
72 |
+
descs = [info["ver"]] + list_sub(info["tags"], un_tags) + [f'DLs: {info["downloads"]}'] + [f'❤: {info["likes"]}'] + [info["last_modified"]]
|
73 |
+
info["md"] = f'Model Info: {", ".join(descs)} [Model Repo]({info["url"]})'
|
74 |
+
return info
|
75 |
+
|
76 |
+
|
77 |
+
def save_gallery_images(images, progress=gr.Progress(track_tqdm=True)):
|
78 |
+
from datetime import datetime, timezone, timedelta
|
79 |
+
progress(0, desc="Updating gallery...")
|
80 |
+
dt_now = datetime.now(timezone(timedelta(hours=9)))
|
81 |
+
basename = dt_now.strftime('%Y%m%d_%H%M%S_')
|
82 |
+
i = 1
|
83 |
+
if not images: return images
|
84 |
+
output_images = []
|
85 |
+
output_paths = []
|
86 |
+
for image in images:
|
87 |
+
filename = f'{image[1]}_{basename}{str(i)}.png'
|
88 |
+
i += 1
|
89 |
+
oldpath = Path(image[0])
|
90 |
+
newpath = oldpath
|
91 |
+
try:
|
92 |
+
if oldpath.stem == "image" and oldpath.exists():
|
93 |
+
newpath = oldpath.resolve().rename(Path(filename).resolve())
|
94 |
+
except Exception as e:
|
95 |
+
print(e)
|
96 |
+
pass
|
97 |
+
finally:
|
98 |
+
output_paths.append(str(newpath))
|
99 |
+
output_images.append((str(newpath), str(filename)))
|
100 |
+
progress(1, desc="Gallery updated.")
|
101 |
+
return gr.update(value=output_images), gr.update(value=output_paths)
|
102 |
+
|
103 |
+
|
104 |
+
def load_model(model_name: str):
|
105 |
+
global loaded_models
|
106 |
+
global model_info_dict
|
107 |
+
if model_name in loaded_models.keys(): return loaded_models[model_name]
|
108 |
+
try:
|
109 |
+
loaded_models[model_name] = gr.load(f'models/{model_name}')
|
110 |
+
print(f"Loaded: {model_name}")
|
111 |
+
except Exception as e:
|
112 |
+
if model_name in loaded_models.keys(): del loaded_models[model_name]
|
113 |
+
print(f"Failed to load: {model_name}")
|
114 |
+
print(e)
|
115 |
+
return None
|
116 |
+
try:
|
117 |
+
model_info_dict[model_name] = get_t2i_model_info_dict(model_name)
|
118 |
+
except Exception as e:
|
119 |
+
if model_name in model_info_dict.keys(): del model_info_dict[model_name]
|
120 |
+
print(e)
|
121 |
+
return loaded_models[model_name]
|
122 |
+
|
123 |
+
|
124 |
+
async def async_load_models(models: list, limit: int=5):
|
125 |
+
sem = asyncio.Semaphore(limit)
|
126 |
+
async def async_load_model(model: str):
|
127 |
+
async with sem:
|
128 |
+
try:
|
129 |
+
return load_model(model)
|
130 |
+
except Exception as e:
|
131 |
+
print(e)
|
132 |
+
tasks = [asyncio.create_task(async_load_model(model)) for model in models]
|
133 |
+
return await asyncio.wait(tasks)
|
134 |
+
|
135 |
+
|
136 |
+
def load_models(models: list, limit: int=5):
|
137 |
+
loop = asyncio.get_event_loop()
|
138 |
+
try:
|
139 |
+
loop.run_until_complete(async_load_models(models, limit))
|
140 |
+
except Exception as e:
|
141 |
+
print(e)
|
142 |
+
pass
|
143 |
+
loop.close()
|
144 |
+
|
145 |
+
|
146 |
+
def get_model_info_md(model_name: str):
|
147 |
+
if model_name in model_info_dict.keys(): return model_info_dict[model_name].get("md", "")
|
148 |
+
|
149 |
+
|
150 |
+
def change_model(model_name: str):
|
151 |
+
load_model(model_name)
|
152 |
+
return get_model_info_md(model_name)
|
153 |
+
|
154 |
+
|
155 |
+
def infer(prompt: str, model_name: str, recom_prompt: bool, progress=gr.Progress(track_tqdm=True)):
|
156 |
+
from PIL import Image
|
157 |
+
import random
|
158 |
+
seed = ""
|
159 |
+
rand = random.randint(1, 500)
|
160 |
+
for i in range(rand):
|
161 |
+
seed += " "
|
162 |
+
rprompt = ", highly detailed, masterpiece, best quality, very aesthetic, absurdres, " if recom_prompt else ""
|
163 |
+
caption = model_name.split("/")[-1]
|
164 |
+
try:
|
165 |
+
model = load_model(model_name)
|
166 |
+
if not model: return (Image.Image(), None)
|
167 |
+
image_path = model(prompt + rprompt + seed)
|
168 |
+
image = Image.open(image_path).convert('RGB')
|
169 |
+
except Exception as e:
|
170 |
+
print(e)
|
171 |
+
return (Image.Image(), None)
|
172 |
+
return (image, caption)
|
173 |
+
|
174 |
+
|
175 |
+
def infer_multi(prompt: str, model_name: str, recom_prompt: bool, image_num: float, results: list, progress=gr.Progress(track_tqdm=True)):
|
176 |
+
image_num = int(image_num)
|
177 |
+
images = results if results else []
|
178 |
+
for i in range(image_num):
|
179 |
+
images.append(infer(prompt, model_name, recom_prompt))
|
180 |
+
yield images
|