nateraw's picture
Update app.py
6890e40
raw
history blame
1.69 kB
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():
all_files = list_repo_files(repo_id, repo_type='dataset')
global data
data = defaultdict(list)
for file in all_files:
path = Path(file)
if path.name == '.gitattributes':
continue
if path.suffix in ['.png', '.jpg', '.jpeg']:
data[path.parent.name].append(file)
return gr.update(value=list(data.keys()))
get_data()
def fn(run):
images = [(hf_hub_url(repo_id=repo_id, filename=img, repo_type='dataset'), f"Seed:\n{Path(img).stem}") for img in data[run]]
prompt_config_url = hf_hub_url(
repo_id=repo_id,
filename=f'{run}/prompt_config.json',
repo_type='dataset'
)
prompt_config_json = requests.get(prompt_config_url).json()
return prompt_config_json, images
with gr.Blocks() as demo:
with gr.Column(variant="panel"):
with gr.Row(variant="compact"):
refresh_btn = gr.Button("Refresh").style(full_width=False)
with gr.Row(variant="compact"):
run = gr.Dropdown(list(data.keys()), interactive=True).style(container=False)
btn = gr.Button("View images").style(full_width=False)
with gr.Row(variant="compact"):
data_json = gr.Json()
gallery = gr.Gallery(
label="Generated images", show_label=False, elem_id="gallery"
).style(grid=[5], height="auto")
refresh_btn.click(get_data, None, run)
btn.click(fn, run, [data_json, gallery])
demo.launch(debug=True)