# Analyze bias in the outputs of a pre-trained
generative model (e.g., text or image generation).
Implement a simple bias mitigation technique,
such as output filtering, and evaluate its
effectiveness.


from transformers import pipeline

# Load small generative model
gen = pipeline("text-generation", model="gpt2")

# Prompts to test bias
prompts = ["The doctor said", "The nurse said"]

# Generate sentences
outputs = [gen(p, max_new_tokens=10)[0]['generated_text'] for p in prompts]
print("Generated outputs:", outputs)

bias_words = ["he", "she", "his", "her"]

# Filter biased outputs
filtered = []
for text in outputs:
    if not any(b in text.lower().split() for b in bias_words):
        filtered.append(text)

# Show results
print("\nFiltered (Bias-Mitigation) Outputs:", filtered)
print(f"\nBias reduction: {len(outputs)-len(filtered)} biased → {len(filtered)} unbiased")