nateraw commited on
Commit
4091c83
1 Parent(s): fd877b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -36
app.py CHANGED
@@ -24,45 +24,110 @@ def get_data(revision='main'):
24
 
25
  return data, prompts
26
 
27
- def on_refresh(data_state):
28
- data, prompts = get_data()
29
- data_state.update(dict(data))
30
- return [
31
- data_state,
32
- gr.update(choices=prompts),
33
- ]
34
 
35
- def on_submit(run_idx, data):
36
- run = sorted(data.keys())[run_idx]
37
- images = [(hf_hub_url(repo_id=repo_id, filename=img, repo_type='dataset'), f"Seed:\n{Path(img).stem}") for img in data[run]]
38
- prompt_config_url = hf_hub_url(
39
- repo_id=repo_id,
40
- filename=f'{run}/prompt_config.json',
41
- repo_type='dataset'
42
- )
43
- prompt_config_json = requests.get(prompt_config_url).json()
44
- return prompt_config_json, images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- data, prompts = get_data()
47
- runs = sorted(data.keys())
48
- with gr.Blocks() as demo:
49
- data_state = gr.State(data)
50
- with gr.Column(variant="panel"):
51
- with gr.Row(variant="compact"):
52
- refresh_btn = gr.Button("Refresh").style(full_width=True)
53
 
54
- with gr.Row(variant="compact"):
55
- run_idx = gr.Dropdown(prompts, type='index') #interactive=True) # .style(container=False)
56
- btn = gr.Button("View images").style(full_width=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- with gr.Row(variant="compact"):
59
- data_json = gr.Json()
60
- gallery = gr.Gallery(
61
- label="Generated images", show_label=False, elem_id="gallery"
62
- ).style(grid=[5], height="auto")
63
 
64
- refresh_btn.click(on_refresh, data_state, [data_state, run_idx])
65
- btn.click(on_submit, [run_idx, data_state], [data_json, gallery])
 
66
 
67
- if __name__ == '__main__':
68
- demo.launch(debug=True)
 
24
 
25
  return data, prompts
26
 
 
 
 
 
 
 
 
27
 
28
+ def assembleHTML(data):
29
+ # Borrowed mostly from:
30
+ # https://huggingface.co/spaces/sd-concepts-library/stable-diffusion-conceptualizer
31
+ html_gallery = ''
32
+ html_gallery = html_gallery+'''
33
+ <div class="flex gr-gap gr-form-gap row gap-4 w-full flex-wrap" id="main_row">
34
+ '''
35
+ for key, values in data.items():
36
+ html_gallery = html_gallery+ f'''
37
+ <div class="gr-block gr-box relative w-full overflow-hidden border-solid border border-gray-200 gr-panel">
38
+ <div class="output-markdown gr-prose" style="max-width: 100%;">
39
+ <h3>
40
+ <a href="https://huggingface.co/datasets/nateraw/stable-diffusion-gallery/blob/main/{key}/prompt_config.json" target="_blank">
41
+ <code>{ key }</code>
42
+ </a>
43
+ </h3>
44
+ </div>
45
+ <div id="gallery" class="gr-block gr-box relative w-full overflow-hidden border-solid border border-gray-200">
46
+ <div class="wrap svelte-17ttdjv opacity-0"></div>
47
+ <div class="absolute left-0 top-0 py-1 px-2 rounded-br-lg shadow-sm text-xs text-gray-500 flex items-center pointer-events-none bg-white z-20 border-b border-r border-gray-100 dark:bg-gray-900">
48
+ <span class="mr-2 h-[12px] w-[12px] opacity-80">
49
+ <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-image">
50
+ <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
51
+ <circle cx="8.5" cy="8.5" r="1.5"></circle>
52
+ <polyline points="21 15 16 10 5 21"></polyline>
53
+ </svg>
54
+ </span> Images
55
+ </div>
56
+ <div class="overflow-y-auto h-full p-2" style="position: relative;">
57
+ <div class="grid gap-2 grid-cols-2 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-2 xl:grid-cols-2 2xl:grid-cols-2 svelte-1g9btlg pt-6">
58
+ '''
59
+ for image in values[:4]:
60
+ html_gallery = html_gallery + f'''
61
+ <button class="gallery-item svelte-1g9btlg">
62
+ <img alt="" loading="lazy" class="h-full w-full overflow-hidden object-contain" src="{hf_hub_url(repo_id, filename=image, repo_type='dataset')}">
63
+ </button>
64
+ '''
65
+ html_gallery = html_gallery+'''
66
+ </div>
67
+ <iframe style="display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; border: 0; opacity: 0; pointer-events: none; z-index: -1;" aria-hidden="true" tabindex="-1" src="about:blank"></iframe>
68
+ </div>
69
+ </div>
70
+ </div>
71
+ '''
72
+ html_gallery = html_gallery+'''
73
+ </div>
74
+ '''
75
+ return html_gallery
76
 
 
 
 
 
 
 
 
77
 
78
+ css = '''
79
+ .gradio-container {font-family: 'IBM Plex Sans', sans-serif}
80
+ #top_title{margin-bottom: .5em}
81
+ #top_title h2{margin-bottom: 0; text-align: center}
82
+ /*#main_row{flex-wrap: wrap; gap: 1em; max-height: 550px; overflow-y: scroll; flex-direction: row}*/
83
+ #component-3{height: 760px; overflow: auto}
84
+ #component-9{position: sticky;top: 0;align-self: flex-start;}
85
+ @media (min-width: 768px){#main_row > div{flex: 1 1 32%; margin-left: 0 !important}}
86
+ .gr-prose code::before, .gr-prose code::after {content: "" !important}
87
+ ::-webkit-scrollbar {width: 10px}
88
+ ::-webkit-scrollbar-track {background: #f1f1f1}
89
+ ::-webkit-scrollbar-thumb {background: #888}
90
+ ::-webkit-scrollbar-thumb:hover {background: #555}
91
+ .gr-button {white-space: nowrap}
92
+ .gr-button:focus {
93
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
94
+ outline: none;
95
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
96
+ --tw-border-opacity: 1;
97
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
98
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
99
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
100
+ --tw-ring-opacity: .5;
101
+ }
102
+ #prompt_input{flex: 1 3 auto; width: auto !important;}
103
+ #prompt_area{margin-bottom: .75em}
104
+ #prompt_area > div:first-child{flex: 1 3 auto}
105
+ .animate-spin {
106
+ animation: spin 1s linear infinite;
107
+ }
108
+ @keyframes spin {
109
+ from {
110
+ transform: rotate(0deg);
111
+ }
112
+ to {
113
+ transform: rotate(360deg);
114
+ }
115
+ }
116
+ #share-btn-container {
117
+ 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;
118
+ }
119
+ #share-btn {
120
+ 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;
121
+ }
122
+ #share-btn * {
123
+ all: unset;
124
+ }
125
+ '''
126
 
127
+ data, prompts = get_data()
 
 
 
 
128
 
129
+ with gr.Blocks(css=css) as demo:
130
+ with gr.Box().style(border=None):
131
+ html = gr.HTML(assembleHTML(data))
132
 
133
+ demo.launch(debug=True)