Merge pull request #1150 from ZeroCool940711/dev

Added the Home tab and gallery tab on txt2img made by @devilismyfriend
This commit is contained in:
ZeroCool 2022-09-15 06:31:38 -07:00 committed by GitHub
commit b7d6329dc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 247 additions and 105 deletions

View File

@ -92,4 +92,20 @@ button[kind="header"] {
/***********************************************************
* Additional CSS for other elements
************************************************************/
************************************************************/
button[data-baseweb="tab"] {
font-size: 20px;
}
@media (min-width: 1200px){
h1 {
font-size: 1.75rem;
}
}
#tabs-1-tabpanel-0 > div:nth-child(1) > div > div.stTabs.css-0.exp6ofz0 {
width: 50rem;
align-self: center;
}
div.gallery:hover {
border: 1px solid #777;
}

View File

@ -1,5 +1,18 @@
from webui_streamlit import st, defaults
# base webui import and utils.
from webui_streamlit import st
from sd_utils import *
# streamlit imports
#other imports
# Temp imports
# end of imports
#---------------------------------------------------------------------------------------------------------------
import os
from PIL import Image
@ -30,15 +43,17 @@ def getLatestGeneratedImagesFromPath():
files.append(os.path.join(r, file))
#sort the files by date
files.sort(key=os.path.getmtime)
#reverse the list so the latest images are first
for f in files:
img = Image.open(f)
files[files.index(f)] = img
#get the latest 10 files
#get all the files with the .png or .jpg extension
#sort files by date
#get the latest 10 files
latestFiles = files[-10:]
latestFiles = files
#reverse the list
latestFiles.reverse()
return latestFiles
@ -98,41 +113,109 @@ def layout():
# create a tab for the gallery
#st.markdown("<h2 style='text-align: center; color: white;'>Gallery</h2>", unsafe_allow_html=True)
#st.markdown("<h2 style='text-align: center; color: white;'>Gallery</h2>", unsafe_allow_html=True)
history_tab, discover_tabs, settings_tab = st.tabs(["History","Discover","Settings"])
with discover_tabs:
st.markdown("<h1 style='text-align: center; color: white;'>Soon :)</h1>", unsafe_allow_html=True)
with settings_tab:
st.markdown("<h1 style='text-align: center; color: white;'>Soon :)</h1>", unsafe_allow_html=True)
history_tab, discover_tabs = st.tabs(["History","Discover"])
latestImages = getLatestGeneratedImagesFromPath()
st.session_state['latestImages'] = latestImages
with history_tab:
##---------------------------------------------------------
## image slideshow test
## Number of entries per screen
#slideshow_N = 9
#slideshow_page_number = 0
#slideshow_last_page = len(latestImages) // slideshow_N
## Add a next button and a previous button
#slideshow_prev, slideshow_image_col , slideshow_next = st.columns([1, 10, 1])
#with slideshow_image_col:
#slideshow_image = st.empty()
#slideshow_image.image(st.session_state['latestImages'][0])
#current_image = 0
#if slideshow_next.button("Next", key=1):
##print (current_image+1)
#current_image = current_image+1
#slideshow_image.image(st.session_state['latestImages'][current_image+1])
#if slideshow_prev.button("Previous", key=0):
##print ([current_image-1])
#current_image = current_image-1
#slideshow_image.image(st.session_state['latestImages'][current_image - 1])
#---------------------------------------------------------
placeholder = st.empty()
latestImages = getLatestGeneratedImagesFromPath()
st.session_state['latestImages'] = latestImages
# image gallery
# Number of entries per screen
gallery_N = 9
gallery_page_number = 0
#gallery_last_page = len(latestImages) // gallery_N
# Add a next button and a previous button
#gallery_prev, gallery_pagination , gallery_next = st.columns([1, 10, 1])
# the pagination doesnt work for now so its better to disable the buttons.
#if gallery_next.button("Next", key=3):
#if gallery_page_number + 1 > gallery_last_page:
#gallery_page_number = 0
#else:
#gallery_page_number += 1
#if gallery_prev.button("Previous", key=2):
#if gallery_page_number - 1 < 0:
#gallery_page_number = gallery_last_page
#else:
#gallery_page_number -= 1
# Get start and end indices of the next page of the dataframe
gallery_start_idx = gallery_page_number * gallery_N
gallery_end_idx = (1 + gallery_page_number) * gallery_N
#---------------------------------------------------------
#populate the 3 images per column
with placeholder.container():
col1, col2, col3 = st.columns(3)
col1_cont = st.container()
col2_cont = st.container()
col3_cont = st.container()
#print (len(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx]))
with col1_cont:
with col1:
st.image(st.session_state['latestImages'][0])
st.image(st.session_state['latestImages'][3])
st.image(st.session_state['latestImages'][6])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][0])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][3])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][6])
with col2_cont:
with col2:
st.image(st.session_state['latestImages'][1])
st.image(st.session_state['latestImages'][4])
st.image(st.session_state['latestImages'][7])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][1])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][4])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][7])
with col3_cont:
with col3:
st.image(st.session_state['latestImages'][2])
st.image(st.session_state['latestImages'][5])
st.image(st.session_state['latestImages'][8])
st.session_state['historyTab'] = [history_tab,col1,col2,col3,placeholder,col1_cont,col2_cont,col3_cont]
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][2])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][5])
st.image(st.session_state['latestImages'][gallery_start_idx:gallery_end_idx][8])
st.session_state['historyTab'] = [history_tab,col1,col2,col3,placeholder,col1_cont,col2_cont,col3_cont]
with discover_tabs:
st.markdown("<h1 style='text-align: center; color: white;'>Soon :)</h1>", unsafe_allow_html=True)
#display the images
#add a button to the gallery
#st.markdown("<h2 style='text-align: center; color: white;'>Try it out</h2>", unsafe_allow_html=True)
@ -140,3 +223,4 @@ def layout():
#if st.button("Try it out"):
#if the button is clicked, go to the gallery
#st.experimental_rerun()

