#@title Prepare the Concepts Library to be used
import requests
import os
import gradio as gr
import wget
import torch
from torch import autocast
from diffusers import StableDiffusionPipeline
from huggingface_hub import HfApi
from transformers import CLIPTextModel, CLIPTokenizer
import html
community_icon_html = """
"""
loading_icon_html = """ """
share_js = """async () => {
async function uploadFile(file){
const UPLOAD_URL = 'https://huggingface.co/uploads';
const response = await fetch(UPLOAD_URL, {
method: 'POST',
headers: {
'Content-Type': file.type,
'X-Requested-With': 'XMLHttpRequest',
},
body: file, /// <- File inherits from Blob
});
const url = await response.text();
return url;
}
const gradioEl = document.querySelector('body > gradio-app');
const imgEls = gradioEl.querySelectorAll('#generated-gallery img');
const promptTxt = gradioEl.querySelector('#prompt_input input').value;
const shareBtnEl = gradioEl.querySelector('#share-btn');
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
if(!imgEls.length){
return;
};
shareBtnEl.style.pointerEvents = 'none';
shareIconEl.style.display = 'none';
loadingIconEl.style.removeProperty('display');
const files = await Promise.all(
[...imgEls].map(async (imgEl) => {
const res = await fetch(imgEl.src);
const blob = await res.blob();
const imgId = Date.now() % 200;
const fileName = `diffuse-the-rest-${{imgId}}.png`;
return new File([blob], fileName, { type: 'image/png' });
})
);
const REGEX_CONCEPT = /<(((?!<.+>).)+)>/gm;
const matches = [...promptTxt.matchAll(REGEX_CONCEPT)];
const concepts = matches.map(m => m[1]);
const conceptLibraryMd = concepts.map(c => `${c} `).join(`, `);
const urls = await Promise.all(files.map((f) => uploadFile(f)));
const htmlImgs = urls.map(url => ` `);
const htmlImgsMd = htmlImgs.join(`\n`);
const descriptionMd = `#### Prompt:
${promptTxt.replace(//g, '\\\>')}
#### Concepts Used:
${conceptLibraryMd}
#### Generations:
${htmlImgsMd}
`;
const params = new URLSearchParams({
title: promptTxt,
description: descriptionMd,
});
const paramsStr = params.toString();
window.open(`https://huggingface.co/spaces/sd-concepts-library/stable-diffusion-conceptualizer/discussions/new?${paramsStr}`, '_blank');
shareBtnEl.style.removeProperty('pointer-events');
shareIconEl.style.removeProperty('display');
loadingIconEl.style.display = 'none';
}"""
api = HfApi()
models_list = api.list_models(author="sd-concepts-library", sort="likes", direction=-1)
models = []
# NEW LOGIN ATTEMPT {{{
api_key = os.environ['api_key']
my_token = api_key
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16, use_auth_token=my_token).to("cuda")
# }}}
# pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True, revision="fp16", torch_dtype=torch.float16).to("cuda")
def load_learned_embed_in_clip(learned_embeds_path, text_encoder, tokenizer, token=None):
loaded_learned_embeds = torch.load(learned_embeds_path, map_location="cpu")
_old_token = token
# separate token and the embeds
trained_token = list(loaded_learned_embeds.keys())[0]
embeds = loaded_learned_embeds[trained_token]
# cast to dtype of text_encoder
dtype = text_encoder.get_input_embeddings().weight.dtype
# add the token in tokenizer
token = token if token is not None else trained_token
num_added_tokens = tokenizer.add_tokens(token)
i = 1
print("start while loop **************")
while(num_added_tokens == 0):
token = f"{token[:-1]}-{i}>"
num_added_tokens = tokenizer.add_tokens(token)
print("i --> ", i)
print("token --> ", token)
print("num_added_tokens --> ", num_added_tokens)
i+=1
print("end while loop **************")
# resize the token embeddings
text_encoder.resize_token_embeddings(len(tokenizer))
# get the id for the token and assign the embeds
token_id = tokenizer.convert_tokens_to_ids(token)
print("&&&&&&&&&&&&&&&&")
print("learned_embeds_path --> ", learned_embeds_path)
print("text_encoder --> ", text_encoder)
print("tokenizer --> ", tokenizer)
print("_old_token --> ", _old_token)
print("token --> ", token)
print("trained_token --> ", trained_token)
print("dtype --> ", dtype)
print("num_added_tokens --> ", num_added_tokens)
print("text_encoder --> ", text_encoder)
print("token_id --> ", token_id)
print("embeds --> ", embeds)
print("&&&&&&&&&&&&&&&&")
text_encoder.get_input_embeddings().weight.data[token_id] = embeds # <------ POINT OF FAILURE
return token
ahx_model_list = [model for model in models_list if "ahx" in model.modelId]
# UNDER CONSTRUCTION ---------------------------------------------------------------
from time import sleep
print("--------------------------------------------------")
print("--------------------------------------------------")
print("Setting up the public library........")
print("--------------------------------------------------")
for model in ahx_model_list:
model_content = {}
model_id = model.modelId
model_content["id"] = model_id
embeds_url = f"https://huggingface.co/{model_id}/resolve/main/learned_embeds.bin"
os.makedirs(model_id,exist_ok = True)
if not os.path.exists(f"{model_id}/learned_embeds.bin"):
try:
wget.download(embeds_url, out=model_id)
except:
# print("FAILURE: <-------------------------------------------------------------------")
# print("model -->", model)
# print("model_id -->", model_id)
# print("CONTINUING - MODEL NOT LOADING")
continue
token_identifier = f"https://huggingface.co/{model_id}/raw/main/token_identifier.txt"
response = requests.get(token_identifier)
token_name = response.text
concept_type = f"https://huggingface.co/{model_id}/raw/main/type_of_concept.txt"
response = requests.get(concept_type)
concept_name = response.text
model_content["concept_type"] = concept_name
images = []
for i in range(4):
url = f"https://huggingface.co/{model_id}/resolve/main/concept_images/{i}.jpeg"
image_download = requests.get(url)
url_code = image_download.status_code
if(url_code == 200):
file = open(f"{model_id}/{i}.jpeg", "wb") ## Creates the file for image
file.write(image_download.content) ## Saves file content
file.close()
images.append(f"{model_id}/{i}.jpeg")
model_content["images"] = images
#if token cannot be loaded, skip it
try:
learned_token = load_learned_embed_in_clip(f"{model_id}/learned_embeds.bin", pipe.text_encoder, pipe.tokenizer, token_name)
print("success / model loaded:")
print("model -->", model)
print("model_id -->", model_id)
except:
print("FAILURE: <- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
print("model -->", model)
print("model_id -->", model_id)
print("CONTINUING - MODEL NOT LOADING")
continue
model_content["token"] = learned_token
models.append(model_content)
models.append(model_content)
print("--------------------------------------------------")
sleep(5)
print("--------------------------------------------------")
print("--------------------------------------------------")
print("--------------------------------------------------")
sleep(60)
#@title Run the app to navigate around [the Library](https://huggingface.co/sd-concepts-library)
#@markdown Click the `Running on public URL:` result to run the Gradio app
SELECT_LABEL = "Select concept"
def assembleHTML(model):
html_gallery = ''
html_gallery = html_gallery+'''
'''
cap = 0
for model in models:
html_gallery = html_gallery+f'''
'''
for image in model["images"]:
html_gallery = html_gallery + f'''
'''
html_gallery = html_gallery+'''
'''
cap += 1
if(cap == 99):
break
html_gallery = html_gallery+'''
'''
return html_gallery
def title_block(title, id):
return gr.Markdown(f"### [`{title}`](https://huggingface.co/{id})")
def image_block(image_list, concept_type):
return gr.Gallery(
label=concept_type, value=image_list, elem_id="gallery"
).style(grid=[2], height="auto")
def checkbox_block():
checkbox = gr.Checkbox(label=SELECT_LABEL).style(container=False)
return checkbox
def infer(text):
#with autocast("cuda"):
images_list = pipe(
[text],
num_inference_steps=50,
guidance_scale=7.5
)
#output_images = []
#for i, image in enumerate(images_list.images):
# output_images.append(image)
return images_list.images, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
# idetnical to `infer` function without gradio state updates for share btn
def infer_examples(text):
#with autocast("cuda"):
images_list = pipe(
[text],
num_inference_steps=50,
guidance_scale=7.5
)
#output_images = []
#for i, image in enumerate(images_list["sample"]):
# output_images.append(image)
return images_list.images
css = '''
.gradio-container {font-family: 'IBM Plex Sans', sans-serif}
#top_title{margin-bottom: .5em}
#top_title h2{margin-bottom: 0; text-align: center}
/*#main_row{flex-wrap: wrap; gap: 1em; max-height: 550px; overflow-y: scroll; flex-direction: row}*/
#component-3{height: 760px; overflow: auto}
#component-9{position: sticky;top: 0;align-self: flex-start;}
@media (min-width: 768px){#main_row > div{flex: 1 1 32%; margin-left: 0 !important}}
.gr-prose code::before, .gr-prose code::after {content: "" !important}
::-webkit-scrollbar {width: 10px}
::-webkit-scrollbar-track {background: #f1f1f1}
::-webkit-scrollbar-thumb {background: #888}
::-webkit-scrollbar-thumb:hover {background: #555}
.gr-button {white-space: nowrap}
.gr-button:focus {
border-color: rgb(147 197 253 / var(--tw-border-opacity));
outline: none;
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
--tw-border-opacity: 1;
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
--tw-ring-opacity: .5;
}
#prompt_input{flex: 1 3 auto; width: auto !important;}
#prompt_area{margin-bottom: .75em}
#prompt_area > div:first-child{flex: 1 3 auto}
.animate-spin {
animation: spin 1s linear infinite;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
#share-btn-container {
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
}
#share-btn {
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
}
#share-btn * {
all: unset;
}
'''
# examples = ["a in style", "a style mecha robot", "a piano being played by ", "Candid photo of , high resolution photo, trending on artstation, interior design"]
examples = []
with gr.Blocks(css=css) as demo:
state = gr.Variable({
'selected': -1
})
state = {}
def update_state(i):
global checkbox_states
if(checkbox_states[i]):
checkbox_states[i] = False
state[i] = False
else:
state[i] = True
checkbox_states[i] = True
gr.HTML('''
🧑🚀 Astronaut Horse Concept Loader
Run your own text prompts into fine-tuned artist concepts, see the example below. Currently only loading two artist concepts while testing. Soon will automatically be able to add all concepts from astronaut horse artist collaborations. There's some buggy stuff here that'll be cleared up next week but I wanted to at least get this usable for the weekend!
http://www.astronaut.horse
Prompt Examples Using Artist Token:
"a photograph of pink crystals in the style of <artist>"
"a painting of a horse in the style of <ivan-stripes>"
Currently-Usable Concept Tokens:
''')
with gr.Row():
# with gr.Column():
# gr.Markdown(f"")
# with gr.Row():
# image_blocks = []
# #for i, model in enumerate(models):
# with gr.Box().style(border=None):
# gr.HTML(assembleHTML(models))
# #title_block(model["token"], model["id"])
# #image_blocks.append(image_block(model["images"], model["concept_type"]))
with gr.Column():
with gr.Box():
with gr.Row(elem_id="prompt_area").style(mobile_collapse=False, equal_height=True):
text = gr.Textbox(
label="Enter your prompt", placeholder="Enter your prompt", show_label=False, max_lines=1, elem_id="prompt_input"
).style(
border=(True, False, True, True),
rounded=(True, False, False, True),
container=False,
)
btn = gr.Button("generate image",elem_id="run_btn").style(
margin=False,
rounded=(False, True, True, False),
)
with gr.Row().style():
infer_outputs = gr.Gallery(show_label=False, elem_id="generated-gallery").style(grid=[2], height="512px")
with gr.Row():
gr.HTML("
")
with gr.Row():
gr.Examples(examples=examples, fn=infer_examples, inputs=[text], outputs=infer_outputs, cache_examples=True)
with gr.Group(elem_id="share-btn-container"):
community_icon = gr.HTML(community_icon_html, visible=False)
loading_icon = gr.HTML(loading_icon_html, visible=False)
checkbox_states = {}
inputs = [text]
btn.click(
infer,
inputs=inputs,
outputs=[infer_outputs, community_icon, loading_icon]
)
# after loading_icon on line 392.5
# share_button = gr.Button("", elem_id="share-btn", visible=False)
# and update outputs=[...] on line 398 to match this
# outputs=[infer_outputs, community_icon, loading_icon, share_button]
# then this has to be added after line 399
# share_button.click(
# None,
# [],
# [],
# _js=share_js,
# )
demo.queue(max_size=20).launch()