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
7 |
+
from tqdm import tqdm
|
8 |
+
from time import sleep
|
9 |
+
from PIL import Image
|
10 |
+
import requests
|
11 |
+
from io import BytesIO
|
12 |
+
from datasets import Dataset, load_dataset
|
13 |
+
import json
|
14 |
+
import cv2
|
15 |
+
import pathlib
|
16 |
+
|
17 |
+
import imagehash
|
18 |
+
|
19 |
+
MAX_MODEL_NUM = 300
|
20 |
+
|
21 |
+
'''
|
22 |
+
Yntec digplay
|
23 |
+
'''
|
24 |
+
|
25 |
+
hf_civital_image_info_dataset = load_dataset("svjack/hf_civital_image_info")
|
26 |
+
hf_civital_image_info_df = hf_civital_image_info_dataset["train"].to_pandas()
|
27 |
+
|
28 |
+
def gen_interface(model_name, max_times = 3):
|
29 |
+
times = 0
|
30 |
+
gr_model_interface = None
|
31 |
+
while gr_model_interface is None and times < max_times:
|
32 |
+
try:
|
33 |
+
gr_model_interface = gr.load("models/{}".format(model_name),live=True)
|
34 |
+
except:
|
35 |
+
print("error {} times {}".format(model_name, times))
|
36 |
+
sleep(2)
|
37 |
+
times += 1
|
38 |
+
return gr_model_interface
|
39 |
+
|
40 |
+
#gr_model_interface.title
|
41 |
+
def toImgOpenCV(imgPIL): # Conver imgPIL to imgOpenCV
|
42 |
+
i = np.array(imgPIL) # After mapping from PIL to numpy : [R,G,B,A]
|
43 |
+
# numpy Image Channel system: [B,G,R,A]
|
44 |
+
red = i[:,:,0].copy(); i[:,:,0] = i[:,:,2].copy(); i[:,:,2] = red;
|
45 |
+
return i;
|
46 |
+
|
47 |
+
def toImgPIL(imgOpenCV): return Image.fromarray(cv2.cvtColor(imgOpenCV, cv2.COLOR_BGR2RGB));
|
48 |
+
|
49 |
+
def jpg_val_to_img(jpg_bytes):
|
50 |
+
img_buf = np.frombuffer(jpg_bytes, np.uint8)
|
51 |
+
img = cv2.imdecode(img_buf, cv2.IMREAD_UNCHANGED)
|
52 |
+
return toImgPIL(img)
|
53 |
+
|
54 |
+
|
55 |
+
model_list = hf_civital_image_info_df["hf_repo_id"].drop_duplicates().values.tolist()
|
56 |
+
model_interface_list = []
|
57 |
+
for model_name in tqdm(model_list):
|
58 |
+
gr_model_interface = gen_interface(model_name)
|
59 |
+
if gr_model_interface is not None:
|
60 |
+
model_interface_list.append(gr_model_interface)
|
61 |
+
if len(model_interface_list) >= MAX_MODEL_NUM:
|
62 |
+
break
|
63 |
+
|
64 |
+
def get_civital_iframe(url, width = 1400, height = 768, as_html = True, visible = False):
|
65 |
+
html= '''
|
66 |
+
<div style="justify-content: center; display: flex;">
|
67 |
+
<iframe
|
68 |
+
src="{}"
|
69 |
+
frameborder="0"
|
70 |
+
width="{}"
|
71 |
+
height="{}"
|
72 |
+
></iframe>
|
73 |
+
</div>
|
74 |
+
'''.format(url, width, height)
|
75 |
+
if as_html:
|
76 |
+
html = gr.HTML(html, visible = visible)
|
77 |
+
return html
|
78 |
+
|
79 |
+
def get_info_by_interface(gr_interface, model_interface_list = model_interface_list):
|
80 |
+
#### out: (gr_interface, civital_url, civital_info)
|
81 |
+
if hasattr(gr_interface, "app"):
|
82 |
+
civital_url = hf_civital_image_info_df[
|
83 |
+
hf_civital_image_info_df["hf_repo_id"] == gr_interface.title
|
84 |
+
]["civital_url"].iloc[0]
|
85 |
+
civital_info = hf_civital_image_info_df[
|
86 |
+
hf_civital_image_info_df["hf_repo_id"] == gr_interface.title
|
87 |
+
][["prompt", "image"]].values.tolist()
|
88 |
+
return gr_interface ,civital_url, civital_info
|
89 |
+
else:
|
90 |
+
civital_url = hf_civital_image_info_df[
|
91 |
+
hf_civital_image_info_df["hf_repo_id"] == gr_interface
|
92 |
+
]["civital_url"].iloc[0]
|
93 |
+
civital_info = hf_civital_image_info_df[
|
94 |
+
hf_civital_image_info_df["hf_repo_id"] == gr_interface
|
95 |
+
][["prompt", "image"]].values.tolist()
|
96 |
+
return list(filter(lambda x:x.title == gr_interface, model_interface_list))[0] ,civital_url, civital_info
|
97 |
+
|
98 |
+
def read_image_from_url(url):
|
99 |
+
response = requests.get(url)
|
100 |
+
img = Image.open(BytesIO(response.content))
|
101 |
+
return img
|
102 |
+
|
103 |
+
def image_click(images, evt: gr.SelectData, gr_interface_value,
|
104 |
+
):
|
105 |
+
img_selected = images[evt.index]
|
106 |
+
#print(img_selected)
|
107 |
+
im_data = img_selected["name"]
|
108 |
+
im = Image.open(im_data)
|
109 |
+
im_hash = imagehash.average_hash(
|
110 |
+
im, hash_size = 1024
|
111 |
+
)
|
112 |
+
min_diff = int(1e10)
|
113 |
+
#print(-1)
|
114 |
+
repo_card_im_dict = dict(
|
115 |
+
get_info_by_interface(gr_interface_value)[2]
|
116 |
+
)
|
117 |
+
min_repo_name = ""
|
118 |
+
for idx ,(repo_name, repo_card_image) in enumerate(repo_card_im_dict.items()):
|
119 |
+
repo_img = jpg_val_to_img(repo_card_image["bytes"])
|
120 |
+
repo_img_hash = imagehash.average_hash(
|
121 |
+
repo_img, hash_size = 1024
|
122 |
+
)
|
123 |
+
diff = im_hash - repo_img_hash
|
124 |
+
if diff < min_diff:
|
125 |
+
min_diff = diff
|
126 |
+
min_repo_name = repo_name
|
127 |
+
#print(idx)
|
128 |
+
prompt = min_repo_name
|
129 |
+
return prompt
|
130 |
+
#return prompt, im
|
131 |
+
|
132 |
+
def try_repo_act_func(civital_url, show_civital_button):
|
133 |
+
repo_html_iframe_hide = get_civital_iframe(civital_url, visible = True if show_civital_button == "Show Civital Page" else False)
|
134 |
+
return repo_html_iframe_hide, gr.Button("Hide Civital Page" if show_civital_button == "Show Civital Page" else "Show Civital Page")
|
135 |
+
|
136 |
+
with gr.Blocks(
|
137 |
+
css = '''
|
138 |
+
.header img {
|
139 |
+
float: middle;
|
140 |
+
width: 33px;
|
141 |
+
height: 33px;
|
142 |
+
}
|
143 |
+
.header h1 {
|
144 |
+
top: 18px;
|
145 |
+
left: 10px;
|
146 |
+
}
|
147 |
+
'''
|
148 |
+
) as demo:
|
149 |
+
gr.HTML(
|
150 |
+
'''
|
151 |
+
<center>
|
152 |
+
<div class="header">
|
153 |
+
<h1 class = "logo"> <img src="https://huggingface.co/spaces/svjack/Civital-Stable-Diffusion-HF/resolve/main/logo.png" alt="logo" /> π€ Civital Model on Huggingface </h1>
|
154 |
+
</center>
|
155 |
+
'''
|
156 |
+
)
|
157 |
+
|
158 |
+
with gr.Row():
|
159 |
+
with gr.Column():
|
160 |
+
with gr.Row():
|
161 |
+
hf_model_dropdown = gr.Dropdown(label = "π€ Hf model",
|
162 |
+
choices=list(map(lambda x: x.title, model_interface_list)),
|
163 |
+
value=list(map(lambda x: x.title, model_interface_list))[0],)
|
164 |
+
with gr.Column():
|
165 |
+
with gr.Row():
|
166 |
+
civital_prompt = gr.Textbox(label = "π€ Civital Prompt (Click from π right gallery to get them, and You can edit βοΈ yourself)",
|
167 |
+
interactive = True,
|
168 |
+
)
|
169 |
+
gen_button = gr.Button(label = "Generate")
|
170 |
+
hf_image = gr.Image(label = "π€ Image generate by π€ Huggingface", height = 768)
|
171 |
+
|
172 |
+
with gr.Column():
|
173 |
+
civital_info_gallery = gr.Gallery(
|
174 |
+
pd.Series(
|
175 |
+
get_info_by_interface(hf_model_dropdown.value)[2]
|
176 |
+
).sample(n = min(len(get_info_by_interface(hf_model_dropdown.value)[2]), 30)).map(lambda t2: t2[1]).map(lambda x: x["bytes"]).map(jpg_val_to_img).values.tolist(),
|
177 |
+
height = 1024,
|
178 |
+
label = "π±οΈπ β‘οΈ π Civital image samples",
|
179 |
+
object_fit = "contain"
|
180 |
+
)
|
181 |
+
|
182 |
+
with gr.Row():
|
183 |
+
with gr.Column():
|
184 |
+
try_repo_button = gr.Button("Show Civital Page")
|
185 |
+
civital_iframe_html = get_civital_iframe(
|
186 |
+
get_info_by_interface(hf_model_dropdown.value)[1]
|
187 |
+
)
|
188 |
+
|
189 |
+
hf_model_dropdown.change(
|
190 |
+
lambda x: pd.Series(
|
191 |
+
get_info_by_interface(x)[2]
|
192 |
+
).sample(n = min(len(get_info_by_interface(x)[2]), 30)).map(lambda t2: t2[1]).map(lambda x: x["bytes"]).map(jpg_val_to_img).values.tolist(),
|
193 |
+
hf_model_dropdown,
|
194 |
+
civital_info_gallery
|
195 |
+
)
|
196 |
+
hf_model_dropdown.change(
|
197 |
+
lambda _: (gr.Button("Show Civital Page"), gr.HTML(visible = False)),
|
198 |
+
None,
|
199 |
+
[try_repo_button, civital_iframe_html]
|
200 |
+
)
|
201 |
+
|
202 |
+
civital_info_gallery.select(
|
203 |
+
image_click,
|
204 |
+
[civital_info_gallery, hf_model_dropdown],
|
205 |
+
civital_prompt
|
206 |
+
)
|
207 |
+
gen_button.click(lambda hf_model_name, text_prompt:
|
208 |
+
get_info_by_interface(hf_model_name)[0](text_prompt),
|
209 |
+
[hf_model_dropdown, civital_prompt],
|
210 |
+
hf_image
|
211 |
+
)
|
212 |
+
|
213 |
+
try_repo_button.click(
|
214 |
+
lambda hf_model_name, button: try_repo_act_func(
|
215 |
+
get_info_by_interface(hf_model_name)[1]
|
216 |
+
, button),
|
217 |
+
[hf_model_dropdown, try_repo_button],
|
218 |
+
[civital_iframe_html, try_repo_button]
|
219 |
+
)
|
220 |
+
|
221 |
+
demo.launch(show_api = False)
|