Spaces:
Runtime error
Runtime error
File size: 7,776 Bytes
681ad9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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 162 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
import gradio as gr
import pandas as pd
import requests
import os
import numpy as np
import re
from tqdm import tqdm
from time import sleep
from PIL import Image
import requests
from io import BytesIO
from datasets import Dataset, load_dataset
import json
import cv2
import pathlib
import imagehash
MAX_MODEL_NUM = 300
'''
Yntec digplay
'''
hf_civital_image_info_dataset = load_dataset("svjack/hf_civital_image_info")
hf_civital_image_info_df = hf_civital_image_info_dataset["train"].to_pandas()
def gen_interface(model_name, max_times = 3):
times = 0
gr_model_interface = None
while gr_model_interface is None and times < max_times:
try:
gr_model_interface = gr.load("models/{}".format(model_name),live=True)
except:
print("error {} times {}".format(model_name, times))
sleep(2)
times += 1
return gr_model_interface
#gr_model_interface.title
def toImgOpenCV(imgPIL): # Conver imgPIL to imgOpenCV
i = np.array(imgPIL) # After mapping from PIL to numpy : [R,G,B,A]
# numpy Image Channel system: [B,G,R,A]
red = i[:,:,0].copy(); i[:,:,0] = i[:,:,2].copy(); i[:,:,2] = red;
return i;
def toImgPIL(imgOpenCV): return Image.fromarray(cv2.cvtColor(imgOpenCV, cv2.COLOR_BGR2RGB));
def jpg_val_to_img(jpg_bytes):
img_buf = np.frombuffer(jpg_bytes, np.uint8)
img = cv2.imdecode(img_buf, cv2.IMREAD_UNCHANGED)
return toImgPIL(img)
model_list = hf_civital_image_info_df["hf_repo_id"].drop_duplicates().values.tolist()
model_interface_list = []
for model_name in tqdm(model_list):
gr_model_interface = gen_interface(model_name)
if gr_model_interface is not None:
model_interface_list.append(gr_model_interface)
if len(model_interface_list) >= MAX_MODEL_NUM:
break
def get_civital_iframe(url, width = 1400, height = 768, as_html = True, visible = False):
html= '''
<div style="justify-content: center; display: flex;">
<iframe
src="{}"
frameborder="0"
width="{}"
height="{}"
></iframe>
</div>
'''.format(url, width, height)
if as_html:
html = gr.HTML(html, visible = visible)
return html
def get_info_by_interface(gr_interface, model_interface_list = model_interface_list):
#### out: (gr_interface, civital_url, civital_info)
if hasattr(gr_interface, "app"):
civital_url = hf_civital_image_info_df[
hf_civital_image_info_df["hf_repo_id"] == gr_interface.title
]["civital_url"].iloc[0]
civital_info = hf_civital_image_info_df[
hf_civital_image_info_df["hf_repo_id"] == gr_interface.title
][["prompt", "image"]].values.tolist()
return gr_interface ,civital_url, civital_info
else:
civital_url = hf_civital_image_info_df[
hf_civital_image_info_df["hf_repo_id"] == gr_interface
]["civital_url"].iloc[0]
civital_info = hf_civital_image_info_df[
hf_civital_image_info_df["hf_repo_id"] == gr_interface
][["prompt", "image"]].values.tolist()
return list(filter(lambda x:x.title == gr_interface, model_interface_list))[0] ,civital_url, civital_info
def read_image_from_url(url):
response = requests.get(url)
img = Image.open(BytesIO(response.content))
return img
def image_click(images, evt: gr.SelectData, gr_interface_value,
):
img_selected = images[evt.index]
#print(img_selected)
im_data = img_selected["name"]
im = Image.open(im_data)
im_hash = imagehash.average_hash(
im, hash_size = 1024
)
min_diff = int(1e10)
#print(-1)
repo_card_im_dict = dict(
get_info_by_interface(gr_interface_value)[2]
)
min_repo_name = ""
for idx ,(repo_name, repo_card_image) in enumerate(repo_card_im_dict.items()):
repo_img = jpg_val_to_img(repo_card_image["bytes"])
repo_img_hash = imagehash.average_hash(
repo_img, hash_size = 1024
)
diff = im_hash - repo_img_hash
if diff < min_diff:
min_diff = diff
min_repo_name = repo_name
#print(idx)
prompt = min_repo_name
return prompt
#return prompt, im
def try_repo_act_func(civital_url, show_civital_button):
repo_html_iframe_hide = get_civital_iframe(civital_url, visible = True if show_civital_button == "Show Civital Page" else False)
return repo_html_iframe_hide, gr.Button("Hide Civital Page" if show_civital_button == "Show Civital Page" else "Show Civital Page")
with gr.Blocks(
css = '''
.header img {
float: middle;
width: 33px;
height: 33px;
}
.header h1 {
top: 18px;
left: 10px;
}
'''
) as demo:
gr.HTML(
'''
<center>
<div class="header">
<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>
</center>
'''
)
with gr.Row():
with gr.Column():
with gr.Row():
hf_model_dropdown = gr.Dropdown(label = "π€ Hf model",
choices=list(map(lambda x: x.title, model_interface_list)),
value=list(map(lambda x: x.title, model_interface_list))[0],)
with gr.Column():
with gr.Row():
civital_prompt = gr.Textbox(label = "π€ Civital Prompt (Click from π right gallery to get them, and You can edit βοΈ yourself)",
interactive = True,
)
gen_button = gr.Button(label = "Generate")
hf_image = gr.Image(label = "π€ Image generate by π€ Huggingface", height = 768)
with gr.Column():
civital_info_gallery = gr.Gallery(
pd.Series(
get_info_by_interface(hf_model_dropdown.value)[2]
).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(),
height = 1024,
label = "π±οΈπ β‘οΈ π Civital image samples",
object_fit = "contain"
)
with gr.Row():
with gr.Column():
try_repo_button = gr.Button("Show Civital Page")
civital_iframe_html = get_civital_iframe(
get_info_by_interface(hf_model_dropdown.value)[1]
)
hf_model_dropdown.change(
lambda x: pd.Series(
get_info_by_interface(x)[2]
).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(),
hf_model_dropdown,
civital_info_gallery
)
hf_model_dropdown.change(
lambda _: (gr.Button("Show Civital Page"), gr.HTML(visible = False)),
None,
[try_repo_button, civital_iframe_html]
)
civital_info_gallery.select(
image_click,
[civital_info_gallery, hf_model_dropdown],
civital_prompt
)
gen_button.click(lambda hf_model_name, text_prompt:
get_info_by_interface(hf_model_name)[0](text_prompt),
[hf_model_dropdown, civital_prompt],
hf_image
)
try_repo_button.click(
lambda hf_model_name, button: try_repo_act_func(
get_info_by_interface(hf_model_name)[1]
, button),
[hf_model_dropdown, try_repo_button],
[civital_iframe_html, try_repo_button]
)
demo.launch(show_api = False)
|