stable-bias/diffusion-bias-explorer
4
1import gradio as gr2import random, os, shutil3from PIL import Image4import pandas as pd5import tempfile6 7def open_sd_ims(adj, group, seed):8 if group != '':9 if adj != '':10 prompt=adj+'_'+group.replace(' ','_')11 if os.path.isdir(prompt) == False:12 shutil.unpack_archive('zipped_images/stablediffusion/'+ prompt.replace(' ', '_') +'.zip', prompt, 'zip')13 else:14 prompt=group15 if os.path.isdir(prompt) == False:16 shutil.unpack_archive('zipped_images/stablediffusion/'+ prompt.replace(' ', '_') +'.zip', prompt, 'zip')17 imnames= os.listdir(prompt+'/Seed_'+ str(seed)+'/')18 images = [(Image.open(prompt+'/Seed_'+ str(seed)+'/'+name)) for name in imnames]19 return images[:9]20 21def open_ims(model, adj, group):22 seed = 4804023 with tempfile.TemporaryDirectory() as tmpdirname:24 print('created temporary directory', tmpdirname)25 if model == "Dall-E 2":26 if group != '':27 if adj != '':28 prompt=adj+'_'+group.replace(' ','_')29 if os.path.isdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt) == False:30 shutil.unpack_archive('zipped_images/'+ model.replace(' ','').lower()+ '/'+ prompt.replace(' ', '_') +'.zip', tmpdirname+ '/'+ model.replace(' ','').lower()+ '/'+ prompt, 'zip')31 else:32 prompt=group33 if os.path.isdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt) == False:34 shutil.unpack_archive('zipped_images/' + model.replace(' ','').lower() + '/'+ prompt.replace(' ', '_') +'.zip', tmpdirname + '/' + model.replace(' ','').lower()+ '/' + prompt, 'zip')35 imnames= os.listdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt+'/')36 images = [(Image.open(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt+'/'+name)).convert("RGB") for name in imnames]37 return images[:9]38 39 else:40 if group != '':41 if adj != '':42 prompt=adj+'_'+group.replace(' ','_')43 if os.path.isdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt) == False:44 shutil.unpack_archive('zipped_images/'+ model.replace(' ','').lower()+ '/'+ prompt.replace(' ', '_') +'.zip', tmpdirname + '/' +model.replace(' ','').lower()+ '/'+ prompt, 'zip')45 else:46 prompt=group47 if os.path.isdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt) == False:48 shutil.unpack_archive('zipped_images/' + model.replace(' ','').lower() + '/'+ prompt.replace(' ', '_') +'.zip', tmpdirname + '/' + model.replace(' ','').lower()+'/'+ prompt, 'zip')49 imnames= os.listdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt+'/'+'Seed_'+ str(seed)+'/')50 images = [(Image.open(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt +'/'+'Seed_'+ str(seed)+'/'+name)) for name in imnames]51 return images[:9]52 53 54vowels = ["a","e","i","o","u"]55prompts = pd.read_csv('promptsadjectives.csv')56 57seeds = [46267, 48040, 51237, 54325, 60884, 64830, 67031, 72935, 92118, 93109]58 59m_adjectives = prompts['Masc-adj'].tolist()[:10]60f_adjectives = prompts['Fem-adj'].tolist()[:10]61adjectives = sorted(m_adjectives+f_adjectives)62#adjectives = ['attractive','strong']63adjectives.insert(0, '')64professions = sorted([p.lower() for p in prompts['Occupation-Noun'].tolist()])65models = ["Stable Diffusion 1.4", "Dall-E 2","Stable Diffusion 2"]66 67with gr.Blocks() as demo:68 gr.Markdown("# Diffusion Bias Explorer")69 gr.Markdown("## Choose from the prompts below to explore how the text-to-image models like [Stable Diffusion v1.4](https://huggingface.co/CompVis/stable-diffusion-v-1-4-original), [Stable Diffion v.2](https://huggingface.co/stabilityai/stable-diffusion-2) and [DALLE-2](https://openai.com/dall-e-2/) represent different professions and adjectives")70 gr.HTML("""<span style="color:red">⚠️ <b>DISCLAIMER: the images displayed by this tool were generated by text-to-image models and may depict offensive stereotypes or contain explicit content.</b></span>""")71# gr.Markdown("Some of the images for Dall-E 2 are missing -- we are still in the process of generating them! If you get an 'error', please pick another prompt.")72# seed_choice = gr.State(0)73# seed_choice = 9310974# print("Seed choice is: " + str(seed_choice))75 with gr.Row():76 with gr.Column():77 model1 = gr.Dropdown(models, label = "Choose a model to compare results", value = models[0], interactive=True)78 adj1 = gr.Dropdown(adjectives, label = "Choose a first adjective (or leave this blank!)", interactive=True)79 choice1 = gr.Dropdown(professions, label = "Choose a first group", interactive=True)80# seed1= gr.Dropdown(seeds, label = "Choose a random seed to compare results", value = seeds[1], interactive=True)81 images1 = gr.Gallery(label="Images").style(grid=[3], height="auto")82 with gr.Column():83 model2 = gr.Dropdown(models, label = "Choose a model to compare results", value = models[0], interactive=True) 84 adj2 = gr.Dropdown(adjectives, label = "Choose a second adjective (or leave this blank!)", interactive=True)85 choice2 = gr.Dropdown(professions, label = "Choose a second group", interactive=True)86# seed2= gr.Dropdown(seeds, label = "Choose a random seed to compare results", value= seeds[1], interactive=True)87 images2 = gr.Gallery(label="Images").style(grid=[3], height="auto")88 89 gr.Markdown("### [Research](http://gender-decoder.katmatfield.com/static/documents/Gaucher-Friesen-Kay-JPSP-Gendered-Wording-in-Job-ads.pdf) has shown that \90 certain words are considered more masculine- or feminine-coded based on how appealing job descriptions containing these words \91 seemed to male and female research participants and to what extent the participants felt that they 'belonged' in that occupation.")92 93 94 #demo.load(random_image, None, [images])95 choice1.change(open_ims, [model1, adj1,choice1], [images1])96 choice2.change(open_ims, [model2, adj2,choice2], [images2])97 adj1.change(open_ims, [model1, adj1, choice1], [images1])98 adj2.change(open_ims, [model2, adj2, choice2], [images2])99# seed1.change(open_ims, [adj1,choice1,seed1], [images1])100# seed2.change(open_ims, [adj2,choice2,seed2], [images2])101 102demo.launch()103 