View File

@ -30,11 +30,11 @@ except:
pass
class plugin_info():
plugname = "txt2img"
description = "Text to Image"
isTab = True
displayPriority = 1
plugname = "txt2img"
description = "Text to Image"
isTab = True
displayPriority = 1
if os.path.exists(os.path.join(st.session_state['defaults'].general.GFPGAN_dir, "experiments", "pretrained_models", "GFPGANv1.3.pth")):
GFPGAN_available = True
@ -93,14 +93,14 @@ def txt2img(prompt: str, ddim_steps: int, sampler_name: str, realesrgan_model_na
def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name):
samples_ddim, _ = sampler.sample(S=ddim_steps, conditioning=conditioning, batch_size=int(x.shape[0]), shape=x[0].shape, verbose=False, unconditional_guidance_scale=cfg_scale,
unconditional_conditioning=unconditional_conditioning, eta=ddim_eta, x_T=x, img_callback=generation_callback,
log_every_t=int(st.session_state.update_preview_frequency))
unconditional_conditioning=unconditional_conditioning, eta=ddim_eta, x_T=x, img_callback=generation_callback,
log_every_t=int(st.session_state.update_preview_frequency))
return samples_ddim
#try:
output_images, seed, info, stats = process_images(
outpath=outpath,
outpath=outpath,
func_init=init,
func_sample=sample,
prompt=prompt,
@ -126,7 +126,7 @@ def txt2img(prompt: str, ddim_steps: int, sampler_name: str, realesrgan_model_na
jpg_sample=save_as_jpg,
variant_amount=variant_amount,
variant_seed=variant_seed,
)
)
del sampler
@ -143,7 +143,7 @@ def layout():
st.session_state["generation_mode"] = "txt2img"
input_col1, generate_col1 = st.columns([10,1])
with input_col1:
#prompt = st.text_area("Input Text","")
prompt = st.text_input("Input Text","", placeholder="A corgi wearing a top hat as an oil painting.")
@ -179,12 +179,12 @@ def layout():
with st.expander("Preview Settings"):
st.session_state["update_preview"] = st.checkbox("Update Image Preview", value=st.session_state['defaults'].txt2img.update_preview,
help="If enabled the image preview will be updated during the generation instead of at the end. \
help="If enabled the image preview will be updated during the generation instead of at the end. \
You can use the Update Preview \Frequency option bellow to customize how frequent it's updated. \
By default this is enabled and the frequency is set to 1 step.")
st.session_state["update_preview_frequency"] = st.text_input("Update Image Preview Frequency", value=st.session_state['defaults'].txt2img.update_preview_frequency,
help="Frequency in steps at which the the preview image is updated. By default the frequency \
help="Frequency in steps at which the the preview image is updated. By default the frequency \
is set to 1 step.")
with col2:
@ -206,20 +206,20 @@ def layout():
st.session_state["progress_bar"] = st.empty()
message = st.empty()
with col3:
# If we have custom models available on the "models/custom"
#folder then we show a menu to select which model we want to use, otherwise we use the main model for SD
if st.session_state["CustomModel_available"]:
st.session_state["custom_model"] = st.selectbox("Custom Model:", st.session_state["custom_models"],
index=st.session_state["custom_models"].index(st.session_state['defaults'].general.default_model),
help="Select the model you want to use. This option is only available if you have custom models \
if st.session_state.CustomModel_available:
st.session_state.custom_model = st.selectbox("Custom Model:", st.session_state.custom_models,
index=st.session_state["custom_models"].index(st.session_state['defaults'].general.default_model),
help="Select the model you want to use. This option is only available if you have custom models \
on your 'models/custom' folder. The model name that will be shown here is the same as the name\
the file for the model has on said folder, it is recommended to give the .ckpt file a name that \
will make it easier for you to distinguish it from other models. Default: Stable Diffusion v1.4")
st.session_state.sampling_steps = st.slider("Sampling Steps", value=st.session_state['defaults'].txt2img.sampling_steps, min_value=1, max_value=250)
sampler_name_list = ["k_lms", "k_euler", "k_euler_a", "k_dpm_2", "k_dpm_2_a", "k_heun", "PLMS", "DDIM"]
sampler_name = st.selectbox("Sampling method", sampler_name_list,
index=sampler_name_list.index(st.session_state['defaults'].txt2img.default_sampler), help="Sampling method to use. Default: k_euler")
@ -238,7 +238,7 @@ def layout():
save_individual_images = st.checkbox("Save individual images.", value=True, help="Save each image generated before any filter or enhancement is applied.")
save_grid = st.checkbox("Save grid",value=True, help="Save a grid with all the images generated into a single image.")
group_by_prompt = st.checkbox("Group results by prompt", value=True,
help="Saves all the images with the same prompt into the same folder. When using a prompt matrix each prompt combination will have its own folder.")
help="Saves all the images with the same prompt into the same folder. When using a prompt matrix each prompt combination will have its own folder.")
write_info_files = st.checkbox("Write Info file", value=True, help="Save a file next to the image with informartion about the generation.")
save_as_jpg = st.checkbox("Save samples as jpg", value=False, help="Saves the images as jpg instead of png.")
@ -247,10 +247,10 @@ def layout():
This greatly improve the quality and consistency of faces but uses extra VRAM. Disable if you need the extra VRAM.")
else:
st.session_state["use_GFPGAN"] = False
if st.session_state["RealESRGAN_available"]:
st.session_state["use_RealESRGAN"] = st.checkbox("Use RealESRGAN", value=st.session_state['defaults'].txt2img.use_RealESRGAN,
help="Uses the RealESRGAN model to upscale the images after the generation.\
help="Uses the RealESRGAN model to upscale the images after the generation.\
This greatly improve the quality and lets you have high resolution images but uses extra VRAM. Disable if you need the extra VRAM.")
st.session_state["RealESRGAN_model"] = st.selectbox("RealESRGAN model", ["RealESRGAN_x4plus", "RealESRGAN_x4plus_anime_6B"], index=0)
else:
@ -265,18 +265,18 @@ def layout():
#print("Loading models")
# load the models when we hit the generate button for the first time, it wont be loaded after that so dont worry.
load_models(False, st.session_state["use_GFPGAN"], st.session_state["use_RealESRGAN"], st.session_state["RealESRGAN_model"], st.session_state["CustomModel_available"],
st.session_state["custom_model"])
st.session_state["custom_model"])
try:
output_images, seeds, info, stats = txt2img(prompt, st.session_state.sampling_steps, sampler_name, st.session_state["RealESRGAN_model"], batch_count, batch_size,
cfg_scale, seed, height, width, separate_prompts, normalize_prompt_weights, save_individual_images,
cfg_scale, seed, height, width, separate_prompts, normalize_prompt_weights, save_individual_images,
save_grid, group_by_prompt, save_as_jpg, st.session_state["use_GFPGAN"], st.session_state["use_RealESRGAN"], st.session_state["RealESRGAN_model"],
fp=st.session_state.defaults.general.fp, variant_amount=variant_amount, variant_seed=variant_seed, write_info_files=write_info_files)
fp=st.session_state.defaults.general.fp, variant_amount=variant_amount, variant_seed=variant_seed, write_info_files=write_info_files)
message.success('Render Complete: ' + info + '; Stats: ' + stats, icon="")
history_tab,col1,col2,col3,PlaceHolder,col1_cont,col2_cont,col3_cont = st.session_state['historyTab']
if 'latestImages' in st.session_state:
for i in output_images:
#push the new image to the list of latest images and remove the oldest one
@ -307,61 +307,62 @@ def layout():
st.image(st.session_state['latestImages'][8])
historyGallery = st.empty()
# check if output_images length is the same as seeds length
with gallery_tab:
st.markdown(createHTMLGallery(output_images,seeds), unsafe_allow_html=True)
# check if output_images length is the same as seeds length
with gallery_tab:
st.markdown(createHTMLGallery(output_images,seeds), unsafe_allow_html=True)
st.session_state['historyTab'] = [history_tab,col1,col2,col3,PlaceHolder,col1_cont,col2_cont,col3_cont]
st.session_state['historyTab'] = [history_tab,col1,col2,col3,PlaceHolder,col1_cont,col2_cont,col3_cont]
except (StopException, KeyError):
print(f"Received Streamlit StopException")
# this will render all the images at the end of the generation but its better if its moved to a second tab inside col2 and shown as a gallery.
# use the current col2 first tab to show the preview_img and update it as its generated.
#preview_image.image(output_images)
# this will render all the images at the end of the generation but its better if its moved to a second tab inside col2 and shown as a gallery.
# use the current col2 first tab to show the preview_img and update it as its generated.
#preview_image.image(output_images)
#on import run init
def createHTMLGallery(images,info):
html3 = """
html3 = """
<div class="gallery-history" style="
display: flex;
flex-wrap: wrap;
align-items: flex-start;">
"""
mkdwn_array = []
for i in images:
try:
seed = info[images.index(i)]
except:
seed = ' '
image_io = BytesIO()
i.save(image_io, 'PNG')
width, height = i.size
#get random number for the id
image_id = "%s" % (str(images.index(i)))
(data, mimetype) = STImage._normalize_to_bytes(image_io.getvalue(), width, 'auto')
this_file = in_memory_file_manager.add(data, mimetype, image_id)
img_str = this_file.url
#img_str = 'data:image/png;base64,' + b64encode(image_io.getvalue()).decode('ascii')
#get image size
#make sure the image is not bigger then 150px but keep the aspect ratio
if width > 150:
height = int(height * (150/width))
width = 150
if height > 150:
width = int(width * (150/height))
height = 150
mkdwn_array = []
for i in images:
try:
seed = info[images.index(i)]
except:
seed = ' '
image_io = BytesIO()
i.save(image_io, 'PNG')
width, height = i.size
#get random number for the id
image_id = "%s" % (str(images.index(i)))
(data, mimetype) = STImage._normalize_to_bytes(image_io.getvalue(), width, 'auto')
this_file = in_memory_file_manager.add(data, mimetype, image_id)
img_str = this_file.url
#img_str = 'data:image/png;base64,' + b64encode(image_io.getvalue()).decode('ascii')
#get image size
#mkdwn = f"""<img src="{img_str}" alt="Image" with="200" height="200" />"""
mkdwn = f'''<div class="gallery" style="margin: 3px;" >
<a href="{img_str}">
<img src="{img_str}" alt="Image" width="{width}" height="{height}">
</a>
<div class="desc" style="text-align: center; opacity: 40%;">{seed}</div>
#make sure the image is not bigger then 150px but keep the aspect ratio
if width > 150:
height = int(height * (150/width))
width = 150
if height > 150:
width = int(width * (150/height))
height = 150
#mkdwn = f"""<img src="{img_str}" alt="Image" with="200" height="200" />"""
mkdwn = f'''<div class="gallery" style="margin: 3px;" >
<a href="{img_str}">
<img src="{img_str}" alt="Image" width="{width}" height="{height}">
</a>
<div class="desc" style="text-align: center; opacity: 40%;">{seed}</div>
</div>
'''
mkdwn_array.append(mkdwn)
html3 += "".join(mkdwn_array)
html3 += '</div>'
return html3
mkdwn_array.append(mkdwn)
html3 += "".join(mkdwn_array)
html3 += '</div>'
return html3

View File

@ -481,7 +481,7 @@ def txt2vid(
Took { round(time_diff, 2) }s total ({ round(time_diff/(max_frames),2) }s per image)
Peak memory usage: { -(mem_max_used // -1_048_576) } MiB / { -(mem_total // -1_048_576) } MiB / { round(mem_max_used/mem_total*100, 3) }%'''
return im, seeds, info, stats
return video_path, seeds, info, stats
#on import run init
def createHTMLGallery(images,info):
@ -679,7 +679,7 @@ def layout():
#load_models(False, False, False, st.session_state["RealESRGAN_model"], CustomModel_available=st.session_state["CustomModel_available"], custom_model=custom_model)
# run video generation
image, seed, info, stats = txt2vid(prompts=prompt, gpu=st.session_state["defaults"].general.gpu,
video, seed, info, stats = txt2vid(prompts=prompt, gpu=st.session_state["defaults"].general.gpu,
num_steps=st.session_state.sampling_steps, max_frames=int(st.session_state.max_frames),
num_inference_steps=st.session_state.num_inference_steps,
cfg_scale=cfg_scale,do_loop=st.session_state["do_loop"],
@ -690,12 +690,49 @@ def layout():
#message.success('Done!', icon="✅")
message.success('Render Complete: ' + info + '; Stats: ' + stats, icon="")
history_tab,col1,col2,col3,PlaceHolder,col1_cont,col2_cont,col3_cont = st.session_state['historyTab']
#if 'latestVideos' in st.session_state:
#for i in video:
##push the new image to the list of latest images and remove the oldest one
##remove the last index from the list\
#st.session_state['latestVideos'].pop()
##add the new image to the start of the list
#st.session_state['latestVideos'].insert(0, i)
#PlaceHolder.empty()
#with PlaceHolder.container():
#col1, col2, col3 = st.columns(3)
#col1_cont = st.container()
#col2_cont = st.container()
#col3_cont = st.container()
#with col1_cont:
#with col1:
#st.image(st.session_state['latestVideos'][0])
#st.image(st.session_state['latestVideos'][3])
#st.image(st.session_state['latestVideos'][6])
#with col2_cont:
#with col2:
#st.image(st.session_state['latestVideos'][1])
#st.image(st.session_state['latestVideos'][4])
#st.image(st.session_state['latestVideos'][7])
#with col3_cont:
#with col3:
#st.image(st.session_state['latestVideos'][2])
#st.image(st.session_state['latestVideos'][5])
#st.image(st.session_state['latestVideos'][8])
#historyGallery = st.empty()
## check if output_images length is the same as seeds length
#with gallery_tab:
#st.markdown(createHTMLGallery(video,seed), unsafe_allow_html=True)
#st.session_state['historyTab'] = [history_tab,col1,col2,col3,PlaceHolder,col1_cont,col2_cont,col3_cont]
#except (StopException, KeyError):
#print(f"Received Streamlit StopException")
# this will render all the images at the end of the generation but its better if its moved to a second tab inside col2 and shown as a gallery.
# use the current col2 first tab to show the preview_img and update it as its generated.
#preview_image.image(output_images)

View File

@ -105,8 +105,12 @@ def layout():
iconName=['dashboard','model_training' ,'cloud_download', 'settings'], default_choice=0)
if tabs =='Stable Diffusion':
txt2img_tab, img2img_tab, txt2vid_tab, postprocessing_tab = st.tabs(["Text-to-Image Unified", "Image-to-Image Unified",
"Text-to-Video","Post-Processing"])
home_tab, txt2img_tab, img2img_tab, txt2vid_tab, postprocessing_tab = st.tabs(["Home","Text-to-Image Unified", "Image-to-Image Unified",
"Text-to-Video","Post-Processing"])
with home_tab:
from home import layout
layout()
with txt2img_tab:
from txt2img import layout
layout()