import gradio as gr
from pathlib import Path
from huggingface_hub import list_repo_files, hf_hub_url
from collections import defaultdict
import requests
repo_id = 'nateraw/stable-diffusion-gallery'
def get_data(revision='main'):
data = defaultdict(list)
for file in list_repo_files(repo_id, repo_type='dataset', revision=revision):
path = Path(file)
if path.name == '.gitattributes':
continue
if path.suffix in ['.png', '.jpg', '.jpeg']:
data[path.parent.name].append(file)
prompts = []
for key in sorted(data.keys()):
prompt_cfg_url = hf_hub_url(repo_id=repo_id, filename=f"{key}/prompt_config.json", repo_type='dataset')
prompt_cfg = requests.get(prompt_cfg_url).json()
text = prompt_cfg.get('prompt')
prompts.append(text)
return data, prompts
def assembleHTML(data):
# Borrowed mostly from:
# https://huggingface.co./spaces/sd-concepts-library/stable-diffusion-conceptualizer
html_gallery = ''
html_gallery = html_gallery+'''
'''
for key, values in data.items():
html_gallery = html_gallery+ f'''
'''
for image in values[:4]:
html_gallery = html_gallery + f'''
'''
html_gallery = html_gallery+'''
'''
html_gallery = html_gallery+'''
'''
return html_gallery
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;
}
'''
data, prompts = get_data()
with gr.Blocks(css=css) as demo:
with gr.Box().style(border=None):
html = gr.HTML(assembleHTML(data))
demo.launch(debug=True)