WorldlyLabs commited on
Commit
dbe3f9b
1 Parent(s): c4b197e

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +159 -0
README.md ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+
5
+ # worldly-v1: Bias Mitigation Script for Image Generation
6
+
7
+ ## Overview
8
+
9
+ **worldly-v1** is a bias mitigation script designed to modify prompts before sending them to an image generation model. It introduces diverse ethnicities and other demographic characteristics into prompts that contain vague references to "people," "person," or related terms, helping ensure more equitable representation in generated images. This version is specifically demonstrated with the **FluxPipeline** model, but it can be used with any image generation model that accepts text-based prompts.
10
+
11
+ ## Purpose
12
+
13
+ The goal of **worldly-v1** is to mitigate bias in AI-generated imagery by diversifying the representations of people in prompts. This script dynamically modifies prompts by injecting randomly selected ethnicities or demographic details, ensuring equal chances of different ethnicities being represented in the generated images.
14
+
15
+ ## How It Works
16
+
17
+ - The script targets terms like "person," "people," "man," "woman," "child," "boy," "girl," and their plurals.
18
+ - It replaces these terms with a randomly selected ethnicity or demographic detail based on a detailed list of major ethnic and racial groups.
19
+ - The modified prompt is then passed to the image generation model to create more diverse and inclusive images.
20
+
21
+ ## Installation and Setup
22
+
23
+ ### Requirements
24
+
25
+ Make sure you have the following Python libraries installed:
26
+
27
+ ```bash
28
+ pip install torch diffusers huggingface_hub Pillow
29
+ ```
30
+
31
+ ### How to Use
32
+
33
+ 1. **Download the Script**
34
+
35
+ You can download and integrate the **worldly-v1** script into your image generation pipeline. Use the `huggingface_hub` library to fetch the script:
36
+
37
+ ```python
38
+ from huggingface_hub import hf_hub_download
39
+ import importlib.util
40
+
41
+ repo_id = "WorldlyLabs/worldly"
42
+ filename = "worldly-v1.py"
43
+ script_path = hf_hub_download(repo_id=repo_id, filename=filename)
44
+
45
+ # Load the script dynamically
46
+ spec = importlib.util.spec_from_file_location("worldly-v1", script_path)
47
+ worldly_v1 = importlib.util.module_from_spec(spec)
48
+ spec.loader.exec_module(worldly_v1)
49
+ ```
50
+
51
+ 2. **Integrating into Image Generation**
52
+
53
+ Once downloaded, you can use **worldly-v1** to modify prompts before image generation. Below is an example of how to integrate the script into an image generation pipeline using the **FluxPipeline** model from Diffusers.
54
+
55
+ ### Example Script
56
+
57
+ ```python
58
+ import os
59
+ import torch
60
+ import gc
61
+ import logging
62
+ from huggingface_hub import hf_hub_download
63
+ import importlib.util
64
+ from diffusers import FluxPipeline
65
+
66
+ # Set up logging to print to console
67
+ logging.basicConfig(
68
+ level=logging.INFO,
69
+ format="%(asctime)s - %(levelname)s - %(message)s"
70
+ )
71
+
72
+ # Download the worldly-v1 script from Hugging Face
73
+ repo_id = "WorldlyLabs/worldly"
74
+ filename = "worldly-v1.py"
75
+ script_path = hf_hub_download(repo_id=repo_id, filename=filename)
76
+
77
+ # Load the worldly-v1 script dynamically
78
+ spec = importlib.util.spec_from_file_location("worldly-v1", script_path)
79
+ worldly_v1 = importlib.util.module_from_spec(spec)
80
+ spec.loader.exec_module(worldly_v1)
81
+
82
+ # List of example prompts
83
+ prompts = {
84
+ "Sample 1": "A person standing in a forest, looking at the sky.",
85
+ "Sample 2": "A group of people walking in a desert, wearing traditional clothing.",
86
+ "Sample 3": "A child holding a kite on a beach, with waves crashing nearby."
87
+ }
88
+
89
+ # Apply the worldly-v1 script to all prompts before image generation
90
+ def modify_all_prompts(prompts):
91
+ modified_prompts = {}
92
+ for prompt_name, prompt in prompts.items():
93
+ modified_prompt = worldly_v1.modify_prompt(prompt)
94
+ logging.info(f"Original prompt for {prompt_name}: {prompt}")
95
+ logging.info(f"Modified prompt for {prompt_name}: {modified_prompt}")
96
+ modified_prompts[prompt_name] = modified_prompt
97
+ return modified_prompts
98
+
99
+ # Function to generate images for each prompt
100
+ def generate_images():
101
+ try:
102
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
103
+ weight_dtype = torch.bfloat16
104
+
105
+ # Load the FLUX pipeline
106
+ print("Loading the FLUX pipeline...")
107
+ pipe = FluxPipeline.from_pretrained(
108
+ "black-forest-labs/FLUX.1-dev",
109
+ torch_dtype=weight_dtype,
110
+ )
111
+
112
+ pipe.to(device)
113
+ pipe.enable_model_cpu_offload()
114
+
115
+ # Modify all prompts before generating images
116
+ modified_prompts = modify_all_prompts(prompts)
117
+
118
+ # Create a folder for the generated images
119
+ output_folder = "./generated_images"
120
+ os.makedirs(output_folder, exist_ok=True)
121
+
122
+ # Generate and save each image based on the modified prompt
123
+ for prompt_name, prompt in modified_prompts.items():
124
+ print(f"Generating image for {prompt_name}")
125
+
126
+ # Generate the image
127
+ prompt_embeds, pooled_prompt_embeds, _ = pipe.encode_prompt(prompt=prompt)
128
+
129
+ image = pipe(
130
+ prompt_embeds=prompt_embeds,
131
+ pooled_prompt_embeds=pooled_prompt_embeds,
132
+ guidance_scale=3.5,
133
+ output_type="pil",
134
+ num_inference_steps=80,
135
+ max_sequence_length=256,
136
+ generator=torch.Generator("cpu").manual_seed(0),
137
+ height=1920,
138
+ width=1080
139
+ ).images[0]
140
+
141
+ # Save the generated image with the prompt name
142
+ output_path = os.path.join(output_folder, f"{prompt_name}.png")
143
+ image.save(output_path)
144
+ print(f"Image for {prompt_name} saved at: {output_path}")
145
+
146
+ # Clear CPU cache
147
+ gc.collect()
148
+
149
+ except Exception as e:
150
+ logging.error(f"Error during image generation: {str(e)}")
151
+
152
+ # Main execution block
153
+ if __name__ == "__main__":
154
+ generate_images()
155
+ ```
156
+
157
+ ## License
158
+
159
+ The **worldly-v1** script is licensed under the MIT License. You are free to use, modify, and distribute this script, as long as the original copyright and permission notice is retained.