Spaces:
Running
Running
Upload 4 files
Browse files- README.md +13 -12
- app.py +52 -0
- multit2i.py +180 -0
- requirements.txt +1 -0
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
-
---
|
2 |
-
title: Yntec Liked Models
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.39.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
1 |
+
---
|
2 |
+
title: Yntec Liked Text-to-Image Models Playground
|
3 |
+
emoji: 🖼️
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: green
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.39.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
short_description: Text-to-Image
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("Yntec", [], "", "likes", 30)
|
14 |
+
load_models(models, 10)
|
15 |
+
|
16 |
+
|
17 |
+
css = """"""
|
18 |
+
|
19 |
+
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", css=css) as demo:
|
20 |
+
with gr.Column():
|
21 |
+
model_name = gr.Dropdown(label="Select Model", choices=list(loaded_models.keys()), value=list(loaded_models.keys())[0], allow_custom_value=True)
|
22 |
+
model_info = gr.Markdown(value=get_model_info_md(list(loaded_models.keys())[0]))
|
23 |
+
image_num = gr.Slider(label="Number of Images", minimum=1, maximum=8, value=1, step=1)
|
24 |
+
recom_prompt = gr.Checkbox(label="Recommended Prompt", value=True)
|
25 |
+
prompt = gr.Text(label="Prompt", lines=1, max_lines=8, placeholder="1girl, solo, ...")
|
26 |
+
run_button = gr.Button("Generate Image")
|
27 |
+
results = gr.Gallery(label="Gallery", interactive=False, show_download_button=True, show_share_button=False,
|
28 |
+
container=True, format="png", object_fit="contain")
|
29 |
+
image_files = gr.Files(label="Download", interactive=False)
|
30 |
+
clear_results = gr.Button("Clear Gallery and Download")
|
31 |
+
gr.Markdown(
|
32 |
+
f"""This demo was created in reference to the following demos.
|
33 |
+
- [Nymbo/Flood](https://huggingface.co/spaces/Nymbo/Flood).
|
34 |
+
- [Yntec/ToyWorldXL](https://huggingface.co/spaces/Yntec/ToyWorldXL).
|
35 |
+
"""
|
36 |
+
)
|
37 |
+
gr.DuplicateButton(value="Duplicate Space")
|
38 |
+
|
39 |
+
model_name.change(change_model, [model_name], [model_info], queue=False, show_api=False)
|
40 |
+
gr.on(
|
41 |
+
triggers=[run_button.click, prompt.submit],
|
42 |
+
fn=infer_multi,
|
43 |
+
inputs=[prompt, model_name, recom_prompt, image_num, results],
|
44 |
+
outputs=[results],
|
45 |
+
queue=True,
|
46 |
+
show_progress="full",
|
47 |
+
show_api=True,
|
48 |
+
).success(save_gallery_images, [results], [results, image_files], queue=False, show_api=False)
|
49 |
+
clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
|
50 |
+
|
51 |
+
demo.queue()
|
52 |
+
demo.launch()
|
multit2i.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import asyncio
|
3 |
+
from pathlib import Path
|
4 |
+
|
5 |
+
|
6 |
+
loaded_models = {}
|
7 |
+
model_info_dict = {}
|
8 |
+
|
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
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
huggingface_hub
|