File size: 8,022 Bytes
681ad9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8ff652
681ad9f
b8ff652
681ad9f
 
 
 
2e86483
 
681ad9f
 
 
 
 
 
7365f59
681ad9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e86483
681ad9f
 
 
 
 
 
 
9f39d4d
681ad9f
2e86483
681ad9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e86483
681ad9f
2e86483
 
205dd67
2e86483
 
681ad9f
2e86483
681ad9f
2e86483
 
205dd67
2e86483
 
681ad9f
2e86483
681ad9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e86483
 
 
681ad9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e86483
681ad9f
 
 
 
 
 
 
 
1cc8980
 
681ad9f
 
2e86483
681ad9f
 
e1897b2
681ad9f
 
 
2e86483
681ad9f
 
 
 
2e86483
681ad9f
 
 
 
 
2e86483
 
681ad9f
 
 
 
 
 
 
 
2e86483
681ad9f
 
2e86483
681ad9f
2e86483
681ad9f
 
2e86483
681ad9f
2e86483
 
681ad9f
 
 
2e86483
681ad9f
 
 
 
 
 
 
 
2e86483
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
223
224
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

API_TOKEN = os.environ.get("HF_READ_TOKEN")

MAX_MODEL_NUM = 300
'''
Yntec digplay
'''

hf_civitai_image_info_dataset = load_dataset("svjack/hf_civitai_image_info_v0", token = API_TOKEN)
hf_civitai_image_info_df = hf_civitai_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,preprocess = False)
        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_civitai_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
print("load model num : {}".format(len(model_interface_list)))

def get_civitai_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, civitai_url, civitai_info)
    if hasattr(gr_interface, "app"):
        civitai_url = hf_civitai_image_info_df[
            hf_civitai_image_info_df["hf_repo_id"] == gr_interface.title
        ]["civital_url"].iloc[0]
        civitai_info = hf_civitai_image_info_df[
            hf_civitai_image_info_df["hf_repo_id"] == gr_interface.title
        ][["prompt", "image"]].values.tolist()
        return gr_interface ,civitai_url, civitai_info
    else:
        civitai_url = hf_civitai_image_info_df[
            hf_civitai_image_info_df["hf_repo_id"] == gr_interface
        ]["civital_url"].iloc[0]
        civitai_info = hf_civitai_image_info_df[
            hf_civitai_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] ,civitai_url, civitai_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(civitai_url, show_civitai_button):
    repo_html_iframe_hide = get_civitai_iframe(civitai_url, visible = True if show_civitai_button == "Show Civitai Page" else False)
    return repo_html_iframe_hide,  gr.Button("Hide Civitai Page" if show_civitai_button == "Show Civitai Page" else "Show Civitai 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/Civitai-Stable-Diffusion-HF/resolve/main/civitai_logo.webp" alt="logo" />  πŸ€— Civitai Model on Huggingface </h1>
        </center>
        '''
    )

    with gr.Row():
        with gr.Column():
            with gr.Row():
                hf_model_dropdown = gr.Dropdown(label = "πŸ€— Hf model",
                    choices=sorted(map(lambda x: x.title, model_interface_list)),
                    value=sorted(map(lambda x: x.title, model_interface_list))[0],)
            with gr.Column():
                with gr.Row():
                    civitai_prompt = gr.Textbox(label = "🀭 Civitai Prompt (Click from πŸ‘‰ right gallery to get them, and You can edit ✍️ yourself) Don't use them to generate NSFW content, such as porns. πŸ˜¬πŸ‘Ž Or you will get βŒπŸ‘€",
                        interactive = True,
                    )
                gen_button = gr.Button(label = "Generate")
                hf_image = gr.Image(label = "🀭 Image generate by πŸ€— Huggingface", height = 768)

        with gr.Column():
            civitai_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 = "πŸ–±οΈπŸ‘‡ ➑️ πŸ‘ˆ Civitai image samples",
                object_fit = "contain"
            )

    with gr.Row():
        with gr.Column():
            try_repo_button = gr.Button("Show Civitai Page")
            civitai_iframe_html = get_civitai_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,
        civitai_info_gallery
    )
    hf_model_dropdown.change(
        lambda _: (gr.Button("Show Civitai Page"), gr.HTML(visible = False)),
        None,
        [try_repo_button, civitai_iframe_html]
    )

    civitai_info_gallery.select(
            image_click,
            [civitai_info_gallery, hf_model_dropdown],
            civitai_prompt
    )
    gen_button.click(lambda hf_model_name, text_prompt:
            get_info_by_interface(hf_model_name)[0](text_prompt),
            [hf_model_dropdown, civitai_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],
        [civitai_iframe_html, try_repo_button]
    )

demo.launch(show_api = False)