felfri commited on
Commit
976932f
·
verified ·
1 Parent(s): 633ff14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -33
app.py CHANGED
@@ -7,34 +7,26 @@ import zipfile
7
 
8
  with zipfile.ZipFile("images/fair_diffusion.zip","r") as zip_ref:
9
  zip_ref.extractall("images/")
10
-
11
-
12
  with zipfile.ZipFile("images/stable_diffusion.zip","r") as zip_ref:
13
  zip_ref.extractall("images/")
14
-
15
-
16
 
17
  def open_stable_ims(profession):
18
- if not profession:
19
- return []
20
- dirname = 'images/stable_diffusion/'+ profession+'/'
21
- if not os.path.isdir(dirname):
22
- return []
23
- images = [Image.open(os.path.join(dirname+im)).convert("RGB") for im in os.listdir(dirname)]
24
- random.shuffle(images)
25
- return images[:16]
26
 
27
  def open_fair_ims(profession):
28
- if not profession:
29
- return []
30
- dirname = f'images/fair_diffusion/{profession}/'
31
- if not os.path.isdir(dirname):
32
- return []
33
- images = [Image.open(os.path.join(dirname+im)).convert("RGB") for im in os.listdir(dirname)]
34
- random.shuffle(images)
35
- return images[:16]
36
-
37
-
38
 
39
  professions = sorted(os.listdir('images/fair_diffusion'))
40
 
@@ -44,16 +36,15 @@ with gr.Blocks() as demo:
44
  with gr.Row():
45
  with gr.Column():
46
  gr.Markdown('## [Stable Diffusion v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5) Generations')
47
- choice1 = gr.Dropdown(professions, label = "Choose a profession", multiselect= False, interactive=True)
48
- images1 = gr.Gallery(label="Images", columns=4, height="auto")
49
  with gr.Column():
50
  gr.Markdown('## Fair Diffusion Generations')
51
- choice2 = gr.Dropdown(professions, label = "Choose a profession", multiselect = False, interactive=True)
52
- images2 = gr.Gallery(label="Images", columns=4, height="auto")
53
-
54
- gr.Markdown("We present a novel strategy, called **Fair Diffusion**, to attenuate biases after the deployment of generative text-to-image models. Specifically, we demonstrate shifting a bias, based on human instructions, in any direction yielding arbitrarily new proportions for, e.g., identity groups. As our empirical evaluation demonstrates, this introduced control enables instructing generative image models on fairness, with no data filtering and additional training required. For the full paper by Friedrich et al., see [here](https://arxiv.org/pdf/2302.10893.pdf).")
55
-
56
- choice1.change(open_stable_ims, inputs=choice1, outputs=images1)
57
- choice2.change(open_fair_ims, inputs=choice2, outputs=images2)
58
-
59
- demo.launch()
 
7
 
8
  with zipfile.ZipFile("images/fair_diffusion.zip","r") as zip_ref:
9
  zip_ref.extractall("images/")
 
 
10
  with zipfile.ZipFile("images/stable_diffusion.zip","r") as zip_ref:
11
  zip_ref.extractall("images/")
 
 
12
 
13
  def open_stable_ims(profession):
14
+ if profession and len(profession) != 0:
15
+ dirname = 'images/stable_diffusion/'+ profession+'/'
16
+ if os.path.exists(dirname):
17
+ images = [Image.open(os.path.join(dirname+im)).convert("RGB") for im in os.listdir(dirname)]
18
+ random.shuffle(images)
19
+ return images[:16]
20
+ return []
 
21
 
22
  def open_fair_ims(profession):
23
+ if profession and len(profession) != 0:
24
+ dirname = 'images/fair_diffusion/' + profession+'/'
25
+ if os.path.exists(dirname):
26
+ images = [Image.open(os.path.join(dirname+im)).convert("RGB") for im in os.listdir(dirname)]
27
+ random.shuffle(images)
28
+ return images[:16]
29
+ return []
 
 
 
30
 
31
  professions = sorted(os.listdir('images/fair_diffusion'))
32
 
 
36
  with gr.Row():
37
  with gr.Column():
38
  gr.Markdown('## [Stable Diffusion v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5) Generations')
39
+ choice1 = gr.Dropdown(professions, label="Choose a profession", multiselect=False, interactive=True)
40
+ images1 = gr.Gallery(label="Images")
41
  with gr.Column():
42
  gr.Markdown('## Fair Diffusion Generations')
43
+ choice2 = gr.Dropdown(professions, label="Choose a profession", multiselect=False, interactive=True)
44
+ images2 = gr.Gallery(label="Images")
45
+ gr.Markdown("We present a novel strategy, called **Fair Diffusion**, to attenuate biases after the deployment of generative text-to-image models. Specifically, we demonstrate shifting a bias, based on human instructions, in any direction yielding arbitrarily new proportions for, e.g., identity groups. As our empirical evaluation demonstrates, this introduced control enables instructing generative image models on fairness, with no data filtering and additional training required. For the full paper by Friedrich et al., see [here](https://arxiv.org/pdf/2302.10893.pdf).")
46
+
47
+ choice1.change(open_stable_ims, choice1, [images1])
48
+ choice2.change(open_fair_ims, choice2, [images2])
49
+
50
+ demo.launch()