CoolFace
Apppublic

society-ethics/DiffusionBiasExplorer

sourceHugging Facecc-by-sa-4.0updated 1y agoView on Hugging Face
60likes
app.py105 linesDownload Raw Back to root
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+'_'+str(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+'_'+str(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        # Once a subgroup as been selected....41        if group not in ('', None, []):42            # If an adjective is selected, generate the adj + subgroup.43            if adj not in ('', None, []):44                prompt=adj+'_'+str(group).replace(' ','_')45                if os.path.isdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt) == False:46                    shutil.unpack_archive('zipped_images/'+ model.replace(' ','').lower()+ '/'+ prompt.replace(' ', '_') +'.zip',  tmpdirname + '/' +model.replace(' ','').lower()+ '/'+ prompt, 'zip')47            # Otherwise, just the subgroup.48            else:49                prompt=group50                if os.path.isdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt) == False:51                    shutil.unpack_archive('zipped_images/' + model.replace(' ','').lower() +  '/'+  prompt.replace(' ', '_') +'.zip', tmpdirname + '/' + model.replace(' ','').lower()+'/'+ prompt, 'zip')52            imnames= os.listdir(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt+'/'+'Seed_'+ str(seed)+'/')53            images = [(Image.open(tmpdirname + '/' + model.replace(' ','').lower()+ '/'+ prompt +'/'+'Seed_'+ str(seed)+'/'+name)) for name in imnames]54            return images[:9]55 56 57vowels = ["a","e","i","o","u"]58prompts = pd.read_csv('promptsadjectives.csv')59 60seeds = [46267, 48040, 51237, 54325, 60884, 64830, 67031, 72935, 92118, 93109]61 62m_adjectives = prompts['Masc-adj'].tolist()[:10]63f_adjectives = prompts['Fem-adj'].tolist()[:10]64adjectives = sorted(m_adjectives+f_adjectives)65#adjectives = ['attractive','strong']66adjectives.insert(0, '')67professions = sorted([p.lower() for p in prompts['Occupation-Noun'].tolist()])68models = ["Stable Diffusion 1.4", "Dall-E 2","Stable Diffusion 2"]69 70with gr.Blocks() as demo:71    gr.Markdown("# Diffusion Bias Explorer")72    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 Diffusion v.2](https://huggingface.co/stabilityai/stable-diffusion-2) and [DALLE-2](https://openai.com/dall-e-2/) represent different professions and adjectives")73#    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.")74#    seed_choice = gr.State(0)75#    seed_choice = 9310976#    print("Seed choice is: " + str(seed_choice))77    with gr.Row():78        with gr.Column():79            model1 = gr.Dropdown(models, label = "Choose a model to compare results", value = models[0], interactive=True)80            adj1 = gr.Dropdown(adjectives, label = "Choose a first adjective (or leave this blank!)", interactive=True)81            choice1 = gr.Dropdown(professions, label = "Choose a first group", interactive=True)82#           seed1= gr.Dropdown(seeds, label = "Choose a random seed to compare results", value = seeds[1], interactive=True)83            images1 = gr.Gallery(label="Images", columns=3, rows=3, height="auto") #.style(grid=[3], height="auto")84        with gr.Column():85            model2 = gr.Dropdown(models, label = "Choose a model to compare results", value = models[0], interactive=True)         86            adj2 = gr.Dropdown(adjectives, label = "Choose a second adjective (or leave this blank!)", interactive=True)87            choice2 = gr.Dropdown(professions, label = "Choose a second group", interactive=True)88#           seed2= gr.Dropdown(seeds, label = "Choose a random seed to compare results", value= seeds[1], interactive=True)89            images2 = gr.Gallery(label="Images", columns=3, rows=3, height="auto")#.style(grid=[3], height="auto")90 91    gr.Markdown("### [Research](http://gender-decoder.katmatfield.com/static/documents/Gaucher-Friesen-Kay-JPSP-Gendered-Wording-in-Job-ads.pdf) has shown that \92                    certain words are considered more masculine- or feminine-coded based on how appealing job descriptions containing these words \93                    seemed to male and female research participants and to what extent the participants felt that they 'belonged' in that occupation.")94 95 96    #demo.load(random_image, None, [images])97    choice1.change(open_ims, [model1, adj1,choice1], [images1])98    choice2.change(open_ims, [model2, adj2,choice2], [images2])99    adj1.change(open_ims, [model1, adj1, choice1], [images1])100    adj2.change(open_ims, [model2, adj2, choice2], [images2])101#    seed1.change(open_ims, [adj1,choice1,seed1], [images1])102#    seed2.change(open_ims, [adj2,choice2,seed2], [images2])103 104demo.launch()105 
society-ethics/DiffusionBiasExplorer · CoolFace