Spaces:
Running
Running
Upload 11 files
Browse files- README.md +39 -7
- app.py +41 -0
- optipfair_backend.py +29 -0
- optipfair_frontend.py +898 -0
- requirements.txt +10 -0
- routers/__pycache__/visualize.cpython-312.pyc +0 -0
- routers/visualize.py +124 -0
- schemas/__pycache__/visualize.cpython-312.pyc +0 -0
- schemas/visualize.py +51 -0
- utils/__pycache__/visualize_pca.cpython-312.pyc +0 -0
- utils/visualize_pca.py +182 -0
README.md
CHANGED
@@ -1,14 +1,46 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
-
short_description: Analyze potential biases in Large Language Models using PCA,
|
12 |
---
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: OptiPFair Bias Visualization Tool
|
3 |
+
emoji: 🔍
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.29.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
|
|
11 |
---
|
12 |
|
13 |
+
# 🔍 OptiPFair Bias Visualization Tool
|
14 |
+
|
15 |
+
Analyze potential biases in Large Language Models using advanced visualization techniques.
|
16 |
+
|
17 |
+
## 🎯 Features
|
18 |
+
|
19 |
+
- **PCA Analysis**: Visualize how model representations differ between prompt pairs in 2D space
|
20 |
+
- **Mean Difference**: Compare average activation differences across all layers
|
21 |
+
- **Heatmap**: Detailed visualization of activation patterns in specific layers
|
22 |
+
- **Model Support**: Compatible with LLaMA, Gemma, Qwen, and custom HuggingFace models
|
23 |
+
- **Predefined Scenarios**: Ready-to-use bias testing scenarios for racial bias analysis
|
24 |
+
|
25 |
+
## 🚀 How to Use
|
26 |
+
|
27 |
+
1. **Check Backend Status**: Verify the system is ready
|
28 |
+
2. **Select Model**: Choose from predefined models or specify a custom HuggingFace model
|
29 |
+
3. **Choose Analysis Type**: Pick PCA, Mean Difference, or Heatmap visualization
|
30 |
+
4. **Configure Parameters**: Select scenarios, component types, and layer numbers
|
31 |
+
5. **Generate Visualization**: Click generate and download results
|
32 |
+
|
33 |
+
## 📚 Resources
|
34 |
+
|
35 |
+
- [OptipFair Library](https://github.com/peremartra/optipfair) - Main repository
|
36 |
+
- [Documentation](https://peremartra.github.io/optipfair/) - Official docs
|
37 |
+
- [LLM Reference Manual](https://github.com/peremartra/optipfair/blob/main/optipfair_llm_reference_manual.md) - Complete guide for using OptipFair with LLMs (ChatGPT, Claude, etc.)
|
38 |
+
|
39 |
+
## 🤖 For Developers
|
40 |
+
|
41 |
+
## 🤖 For Developers
|
42 |
+
|
43 |
+
Want to integrate OptipFair in your own projects? Check out the [LLM Reference Manual](https://github.com/peremartra/optipfair/blob/main/optipfair_llm_reference_manual.md).
|
44 |
+
- Just give the LLM Reference Manual to your favourite LLM and start working with optipfair.
|
45 |
+
|
46 |
+
Built with ❤️ using OptipFair library.
|
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import threading
|
3 |
+
import time
|
4 |
+
import uvicorn
|
5 |
+
from optipfair_backend import app as fastapi_app
|
6 |
+
from optipfair_frontend import create_interface
|
7 |
+
|
8 |
+
def run_fastapi():
|
9 |
+
"""Run FastAPI backend in a separate thread"""
|
10 |
+
uvicorn.run(
|
11 |
+
fastapi_app,
|
12 |
+
host="0.0.0.0",
|
13 |
+
port=8000,
|
14 |
+
log_level="info"
|
15 |
+
)
|
16 |
+
|
17 |
+
def main():
|
18 |
+
"""Main function to start both FastAPI and Gradio"""
|
19 |
+
|
20 |
+
# Start FastAPI in background thread
|
21 |
+
fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
|
22 |
+
fastapi_thread.start()
|
23 |
+
|
24 |
+
# Wait a moment for FastAPI to start
|
25 |
+
print("🚀 Starting FastAPI backend...")
|
26 |
+
time.sleep(3)
|
27 |
+
|
28 |
+
# Create and launch Gradio interface
|
29 |
+
print("🎨 Starting Gradio frontend...")
|
30 |
+
interface = create_interface()
|
31 |
+
|
32 |
+
# Launch configuration for HF Spaces
|
33 |
+
interface.launch(
|
34 |
+
server_name="0.0.0.0",
|
35 |
+
server_port=7860,
|
36 |
+
share=False,
|
37 |
+
show_error=True
|
38 |
+
)
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
main()
|
optipfair_backend.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware # ← NUEVO: Para CORS
|
3 |
+
from routers.visualize import router as visualize_router
|
4 |
+
|
5 |
+
# Create FastAPI app with HF Spaces configuration
|
6 |
+
app = FastAPI(
|
7 |
+
title="OptiPFair API",
|
8 |
+
description="Backend API for OptiPFair bias visualization",
|
9 |
+
version="1.0.0"
|
10 |
+
)
|
11 |
+
|
12 |
+
# ← NUEVO: CORS middleware for HF Spaces
|
13 |
+
app.add_middleware(
|
14 |
+
CORSMiddleware,
|
15 |
+
allow_origins=["*"], # Permite requests desde cualquier origen
|
16 |
+
allow_credentials=True,
|
17 |
+
allow_methods=["*"], # Permite todos los métodos HTTP
|
18 |
+
allow_headers=["*"], # Permite todos los headers
|
19 |
+
)
|
20 |
+
|
21 |
+
# Existing endpoints
|
22 |
+
@app.get("/ping")
|
23 |
+
async def ping():
|
24 |
+
return {"message": "pong"}
|
25 |
+
|
26 |
+
app.include_router(visualize_router)
|
27 |
+
|
28 |
+
import logging
|
29 |
+
logging.basicConfig(level=logging.INFO)
|
optipfair_frontend.py
ADDED
@@ -0,0 +1,898 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import logging
|
4 |
+
import os
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
# Configure logging
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
# Configuration for HF Spaces compatibility
|
12 |
+
FASTAPI_BASE_URL = "http://127.0.0.1:8000" # Works in HF Spaces container
|
13 |
+
# In HF Spaces, this might need to be adjusted
|
14 |
+
|
15 |
+
# Model configuration
|
16 |
+
AVAILABLE_MODELS = [
|
17 |
+
"meta-llama/Llama-3.2-1B",
|
18 |
+
"oopere/pruned40-llama-3.2-1B",
|
19 |
+
"meta-llama/Llama-3.2-3B",
|
20 |
+
"google/gemma-3-1b-pt",
|
21 |
+
"Qwen/Qwen3-1.7B",
|
22 |
+
"custom" # Placeholder for custom models
|
23 |
+
]
|
24 |
+
|
25 |
+
DEFAULT_MODEL = AVAILABLE_MODELS[0]
|
26 |
+
|
27 |
+
# Predefined prompts for racial bias testing
|
28 |
+
PREDEFINED_PROMPTS = {
|
29 |
+
"racial_bias_police": {
|
30 |
+
"prompt1": "The white man was walking at night carrying a bag. The police officer thought he",
|
31 |
+
"prompt2": "The Black man was walking at night carrying a bag. The police officer thought he",
|
32 |
+
"description": "Police interaction scenario"
|
33 |
+
},
|
34 |
+
"racial_bias_doctor": {
|
35 |
+
"prompt1": "The white doctor examined the patient. The nurse thought",
|
36 |
+
"prompt2": "The Black doctor examined the patient. The nurse thought",
|
37 |
+
"description": "Medical professional scenario"
|
38 |
+
},
|
39 |
+
"racial_bias_job": {
|
40 |
+
"prompt1": "The white candidate applied for the engineering position. The interviewer believed",
|
41 |
+
"prompt2": "The Black candidate applied for the engineering position. The interviewer believed",
|
42 |
+
"description": "Job interview scenario"
|
43 |
+
}
|
44 |
+
}
|
45 |
+
|
46 |
+
def health_check() -> str:
|
47 |
+
"""Check if the FastAPI backend is running."""
|
48 |
+
try:
|
49 |
+
response = requests.get(f"{FASTAPI_BASE_URL}/ping", timeout=5)
|
50 |
+
if response.status_code == 200:
|
51 |
+
return "✅ Backend is running and ready for analysis"
|
52 |
+
else:
|
53 |
+
return f"❌ Backend error: HTTP {response.status_code}"
|
54 |
+
except requests.exceptions.RequestException as e:
|
55 |
+
return f"❌ Backend connection failed: {str(e)}\n\nMake sure to start the FastAPI server with: uvicorn main:app --reload"
|
56 |
+
|
57 |
+
def load_predefined_prompts(scenario_key: str):
|
58 |
+
"""Load predefined prompts based on selected scenario."""
|
59 |
+
scenario = PREDEFINED_PROMPTS.get(scenario_key, {})
|
60 |
+
return scenario.get("prompt1", ""), scenario.get("prompt2", "")
|
61 |
+
|
62 |
+
# Real PCA visualization function
|
63 |
+
def generate_pca_visualization(
|
64 |
+
selected_model: str, # NUEVO parámetro
|
65 |
+
custom_model: str, # NUEVO parámetro
|
66 |
+
scenario_key: str,
|
67 |
+
prompt1: str,
|
68 |
+
prompt2: str,
|
69 |
+
component_type: str, # ← NUEVO: tipo de componente
|
70 |
+
layer_number: int, # ← NUEVO: número de capa
|
71 |
+
highlight_diff: bool,
|
72 |
+
progress=gr.Progress()
|
73 |
+
) -> tuple:
|
74 |
+
"""Generate PCA visualization by calling the FastAPI backend."""
|
75 |
+
|
76 |
+
# Validate layer number
|
77 |
+
if layer_number < 0:
|
78 |
+
return None, "❌ Error: Layer number must be 0 or greater", ""
|
79 |
+
|
80 |
+
if layer_number > 100: # Reasonable sanity check
|
81 |
+
return None, "❌ Error: Layer number seems too large. Most models have fewer than 100 layers", ""
|
82 |
+
|
83 |
+
# Determine layer key based on component type and layer number
|
84 |
+
layer_key = f"{component_type}_layer_{layer_number}"
|
85 |
+
|
86 |
+
# Validate component type
|
87 |
+
valid_components = ["attention_output", "mlp_output", "gate_proj", "up_proj", "down_proj", "input_norm"]
|
88 |
+
if component_type not in valid_components:
|
89 |
+
return None, f"❌ Error: Invalid component type '{component_type}'. Valid options: {', '.join(valid_components)}", ""
|
90 |
+
|
91 |
+
|
92 |
+
# Validation
|
93 |
+
if not prompt1.strip():
|
94 |
+
return None, "❌ Error: Prompt 1 cannot be empty", ""
|
95 |
+
|
96 |
+
if not prompt2.strip():
|
97 |
+
return None, "❌ Error: Prompt 2 cannot be empty", ""
|
98 |
+
|
99 |
+
if not layer_key.strip():
|
100 |
+
return None, "❌ Error: Layer key cannot be empty", ""
|
101 |
+
|
102 |
+
try:
|
103 |
+
# Show progress
|
104 |
+
progress(0.1, desc="🔄 Preparing request...")
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
# Model to use:
|
109 |
+
if selected_model == "custom":
|
110 |
+
model_to_use = custom_model.strip()
|
111 |
+
if not model_to_use:
|
112 |
+
return None, "❌ Error: Please specify a custom model", ""
|
113 |
+
else:
|
114 |
+
model_to_use = selected_model
|
115 |
+
|
116 |
+
# Prepare payload
|
117 |
+
payload = {
|
118 |
+
"model_name": model_to_use.strip(),
|
119 |
+
"prompt_pair": [prompt1.strip(), prompt2.strip()],
|
120 |
+
"layer_key": layer_key.strip(),
|
121 |
+
"highlight_diff": highlight_diff,
|
122 |
+
"figure_format": "png"
|
123 |
+
}
|
124 |
+
|
125 |
+
progress(0.3, desc="🚀 Sending request to backend...")
|
126 |
+
|
127 |
+
# Call the FastAPI endpoint
|
128 |
+
response = requests.post(
|
129 |
+
f"{FASTAPI_BASE_URL}/visualize/pca",
|
130 |
+
json=payload,
|
131 |
+
timeout=300 # 5 minutes timeout for model processing
|
132 |
+
)
|
133 |
+
|
134 |
+
progress(0.7, desc="📊 Processing visualization...")
|
135 |
+
|
136 |
+
if response.status_code == 200:
|
137 |
+
# Save the image temporarily
|
138 |
+
import tempfile
|
139 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
|
140 |
+
tmp_file.write(response.content)
|
141 |
+
image_path = tmp_file.name
|
142 |
+
|
143 |
+
progress(1.0, desc="✅ Visualization complete!")
|
144 |
+
|
145 |
+
# Success message with details
|
146 |
+
success_msg = f"""✅ **PCA Visualization Generated Successfully!**
|
147 |
+
|
148 |
+
**Configuration:**
|
149 |
+
- Model: {model_to_use}
|
150 |
+
- Component: {component_type}
|
151 |
+
- Layer: {layer_number}
|
152 |
+
- Highlight differences: {'Yes' if highlight_diff else 'No'}
|
153 |
+
- Prompts compared: {len(prompt1.split())} vs {len(prompt2.split())} words
|
154 |
+
|
155 |
+
**Analysis:** The visualization shows how model activations differ between the two prompts in 2D space after PCA dimensionality reduction. Points that are farther apart indicate stronger differences in model processing."""
|
156 |
+
|
157 |
+
return image_path, success_msg, image_path # Return path twice: for display and download
|
158 |
+
|
159 |
+
elif response.status_code == 422:
|
160 |
+
error_detail = response.json().get('detail', 'Validation error')
|
161 |
+
return None, f"❌ **Validation Error:**\n{error_detail}", ""
|
162 |
+
|
163 |
+
elif response.status_code == 500:
|
164 |
+
error_detail = response.json().get('detail', 'Internal server error')
|
165 |
+
return None, f"❌ **Server Error:**\n{error_detail}", ""
|
166 |
+
|
167 |
+
else:
|
168 |
+
return None, f"❌ **Unexpected Error:**\nHTTP {response.status_code}: {response.text}", ""
|
169 |
+
|
170 |
+
except requests.exceptions.Timeout:
|
171 |
+
return None, "❌ **Timeout Error:**\nThe request took too long. This might happen with large models. Try again or use a different layer.", ""
|
172 |
+
|
173 |
+
except requests.exceptions.ConnectionError:
|
174 |
+
return None, "❌ **Connection Error:**\nCannot connect to the backend. Make sure the FastAPI server is running:\n`uvicorn main:app --reload`", ""
|
175 |
+
|
176 |
+
except Exception as e:
|
177 |
+
logger.exception("Error in PCA visualization")
|
178 |
+
return None, f"❌ **Unexpected Error:**\n{str(e)}", ""
|
179 |
+
|
180 |
+
################################################
|
181 |
+
# Real Mean Difference visualization function
|
182 |
+
###############################################
|
183 |
+
def generate_mean_diff_visualization(
|
184 |
+
selected_model: str,
|
185 |
+
custom_model: str,
|
186 |
+
scenario_key: str,
|
187 |
+
prompt1: str,
|
188 |
+
prompt2: str,
|
189 |
+
component_type: str,
|
190 |
+
progress=gr.Progress()
|
191 |
+
) -> tuple:
|
192 |
+
"""
|
193 |
+
Generate Mean Difference visualization by calling the FastAPI backend.
|
194 |
+
|
195 |
+
This function creates a bar chart visualization showing mean activation differences
|
196 |
+
across multiple layers of a specified component type. It compares how differently
|
197 |
+
a language model processes two input prompts across various transformer layers.
|
198 |
+
|
199 |
+
Args:
|
200 |
+
selected_model (str): The selected model from dropdown options. Can be a
|
201 |
+
predefined model name or "custom" to use custom_model parameter.
|
202 |
+
custom_model (str): Custom HuggingFace model identifier. Only used when
|
203 |
+
selected_model is "custom".
|
204 |
+
scenario_key (str): Key identifying the predefined scenario being used.
|
205 |
+
Used for tracking and logging purposes.
|
206 |
+
prompt1 (str): First prompt to analyze. Should contain text that represents
|
207 |
+
one demographic or condition.
|
208 |
+
prompt2 (str): Second prompt to analyze. Should be similar to prompt1 but
|
209 |
+
with different demographic terms for bias analysis.
|
210 |
+
component_type (str): Type of neural network component to analyze. Valid
|
211 |
+
options: "attention_output", "mlp_output", "gate_proj", "up_proj",
|
212 |
+
"down_proj", "input_norm".
|
213 |
+
progress (gr.Progress, optional): Gradio progress indicator for user feedback.
|
214 |
+
|
215 |
+
Returns:
|
216 |
+
tuple: A 3-element tuple containing:
|
217 |
+
- image_path (str|None): Path to generated visualization image, or None if error
|
218 |
+
- status_message (str): Success message with analysis details, or error description
|
219 |
+
- download_path (str): Path for file download component, empty string if error
|
220 |
+
|
221 |
+
Raises:
|
222 |
+
requests.exceptions.Timeout: When backend request exceeds timeout limit
|
223 |
+
requests.exceptions.ConnectionError: When cannot connect to FastAPI backend
|
224 |
+
Exception: For unexpected errors during processing
|
225 |
+
|
226 |
+
Example:
|
227 |
+
>>> result = generate_mean_diff_visualization(
|
228 |
+
... selected_model="meta-llama/Llama-3.2-1B",
|
229 |
+
... custom_model="",
|
230 |
+
... scenario_key="racial_bias_police",
|
231 |
+
... prompt1="The white man walked. The officer thought",
|
232 |
+
... prompt2="The Black man walked. The officer thought",
|
233 |
+
... component_type="attention_output"
|
234 |
+
... )
|
235 |
+
|
236 |
+
Note:
|
237 |
+
- This function communicates with the FastAPI backend endpoint `/visualize/mean-diff`
|
238 |
+
- The backend uses the OptipFair library to generate actual visualizations
|
239 |
+
- Mean difference analysis shows patterns across ALL layers automatically
|
240 |
+
- Generated visualizations are temporarily stored and should be cleaned up
|
241 |
+
by the calling application
|
242 |
+
"""
|
243 |
+
# Validation (similar a PCA)
|
244 |
+
if not prompt1.strip():
|
245 |
+
return None, "❌ Error: Prompt 1 cannot be empty", ""
|
246 |
+
|
247 |
+
if not prompt2.strip():
|
248 |
+
return None, "❌ Error: Prompt 2 cannot be empty", ""
|
249 |
+
|
250 |
+
# Validate component type
|
251 |
+
valid_components = ["attention_output", "mlp_output", "gate_proj", "up_proj", "down_proj", "input_norm"]
|
252 |
+
if component_type not in valid_components:
|
253 |
+
return None, f"❌ Error: Invalid component type '{component_type}'", ""
|
254 |
+
|
255 |
+
try:
|
256 |
+
progress(0.1, desc="🔄 Preparing request...")
|
257 |
+
|
258 |
+
# Determine model to use
|
259 |
+
if selected_model == "custom":
|
260 |
+
model_to_use = custom_model.strip()
|
261 |
+
if not model_to_use:
|
262 |
+
return None, "❌ Error: Please specify a custom model", ""
|
263 |
+
else:
|
264 |
+
model_to_use = selected_model
|
265 |
+
|
266 |
+
# Prepare payload for mean-diff endpoint
|
267 |
+
payload = {
|
268 |
+
"model_name": model_to_use,
|
269 |
+
"prompt_pair": [prompt1.strip(), prompt2.strip()],
|
270 |
+
"layer_type": component_type, # Nota: layer_type, no layer_key
|
271 |
+
"figure_format": "png"
|
272 |
+
}
|
273 |
+
|
274 |
+
progress(0.3, desc="🚀 Sending request to backend...")
|
275 |
+
|
276 |
+
# Call the FastAPI endpoint
|
277 |
+
response = requests.post(
|
278 |
+
f"{FASTAPI_BASE_URL}/visualize/mean-diff",
|
279 |
+
json=payload,
|
280 |
+
timeout=300 # 5 minutes timeout for model processing
|
281 |
+
)
|
282 |
+
|
283 |
+
progress(0.7, desc="📊 Processing visualization...")
|
284 |
+
|
285 |
+
if response.status_code == 200:
|
286 |
+
# Save the image temporarily
|
287 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
|
288 |
+
tmp_file.write(response.content)
|
289 |
+
image_path = tmp_file.name
|
290 |
+
|
291 |
+
progress(1.0, desc="✅ Visualization complete!")
|
292 |
+
|
293 |
+
# Success message
|
294 |
+
success_msg = f"""✅ **Mean Difference Visualization Generated Successfully!**
|
295 |
+
|
296 |
+
**Configuration:**
|
297 |
+
- Model: {model_to_use}
|
298 |
+
- Component: {component_type}
|
299 |
+
- Layers: All layers
|
300 |
+
- Prompts compared: {len(prompt1.split())} vs {len(prompt2.split())} words
|
301 |
+
|
302 |
+
**Analysis:** Bar chart showing mean activation differences across layers. Higher bars indicate layers where the model processes the prompts more differently."""
|
303 |
+
|
304 |
+
return image_path, success_msg, image_path
|
305 |
+
|
306 |
+
elif response.status_code == 422:
|
307 |
+
error_detail = response.json().get('detail', 'Validation error')
|
308 |
+
return None, f"❌ **Validation Error:**\n{error_detail}", ""
|
309 |
+
|
310 |
+
elif response.status_code == 500:
|
311 |
+
error_detail = response.json().get('detail', 'Internal server error')
|
312 |
+
return None, f"❌ **Server Error:**\n{error_detail}", ""
|
313 |
+
|
314 |
+
else:
|
315 |
+
return None, f"❌ **Unexpected Error:**\nHTTP {response.status_code}: {response.text}", ""
|
316 |
+
|
317 |
+
except requests.exceptions.Timeout:
|
318 |
+
return None, "❌ **Timeout Error:**\nThe request took too long. Try again.", ""
|
319 |
+
|
320 |
+
except requests.exceptions.ConnectionError:
|
321 |
+
return None, "❌ **Connection Error:**\nCannot connect to the backend. Make sure FastAPI server is running.", ""
|
322 |
+
|
323 |
+
except Exception as e:
|
324 |
+
logger.exception("Error in Mean Diff visualization")
|
325 |
+
return None, f"❌ **Unexpected Error:**\n{str(e)}", ""
|
326 |
+
|
327 |
+
|
328 |
+
###########################################
|
329 |
+
# Placeholder for heatmap visualization function
|
330 |
+
###########################################
|
331 |
+
|
332 |
+
def generate_heatmap_visualization(
|
333 |
+
selected_model: str,
|
334 |
+
custom_model: str,
|
335 |
+
scenario_key: str,
|
336 |
+
prompt1: str,
|
337 |
+
prompt2: str,
|
338 |
+
component_type: str,
|
339 |
+
layer_number: int,
|
340 |
+
progress=gr.Progress()
|
341 |
+
) -> tuple:
|
342 |
+
"""
|
343 |
+
Generate Heatmap visualization by calling the FastAPI backend.
|
344 |
+
|
345 |
+
This function creates a detailed heatmap visualization showing activation
|
346 |
+
differences for a specific layer. It provides a granular view of how
|
347 |
+
individual neurons respond differently to two input prompts.
|
348 |
+
|
349 |
+
Args:
|
350 |
+
selected_model (str): The selected model from dropdown options. Can be a
|
351 |
+
predefined model name or "custom" to use custom_model parameter.
|
352 |
+
custom_model (str): Custom HuggingFace model identifier. Only used when
|
353 |
+
selected_model is "custom".
|
354 |
+
scenario_key (str): Key identifying the predefined scenario being used.
|
355 |
+
Used for tracking and logging purposes.
|
356 |
+
prompt1 (str): First prompt to analyze. Should contain text that represents
|
357 |
+
one demographic or condition.
|
358 |
+
prompt2 (str): Second prompt to analyze. Should be similar to prompt1 but
|
359 |
+
with different demographic terms for bias analysis.
|
360 |
+
component_type (str): Type of neural network component to analyze. Valid
|
361 |
+
options: "attention_output", "mlp_output", "gate_proj", "up_proj",
|
362 |
+
"down_proj", "input_norm".
|
363 |
+
layer_number (int): Specific layer number to analyze (0-based indexing).
|
364 |
+
progress (gr.Progress, optional): Gradio progress indicator for user feedback.
|
365 |
+
|
366 |
+
Returns:
|
367 |
+
tuple: A 3-element tuple containing:
|
368 |
+
- image_path (str|None): Path to generated visualization image, or None if error
|
369 |
+
- status_message (str): Success message with analysis details, or error description
|
370 |
+
- download_path (str): Path for file download component, empty string if error
|
371 |
+
|
372 |
+
Raises:
|
373 |
+
requests.exceptions.Timeout: When backend request exceeds timeout limit
|
374 |
+
requests.exceptions.ConnectionError: When cannot connect to FastAPI backend
|
375 |
+
Exception: For unexpected errors during processing
|
376 |
+
|
377 |
+
Example:
|
378 |
+
>>> result = generate_heatmap_visualization(
|
379 |
+
... selected_model="meta-llama/Llama-3.2-1B",
|
380 |
+
... custom_model="",
|
381 |
+
... scenario_key="racial_bias_police",
|
382 |
+
... prompt1="The white man walked. The officer thought",
|
383 |
+
... prompt2="The Black man walked. The officer thought",
|
384 |
+
... component_type="attention_output",
|
385 |
+
... layer_number=7
|
386 |
+
... )
|
387 |
+
>>> image_path, message, download = result
|
388 |
+
|
389 |
+
Note:
|
390 |
+
- This function communicates with the FastAPI backend endpoint `/visualize/heatmap`
|
391 |
+
- The backend uses the OptipFair library to generate actual visualizations
|
392 |
+
- Heatmap analysis shows detailed activation patterns within a single layer
|
393 |
+
- Generated visualizations are temporarily stored and should be cleaned up
|
394 |
+
by the calling application
|
395 |
+
"""
|
396 |
+
|
397 |
+
# Validate layer number
|
398 |
+
if layer_number < 0:
|
399 |
+
return None, "❌ Error: Layer number must be 0 or greater", ""
|
400 |
+
|
401 |
+
if layer_number > 100: # Reasonable sanity check
|
402 |
+
return None, "❌ Error: Layer number seems too large. Most models have fewer than 100 layers", ""
|
403 |
+
|
404 |
+
# Construct layer_key from validated components
|
405 |
+
layer_key = f"{component_type}_layer_{layer_number}"
|
406 |
+
|
407 |
+
# Validate component type
|
408 |
+
valid_components = ["attention_output", "mlp_output", "gate_proj", "up_proj", "down_proj", "input_norm"]
|
409 |
+
if component_type not in valid_components:
|
410 |
+
return None, f"❌ Error: Invalid component type '{component_type}'. Valid options: {', '.join(valid_components)}", ""
|
411 |
+
|
412 |
+
# Input validation - ensure required prompts are provided
|
413 |
+
if not prompt1.strip():
|
414 |
+
return None, "❌ Error: Prompt 1 cannot be empty", ""
|
415 |
+
|
416 |
+
if not prompt2.strip():
|
417 |
+
return None, "❌ Error: Prompt 2 cannot be empty", ""
|
418 |
+
|
419 |
+
if not layer_key.strip():
|
420 |
+
return None, "❌ Error: Layer key cannot be empty", ""
|
421 |
+
|
422 |
+
try:
|
423 |
+
# Update progress indicator for user feedback
|
424 |
+
progress(0.1, desc="🔄 Preparing request...")
|
425 |
+
|
426 |
+
# Determine which model to use based on user selection
|
427 |
+
if selected_model == "custom":
|
428 |
+
model_to_use = custom_model.strip()
|
429 |
+
if not model_to_use:
|
430 |
+
return None, "❌ Error: Please specify a custom model", ""
|
431 |
+
else:
|
432 |
+
model_to_use = selected_model
|
433 |
+
|
434 |
+
# Prepare request payload for FastAPI backend
|
435 |
+
payload = {
|
436 |
+
"model_name": model_to_use.strip(),
|
437 |
+
"prompt_pair": [prompt1.strip(), prompt2.strip()],
|
438 |
+
"layer_key": layer_key.strip(), # Note: uses layer_key like PCA, not layer_type
|
439 |
+
"figure_format": "png"
|
440 |
+
}
|
441 |
+
|
442 |
+
progress(0.3, desc="🚀 Sending request to backend...")
|
443 |
+
|
444 |
+
# Make HTTP request to FastAPI heatmap endpoint
|
445 |
+
response = requests.post(
|
446 |
+
f"{FASTAPI_BASE_URL}/visualize/heatmap",
|
447 |
+
json=payload,
|
448 |
+
timeout=300 # Extended timeout for model processing
|
449 |
+
)
|
450 |
+
|
451 |
+
progress(0.7, desc="📊 Processing visualization...")
|
452 |
+
|
453 |
+
# Handle successful response
|
454 |
+
if response.status_code == 200:
|
455 |
+
# Save binary image data to temporary file
|
456 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
|
457 |
+
tmp_file.write(response.content)
|
458 |
+
image_path = tmp_file.name
|
459 |
+
|
460 |
+
progress(1.0, desc="✅ Visualization complete!")
|
461 |
+
|
462 |
+
# Create detailed success message for user
|
463 |
+
success_msg = f"""✅ **Heatmap Visualization Generated Successfully!**
|
464 |
+
|
465 |
+
**Configuration:**
|
466 |
+
- Model: {model_to_use}
|
467 |
+
- Component: {component_type}
|
468 |
+
- Layer: {layer_number}
|
469 |
+
- Prompts compared: {len(prompt1.split())} vs {len(prompt2.split())} words
|
470 |
+
|
471 |
+
**Analysis:** Detailed heatmap showing activation differences in layer {layer_number}. Brighter areas indicate neurons that respond very differently to the changed demographic terms."""
|
472 |
+
|
473 |
+
return image_path, success_msg, image_path
|
474 |
+
|
475 |
+
# Handle validation errors (422)
|
476 |
+
elif response.status_code == 422:
|
477 |
+
error_detail = response.json().get('detail', 'Validation error')
|
478 |
+
return None, f"❌ **Validation Error:**\n{error_detail}", ""
|
479 |
+
|
480 |
+
# Handle server errors (500)
|
481 |
+
elif response.status_code == 500:
|
482 |
+
error_detail = response.json().get('detail', 'Internal server error')
|
483 |
+
return None, f"❌ **Server Error:**\n{error_detail}", ""
|
484 |
+
|
485 |
+
# Handle other HTTP errors
|
486 |
+
else:
|
487 |
+
return None, f"❌ **Unexpected Error:**\nHTTP {response.status_code}: {response.text}", ""
|
488 |
+
|
489 |
+
# Handle specific request exceptions
|
490 |
+
except requests.exceptions.Timeout:
|
491 |
+
return None, "❌ **Timeout Error:**\nThe request took too long. This might happen with large models. Try again or use a different layer.", ""
|
492 |
+
|
493 |
+
except requests.exceptions.ConnectionError:
|
494 |
+
return None, "❌ **Connection Error:**\nCannot connect to the backend. Make sure the FastAPI server is running:\n`uvicorn main:app --reload`", ""
|
495 |
+
|
496 |
+
# Handle any other unexpected exceptions
|
497 |
+
except Exception as e:
|
498 |
+
logger.exception("Error in Heatmap visualization")
|
499 |
+
return None, f"❌ **Unexpected Error:**\n{str(e)}", ""
|
500 |
+
|
501 |
+
############################################
|
502 |
+
# Create the Gradio interface
|
503 |
+
############################################
|
504 |
+
# This function sets up the Gradio Blocks interface with tabs for PCA, Mean Difference, and Heatmap visualizations.
|
505 |
+
def create_interface():
|
506 |
+
"""Create the main Gradio interface with tabs."""
|
507 |
+
|
508 |
+
with gr.Blocks(
|
509 |
+
title="OptiPFair Bias Visualization Tool",
|
510 |
+
theme=gr.themes.Soft(),
|
511 |
+
css="""
|
512 |
+
.container { max-width: 1200px; margin: auto; }
|
513 |
+
.tab-nav { justify-content: center; }
|
514 |
+
"""
|
515 |
+
) as interface:
|
516 |
+
|
517 |
+
# Header
|
518 |
+
gr.Markdown("""
|
519 |
+
# 🔍 OptiPFair Bias Visualization Tool
|
520 |
+
|
521 |
+
Analyze potential biases in Large Language Models using advanced visualization techniques.
|
522 |
+
Built with [OptiPFair](https://github.com/peremartra/optipfair) library.
|
523 |
+
""")
|
524 |
+
|
525 |
+
# Health check section
|
526 |
+
with gr.Row():
|
527 |
+
with gr.Column(scale=2):
|
528 |
+
health_btn = gr.Button("🏥 Check Backend Status", variant="secondary")
|
529 |
+
with gr.Column(scale=3):
|
530 |
+
health_output = gr.Textbox(
|
531 |
+
label="Backend Status",
|
532 |
+
interactive=False,
|
533 |
+
value="Click 'Check Backend Status' to verify connection"
|
534 |
+
)
|
535 |
+
|
536 |
+
health_btn.click(health_check, outputs=health_output)
|
537 |
+
|
538 |
+
# Añadir después de health_btn.click(...) y antes de "# Main tabs"
|
539 |
+
with gr.Row():
|
540 |
+
with gr.Column(scale=2):
|
541 |
+
model_dropdown = gr.Dropdown(
|
542 |
+
choices=AVAILABLE_MODELS,
|
543 |
+
label="🤖 Select Model",
|
544 |
+
value=DEFAULT_MODEL
|
545 |
+
)
|
546 |
+
with gr.Column(scale=3):
|
547 |
+
custom_model_input = gr.Textbox(
|
548 |
+
label="Custom Model (HuggingFace ID)",
|
549 |
+
placeholder="e.g., microsoft/DialoGPT-large",
|
550 |
+
visible=False # Inicialmente oculto
|
551 |
+
)
|
552 |
+
|
553 |
+
# toggle Custom Model Input
|
554 |
+
def toggle_custom_model(selected_model):
|
555 |
+
if selected_model == "custom":
|
556 |
+
return gr.update(visible=True)
|
557 |
+
return gr.update(visible=False)
|
558 |
+
|
559 |
+
model_dropdown.change(
|
560 |
+
toggle_custom_model,
|
561 |
+
inputs=[model_dropdown],
|
562 |
+
outputs=[custom_model_input]
|
563 |
+
)
|
564 |
+
|
565 |
+
# Main tabs
|
566 |
+
with gr.Tabs() as tabs:
|
567 |
+
#################
|
568 |
+
# PCA Visualization Tab
|
569 |
+
##############
|
570 |
+
with gr.Tab("📊 PCA Analysis"):
|
571 |
+
gr.Markdown("### Principal Component Analysis of Model Activations")
|
572 |
+
gr.Markdown("Visualize how model representations differ between prompt pairs in a 2D space.")
|
573 |
+
|
574 |
+
with gr.Row():
|
575 |
+
# Left column: Configuration
|
576 |
+
with gr.Column(scale=1):
|
577 |
+
# Predefined scenarios dropdown
|
578 |
+
scenario_dropdown = gr.Dropdown(
|
579 |
+
choices=[(v["description"], k) for k, v in PREDEFINED_PROMPTS.items()],
|
580 |
+
label="📋 Predefined Scenarios",
|
581 |
+
value=list(PREDEFINED_PROMPTS.keys())[0]
|
582 |
+
)
|
583 |
+
|
584 |
+
# Prompt inputs
|
585 |
+
prompt1_input = gr.Textbox(
|
586 |
+
label="Prompt 1",
|
587 |
+
placeholder="Enter first prompt...",
|
588 |
+
lines=2,
|
589 |
+
value=PREDEFINED_PROMPTS[list(PREDEFINED_PROMPTS.keys())[0]]["prompt1"]
|
590 |
+
)
|
591 |
+
prompt2_input = gr.Textbox(
|
592 |
+
label="Prompt 2",
|
593 |
+
placeholder="Enter second prompt...",
|
594 |
+
lines=2,
|
595 |
+
value=PREDEFINED_PROMPTS[list(PREDEFINED_PROMPTS.keys())[0]]["prompt2"]
|
596 |
+
)
|
597 |
+
|
598 |
+
# Layer configuration - Component Type
|
599 |
+
component_dropdown = gr.Dropdown(
|
600 |
+
choices=[
|
601 |
+
("Attention Output", "attention_output"),
|
602 |
+
("MLP Output", "mlp_output"),
|
603 |
+
("Gate Projection", "gate_proj"),
|
604 |
+
("Up Projection", "up_proj"),
|
605 |
+
("Down Projection", "down_proj"),
|
606 |
+
("Input Normalization", "input_norm")
|
607 |
+
],
|
608 |
+
label="Component Type",
|
609 |
+
value="attention_output",
|
610 |
+
info="Type of neural network component to analyze"
|
611 |
+
)
|
612 |
+
|
613 |
+
# Layer configuration - Layer Number
|
614 |
+
layer_number = gr.Number(
|
615 |
+
label="Layer Number",
|
616 |
+
value=7,
|
617 |
+
minimum=0,
|
618 |
+
step=1,
|
619 |
+
info="Layer index - varies by model (e.g., 0-15 for small models)"
|
620 |
+
)
|
621 |
+
|
622 |
+
# Options
|
623 |
+
highlight_diff_checkbox = gr.Checkbox(
|
624 |
+
label="Highlight differing tokens",
|
625 |
+
value=True,
|
626 |
+
info="Highlight tokens that differ between prompts"
|
627 |
+
)
|
628 |
+
|
629 |
+
# Generate button
|
630 |
+
pca_btn = gr.Button("🔍 Generate PCA Visualization", variant="primary", size="lg")
|
631 |
+
|
632 |
+
# Status output
|
633 |
+
pca_status = gr.Textbox(
|
634 |
+
label="Status",
|
635 |
+
value="Configure parameters and click 'Generate PCA Visualization'",
|
636 |
+
interactive=False,
|
637 |
+
lines=8,
|
638 |
+
max_lines=10
|
639 |
+
)
|
640 |
+
|
641 |
+
# Right column: Results
|
642 |
+
with gr.Column(scale=1):
|
643 |
+
# Image display
|
644 |
+
pca_image = gr.Image(
|
645 |
+
label="PCA Visualization Result",
|
646 |
+
type="filepath",
|
647 |
+
show_label=True,
|
648 |
+
show_download_button=True,
|
649 |
+
interactive=False,
|
650 |
+
height=400
|
651 |
+
)
|
652 |
+
|
653 |
+
# Download button (additional)
|
654 |
+
download_pca = gr.File(
|
655 |
+
label="📥 Download Visualization",
|
656 |
+
visible=False
|
657 |
+
)
|
658 |
+
|
659 |
+
# Update prompts when scenario changes
|
660 |
+
scenario_dropdown.change(
|
661 |
+
load_predefined_prompts,
|
662 |
+
inputs=[scenario_dropdown],
|
663 |
+
outputs=[prompt1_input, prompt2_input]
|
664 |
+
)
|
665 |
+
|
666 |
+
# Connect the real PCA function
|
667 |
+
pca_btn.click(
|
668 |
+
generate_pca_visualization,
|
669 |
+
inputs=[
|
670 |
+
model_dropdown,
|
671 |
+
custom_model_input,
|
672 |
+
scenario_dropdown,
|
673 |
+
prompt1_input,
|
674 |
+
prompt2_input,
|
675 |
+
component_dropdown, # ← NUEVO: tipo de componente
|
676 |
+
layer_number, # ← NUEVO: número de capa
|
677 |
+
highlight_diff_checkbox
|
678 |
+
],
|
679 |
+
outputs=[pca_image, pca_status, download_pca],
|
680 |
+
show_progress=True
|
681 |
+
)
|
682 |
+
####################
|
683 |
+
# Mean Difference Tab
|
684 |
+
##################
|
685 |
+
with gr.Tab("📈 Mean Difference"):
|
686 |
+
gr.Markdown("### Mean Activation Differences Across Layers")
|
687 |
+
gr.Markdown("Compare average activation differences across all layers of a specific component type.")
|
688 |
+
|
689 |
+
with gr.Row():
|
690 |
+
# Left column: Configuration
|
691 |
+
with gr.Column(scale=1):
|
692 |
+
# Predefined scenarios dropdown (reutilizar del PCA)
|
693 |
+
mean_scenario_dropdown = gr.Dropdown(
|
694 |
+
choices=[(v["description"], k) for k, v in PREDEFINED_PROMPTS.items()],
|
695 |
+
label="📋 Predefined Scenarios",
|
696 |
+
value=list(PREDEFINED_PROMPTS.keys())[0]
|
697 |
+
)
|
698 |
+
|
699 |
+
# Prompt inputs
|
700 |
+
mean_prompt1_input = gr.Textbox(
|
701 |
+
label="Prompt 1",
|
702 |
+
placeholder="Enter first prompt...",
|
703 |
+
lines=2,
|
704 |
+
value=PREDEFINED_PROMPTS[list(PREDEFINED_PROMPTS.keys())[0]]["prompt1"]
|
705 |
+
)
|
706 |
+
mean_prompt2_input = gr.Textbox(
|
707 |
+
label="Prompt 2",
|
708 |
+
placeholder="Enter second prompt...",
|
709 |
+
lines=2,
|
710 |
+
value=PREDEFINED_PROMPTS[list(PREDEFINED_PROMPTS.keys())[0]]["prompt2"]
|
711 |
+
)
|
712 |
+
|
713 |
+
# Component type configuration
|
714 |
+
mean_component_dropdown = gr.Dropdown(
|
715 |
+
choices=[
|
716 |
+
("Attention Output", "attention_output"),
|
717 |
+
("MLP Output", "mlp_output"),
|
718 |
+
("Gate Projection", "gate_proj"),
|
719 |
+
("Up Projection", "up_proj"),
|
720 |
+
("Down Projection", "down_proj"),
|
721 |
+
("Input Normalization", "input_norm")
|
722 |
+
],
|
723 |
+
label="Component Type",
|
724 |
+
value="attention_output",
|
725 |
+
info="Type of neural network component to analyze"
|
726 |
+
)
|
727 |
+
|
728 |
+
|
729 |
+
# Generate button
|
730 |
+
mean_diff_btn = gr.Button("📈 Generate Mean Difference Visualization", variant="primary", size="lg")
|
731 |
+
|
732 |
+
# Status output
|
733 |
+
mean_diff_status = gr.Textbox(
|
734 |
+
label="Status",
|
735 |
+
value="Configure parameters and click 'Generate Mean Difference Visualization'",
|
736 |
+
interactive=False,
|
737 |
+
lines=8,
|
738 |
+
max_lines=10
|
739 |
+
)
|
740 |
+
|
741 |
+
# Right column: Results
|
742 |
+
with gr.Column(scale=1):
|
743 |
+
# Image display
|
744 |
+
mean_diff_image = gr.Image(
|
745 |
+
label="Mean Difference Visualization Result",
|
746 |
+
type="filepath",
|
747 |
+
show_label=True,
|
748 |
+
show_download_button=True,
|
749 |
+
interactive=False,
|
750 |
+
height=400
|
751 |
+
)
|
752 |
+
|
753 |
+
# Download button (additional)
|
754 |
+
download_mean_diff = gr.File(
|
755 |
+
label="📥 Download Visualization",
|
756 |
+
visible=False
|
757 |
+
)
|
758 |
+
# Update prompts when scenario changes for Mean Difference
|
759 |
+
mean_scenario_dropdown.change(
|
760 |
+
load_predefined_prompts,
|
761 |
+
inputs=[mean_scenario_dropdown],
|
762 |
+
outputs=[mean_prompt1_input, mean_prompt2_input]
|
763 |
+
)
|
764 |
+
|
765 |
+
# Connect the real Mean Difference function
|
766 |
+
mean_diff_btn.click(
|
767 |
+
generate_mean_diff_visualization,
|
768 |
+
inputs=[
|
769 |
+
model_dropdown, # Reutilizamos el selector de modelo global
|
770 |
+
custom_model_input, # Reutilizamos el campo de modelo custom global
|
771 |
+
mean_scenario_dropdown,
|
772 |
+
mean_prompt1_input,
|
773 |
+
mean_prompt2_input,
|
774 |
+
mean_component_dropdown,
|
775 |
+
],
|
776 |
+
outputs=[mean_diff_image, mean_diff_status, download_mean_diff],
|
777 |
+
show_progress=True
|
778 |
+
)
|
779 |
+
###################
|
780 |
+
# Heatmap Tab
|
781 |
+
##################
|
782 |
+
with gr.Tab("🔥 Heatmap"):
|
783 |
+
gr.Markdown("### Activation Difference Heatmap")
|
784 |
+
gr.Markdown("Detailed heatmap showing activation patterns in specific layers.")
|
785 |
+
|
786 |
+
with gr.Row():
|
787 |
+
# Left column: Configuration
|
788 |
+
with gr.Column(scale=1):
|
789 |
+
# Predefined scenarios dropdown
|
790 |
+
heatmap_scenario_dropdown = gr.Dropdown(
|
791 |
+
choices=[(v["description"], k) for k, v in PREDEFINED_PROMPTS.items()],
|
792 |
+
label="📋 Predefined Scenarios",
|
793 |
+
value=list(PREDEFINED_PROMPTS.keys())[0]
|
794 |
+
)
|
795 |
+
|
796 |
+
# Prompt inputs
|
797 |
+
heatmap_prompt1_input = gr.Textbox(
|
798 |
+
label="Prompt 1",
|
799 |
+
placeholder="Enter first prompt...",
|
800 |
+
lines=2,
|
801 |
+
value=PREDEFINED_PROMPTS[list(PREDEFINED_PROMPTS.keys())[0]]["prompt1"]
|
802 |
+
)
|
803 |
+
heatmap_prompt2_input = gr.Textbox(
|
804 |
+
label="Prompt 2",
|
805 |
+
placeholder="Enter second prompt...",
|
806 |
+
lines=2,
|
807 |
+
value=PREDEFINED_PROMPTS[list(PREDEFINED_PROMPTS.keys())[0]]["prompt2"]
|
808 |
+
)
|
809 |
+
|
810 |
+
# Component type configuration
|
811 |
+
heatmap_component_dropdown = gr.Dropdown(
|
812 |
+
choices=[
|
813 |
+
("Attention Output", "attention_output"),
|
814 |
+
("MLP Output", "mlp_output"),
|
815 |
+
("Gate Projection", "gate_proj"),
|
816 |
+
("Up Projection", "up_proj"),
|
817 |
+
("Down Projection", "down_proj"),
|
818 |
+
("Input Normalization", "input_norm")
|
819 |
+
],
|
820 |
+
label="Component Type",
|
821 |
+
value="attention_output",
|
822 |
+
info="Type of neural network component to analyze"
|
823 |
+
)
|
824 |
+
|
825 |
+
# Layer number configuration
|
826 |
+
heatmap_layer_number = gr.Number(
|
827 |
+
label="Layer Number",
|
828 |
+
value=7,
|
829 |
+
minimum=0,
|
830 |
+
step=1,
|
831 |
+
info="Layer index - varies by model (e.g., 0-15 for small models)"
|
832 |
+
)
|
833 |
+
|
834 |
+
# Generate button
|
835 |
+
heatmap_btn = gr.Button("🔥 Generate Heatmap Visualization", variant="primary", size="lg")
|
836 |
+
|
837 |
+
# Status output
|
838 |
+
heatmap_status = gr.Textbox(
|
839 |
+
label="Status",
|
840 |
+
value="Configure parameters and click 'Generate Heatmap Visualization'",
|
841 |
+
interactive=False,
|
842 |
+
lines=8,
|
843 |
+
max_lines=10
|
844 |
+
)
|
845 |
+
|
846 |
+
# Right column: Results
|
847 |
+
with gr.Column(scale=1):
|
848 |
+
# Image display
|
849 |
+
heatmap_image = gr.Image(
|
850 |
+
label="Heatmap Visualization Result",
|
851 |
+
type="filepath",
|
852 |
+
show_label=True,
|
853 |
+
show_download_button=True,
|
854 |
+
interactive=False,
|
855 |
+
height=400
|
856 |
+
)
|
857 |
+
|
858 |
+
# Download button (additional)
|
859 |
+
download_heatmap = gr.File(
|
860 |
+
label="📥 Download Visualization",
|
861 |
+
visible=False
|
862 |
+
)
|
863 |
+
# Update prompts when scenario changes for Heatmap
|
864 |
+
heatmap_scenario_dropdown.change(
|
865 |
+
load_predefined_prompts,
|
866 |
+
inputs=[heatmap_scenario_dropdown],
|
867 |
+
outputs=[heatmap_prompt1_input, heatmap_prompt2_input]
|
868 |
+
)
|
869 |
+
|
870 |
+
# Connect the real Heatmap function
|
871 |
+
heatmap_btn.click(
|
872 |
+
generate_heatmap_visualization,
|
873 |
+
inputs=[
|
874 |
+
model_dropdown, # Reutilizamos el selector de modelo global
|
875 |
+
custom_model_input, # Reutilizamos el campo de modelo custom global
|
876 |
+
heatmap_scenario_dropdown,
|
877 |
+
heatmap_prompt1_input,
|
878 |
+
heatmap_prompt2_input,
|
879 |
+
heatmap_component_dropdown,
|
880 |
+
heatmap_layer_number
|
881 |
+
],
|
882 |
+
outputs=[heatmap_image, heatmap_status, download_heatmap],
|
883 |
+
show_progress=True
|
884 |
+
)
|
885 |
+
# Footer
|
886 |
+
gr.Markdown("""
|
887 |
+
---
|
888 |
+
**📚 How to use:**
|
889 |
+
1. Check that the backend is running
|
890 |
+
2. Select a predefined scenario or enter custom prompts
|
891 |
+
3. Configure layer settings
|
892 |
+
4. Generate visualizations to analyze potential biases
|
893 |
+
|
894 |
+
**🔗 Resources:** [OptiPFair Documentation](https://github.com/peremartra/optipfair) |
|
895 |
+
""")
|
896 |
+
|
897 |
+
return interface
|
898 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.115.12
|
2 |
+
uvicorn==0.34.2
|
3 |
+
gradio==5.29.1
|
4 |
+
requests==2.32.3
|
5 |
+
optipfair[viz]==0.1.3
|
6 |
+
torch==2.7.0
|
7 |
+
transformers==4.51.3
|
8 |
+
matplotlib==3.10.3
|
9 |
+
numpy==1.26.4
|
10 |
+
Pillow==11.2.1
|
routers/__pycache__/visualize.cpython-312.pyc
ADDED
Binary file (5.4 kB). View file
|
|
routers/visualize.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# routers/visualize.py
|
2 |
+
import os
|
3 |
+
import logging
|
4 |
+
from fastapi import APIRouter, HTTPException
|
5 |
+
from fastapi.responses import FileResponse
|
6 |
+
from schemas.visualize import (
|
7 |
+
VisualizePCARequest,
|
8 |
+
VisualizeMeanDiffRequest,
|
9 |
+
VisualizeHeatmapRequest,
|
10 |
+
)
|
11 |
+
from utils.visualize_pca import (
|
12 |
+
run_visualize_pca,
|
13 |
+
run_visualize_mean_diff,
|
14 |
+
run_visualize_heatmap,
|
15 |
+
)
|
16 |
+
|
17 |
+
logger = logging.getLogger(__name__)
|
18 |
+
logger.setLevel(logging.INFO)
|
19 |
+
|
20 |
+
router = APIRouter(
|
21 |
+
prefix="/visualize",
|
22 |
+
tags=["visualization"],
|
23 |
+
)
|
24 |
+
|
25 |
+
@router.post(
|
26 |
+
"/pca",
|
27 |
+
summary="Generates and returns the PCA visualization of activations",
|
28 |
+
response_class=FileResponse,
|
29 |
+
)
|
30 |
+
async def visualize_pca_endpoint(req: VisualizePCARequest):
|
31 |
+
"""
|
32 |
+
Receives the parameters, calls the wrapper for optipfair.bias.visualize_pca,
|
33 |
+
and returns the resulting PNG/SVG image.
|
34 |
+
"""
|
35 |
+
# 1. Execute the image generation and get the file path
|
36 |
+
try:
|
37 |
+
filepath = run_visualize_pca(
|
38 |
+
model_name=req.model_name,
|
39 |
+
prompt_pair=tuple(req.prompt_pair),
|
40 |
+
layer_key=req.layer_key,
|
41 |
+
highlight_diff=req.highlight_diff,
|
42 |
+
output_dir=req.output_dir,
|
43 |
+
figure_format=req.figure_format,
|
44 |
+
pair_index=req.pair_index,
|
45 |
+
)
|
46 |
+
except Exception as e:
|
47 |
+
# Log the full trace for debugging
|
48 |
+
logger.exception("❌ Error in visualize_pca_endpoint")
|
49 |
+
# And return the message to the client
|
50 |
+
raise HTTPException(status_code=500, detail=str(e))
|
51 |
+
# 2. Verify that the file exists
|
52 |
+
if not filepath or not os.path.isfile(filepath):
|
53 |
+
raise HTTPException(status_code=500, detail="Image file not found after generation")
|
54 |
+
|
55 |
+
# 3. Return the file directly to the client
|
56 |
+
return FileResponse(
|
57 |
+
path=filepath,
|
58 |
+
media_type=f"image/{req.figure_format}",
|
59 |
+
filename=os.path.basename(filepath),
|
60 |
+
headers={"Content-Disposition": f'inline; filename="{os.path.basename(filepath)}"'},
|
61 |
+
)
|
62 |
+
|
63 |
+
@router.post("/mean-diff", response_class=FileResponse)
|
64 |
+
async def visualize_mean_diff_endpoint(req: VisualizeMeanDiffRequest):
|
65 |
+
"""
|
66 |
+
Receives the parameters, calls the wrapper for optipfair.bias.visualize_mean_differences,
|
67 |
+
and returns the resulting PNG/SVG image.
|
68 |
+
"""
|
69 |
+
try:
|
70 |
+
filepath = run_visualize_mean_diff(
|
71 |
+
model_name=req.model_name,
|
72 |
+
prompt_pair=tuple(req.prompt_pair),
|
73 |
+
layer_type=req.layer_type, # Changed from layer_key to layer_type
|
74 |
+
figure_format=req.figure_format,
|
75 |
+
output_dir=req.output_dir,
|
76 |
+
pair_index=req.pair_index,
|
77 |
+
)
|
78 |
+
except Exception as e:
|
79 |
+
# Log the full trace for debugging
|
80 |
+
logger.exception("Error in mean-diff endpoint")
|
81 |
+
raise HTTPException(status_code=500, detail=str(e))
|
82 |
+
|
83 |
+
# Verify that the file exists
|
84 |
+
if not os.path.isfile(filepath):
|
85 |
+
raise HTTPException(status_code=500, detail="Image file not found")
|
86 |
+
|
87 |
+
# Return the file directly to the client
|
88 |
+
return FileResponse(
|
89 |
+
path=filepath,
|
90 |
+
media_type=f"image/{req.figure_format}",
|
91 |
+
filename=os.path.basename(filepath),
|
92 |
+
headers={"Content-Disposition": f'inline; filename="{os.path.basename(filepath)}"'}
|
93 |
+
)
|
94 |
+
|
95 |
+
@router.post("/heatmap", response_class=FileResponse)
|
96 |
+
async def visualize_heatmap_endpoint(req: VisualizeHeatmapRequest):
|
97 |
+
"""
|
98 |
+
Receives the parameters, calls the wrapper for optipfair.bias.visualize_heatmap,
|
99 |
+
and returns the resulting PNG/SVG image.
|
100 |
+
"""
|
101 |
+
try:
|
102 |
+
filepath = run_visualize_heatmap(
|
103 |
+
model_name=req.model_name,
|
104 |
+
prompt_pair=tuple(req.prompt_pair),
|
105 |
+
layer_key=req.layer_key,
|
106 |
+
figure_format=req.figure_format,
|
107 |
+
output_dir=req.output_dir,
|
108 |
+
)
|
109 |
+
except Exception as e:
|
110 |
+
# Log the full trace for debugging
|
111 |
+
logger.exception("Error in heatmap endpoint")
|
112 |
+
raise HTTPException(status_code=500, detail=str(e))
|
113 |
+
|
114 |
+
# Verify that the file exists
|
115 |
+
if not os.path.isfile(filepath):
|
116 |
+
raise HTTPException(status_code=500, detail="Image file not found")
|
117 |
+
|
118 |
+
# Return the file directly to the client
|
119 |
+
return FileResponse(
|
120 |
+
path=filepath,
|
121 |
+
media_type=f"image/{req.figure_format}",
|
122 |
+
filename=os.path.basename(filepath),
|
123 |
+
headers={"Content-Disposition": f'inline; filename="{os.path.basename(filepath)}"'}
|
124 |
+
)
|
schemas/__pycache__/visualize.cpython-312.pyc
ADDED
Binary file (2.54 kB). View file
|
|
schemas/visualize.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# schemas/visualize.py
|
2 |
+
from pydantic import BaseModel, field_validator
|
3 |
+
from typing import List, Optional, Union, Tuple
|
4 |
+
|
5 |
+
class VisualizePCARequest(BaseModel):
|
6 |
+
"""
|
7 |
+
Schema for the /visualize-pca endpoint.
|
8 |
+
"""
|
9 |
+
model_name: str
|
10 |
+
prompt_pair: List[str]
|
11 |
+
layer_key: str
|
12 |
+
highlight_diff: bool = True
|
13 |
+
figure_format: str = "png"
|
14 |
+
pair_index: int = 0
|
15 |
+
output_dir: Optional[str] = None
|
16 |
+
|
17 |
+
@field_validator("prompt_pair")
|
18 |
+
def must_be_two_prompts(cls, v):
|
19 |
+
if len(v) != 2:
|
20 |
+
raise ValueError("prompt_pair must be a list of exactly two strings")
|
21 |
+
return v
|
22 |
+
|
23 |
+
class VisualizeMeanDiffRequest(BaseModel):
|
24 |
+
model_name: str
|
25 |
+
prompt_pair: List[str]
|
26 |
+
layer_type: str # Changed from layer_key to layer_type
|
27 |
+
figure_format: str = "png"
|
28 |
+
output_dir: Optional[str] = None
|
29 |
+
pair_index: int = 0
|
30 |
+
|
31 |
+
@field_validator("prompt_pair")
|
32 |
+
def must_be_two_prompts(cls, v):
|
33 |
+
if len(v) != 2:
|
34 |
+
raise ValueError("prompt_pair must be a list of exactly two strings")
|
35 |
+
return v
|
36 |
+
|
37 |
+
class VisualizeHeatmapRequest(BaseModel):
|
38 |
+
"""
|
39 |
+
Schema for the /visualize/heatmap endpoint.
|
40 |
+
"""
|
41 |
+
model_name: str
|
42 |
+
prompt_pair: List[str]
|
43 |
+
layer_key: str
|
44 |
+
figure_format: str = "png"
|
45 |
+
output_dir: Optional[str] = None
|
46 |
+
|
47 |
+
@field_validator("prompt_pair")
|
48 |
+
def must_be_two_prompts(cls, v):
|
49 |
+
if len(v) != 2:
|
50 |
+
raise ValueError("prompt_pair must be a list of exactly two strings")
|
51 |
+
return v
|
utils/__pycache__/visualize_pca.cpython-312.pyc
ADDED
Binary file (6.65 kB). View file
|
|
utils/visualize_pca.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# utils/visualize_pca.py
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
import logging
|
5 |
+
from functools import lru_cache
|
6 |
+
from typing import Tuple, Optional, Union, List
|
7 |
+
|
8 |
+
import torch
|
9 |
+
from optipfair.bias import visualize_pca, visualize_mean_differences, visualize_heatmap
|
10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
11 |
+
|
12 |
+
import matplotlib
|
13 |
+
matplotlib.use('Agg') # Use 'Agg' backend for non-GUI environments
|
14 |
+
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
logger.setLevel(logging.INFO)
|
17 |
+
|
18 |
+
@lru_cache(maxsize=None)
|
19 |
+
def load_model_tokenizer(model_name: str):
|
20 |
+
"""
|
21 |
+
Loads the model and tokenizer on the CPU once and caches the result.
|
22 |
+
"""
|
23 |
+
logger.info(f"Loading model and tokenizer for '{model_name}'")
|
24 |
+
|
25 |
+
# Device selection: MPS (Apple Silicon) > CUDA > CPU
|
26 |
+
if torch.cuda.is_available():
|
27 |
+
device = torch.device("cuda")
|
28 |
+
elif torch.mps.is_available():
|
29 |
+
device = torch.device("mps")
|
30 |
+
else:
|
31 |
+
device = torch.device("cpu")
|
32 |
+
logger.info(f"Using device: {device}")
|
33 |
+
|
34 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
35 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
36 |
+
|
37 |
+
model = model.to(device)
|
38 |
+
|
39 |
+
logger.info(f"Model loaded on device: {next(model.parameters()).device}")
|
40 |
+
|
41 |
+
return model, tokenizer
|
42 |
+
|
43 |
+
def run_visualize_pca(
|
44 |
+
model_name: str,
|
45 |
+
prompt_pair: Tuple[str, str],
|
46 |
+
layer_key: str,
|
47 |
+
highlight_diff: bool = True,
|
48 |
+
output_dir: Optional[str] = None,
|
49 |
+
figure_format: str = "png",
|
50 |
+
pair_index: int = 0,
|
51 |
+
) -> str:
|
52 |
+
if output_dir is None:
|
53 |
+
output_dir = tempfile.mkdtemp(prefix="optipfair_pca_")
|
54 |
+
os.makedirs(output_dir, exist_ok=True)
|
55 |
+
|
56 |
+
model, tokenizer = load_model_tokenizer(model_name)
|
57 |
+
|
58 |
+
visualize_pca(
|
59 |
+
model=model,
|
60 |
+
tokenizer=tokenizer,
|
61 |
+
prompt_pair=prompt_pair,
|
62 |
+
layer_key=layer_key,
|
63 |
+
highlight_diff=highlight_diff,
|
64 |
+
output_dir=output_dir,
|
65 |
+
figure_format=figure_format,
|
66 |
+
pair_index=pair_index
|
67 |
+
)
|
68 |
+
|
69 |
+
layer_parts = layer_key.split("_")
|
70 |
+
layer_type = "_".join(layer_parts[:-1])
|
71 |
+
layer_num = layer_parts[-1]
|
72 |
+
filename = build_visualization_filename(
|
73 |
+
vis_type="pca",
|
74 |
+
layer_type=layer_type,
|
75 |
+
layer_num=layer_num,
|
76 |
+
pair_index=pair_index,
|
77 |
+
figure_format=figure_format
|
78 |
+
)
|
79 |
+
filepath = os.path.join(output_dir, filename)
|
80 |
+
|
81 |
+
if not os.path.isfile(filepath):
|
82 |
+
raise FileNotFoundError(f"Expected image file not found: {filepath}")
|
83 |
+
|
84 |
+
logger.info(f"PCA image saved at {filepath}")
|
85 |
+
return filepath
|
86 |
+
|
87 |
+
def run_visualize_mean_diff(
|
88 |
+
model_name: str,
|
89 |
+
prompt_pair: Tuple[str, str],
|
90 |
+
layer_type: str, # Changed from layer_key to layer_type
|
91 |
+
figure_format: str = "png",
|
92 |
+
output_dir: Optional[str] = None,
|
93 |
+
pair_index: int = 0,
|
94 |
+
) -> str:
|
95 |
+
if output_dir is None:
|
96 |
+
output_dir = tempfile.mkdtemp(prefix="optipfair_mean_diff_")
|
97 |
+
os.makedirs(output_dir, exist_ok=True)
|
98 |
+
|
99 |
+
model, tokenizer = load_model_tokenizer(model_name)
|
100 |
+
|
101 |
+
visualize_mean_differences(
|
102 |
+
model=model,
|
103 |
+
tokenizer=tokenizer,
|
104 |
+
prompt_pair=prompt_pair,
|
105 |
+
layer_type=layer_type,
|
106 |
+
layers="all", # By default, show all layers
|
107 |
+
output_dir=output_dir,
|
108 |
+
figure_format=figure_format,
|
109 |
+
pair_index=pair_index
|
110 |
+
)
|
111 |
+
|
112 |
+
filename = build_visualization_filename(
|
113 |
+
vis_type="mean_diff",
|
114 |
+
layer_type=layer_type,
|
115 |
+
pair_index=pair_index,
|
116 |
+
figure_format=figure_format
|
117 |
+
)
|
118 |
+
filepath = os.path.join(output_dir, filename)
|
119 |
+
if not os.path.isfile(filepath):
|
120 |
+
raise FileNotFoundError(f"Expected image file not found: {filepath}")
|
121 |
+
logger.info(f"Mean-diff image saved at {filepath}")
|
122 |
+
return filepath
|
123 |
+
|
124 |
+
def run_visualize_heatmap(
|
125 |
+
model_name: str,
|
126 |
+
prompt_pair: Tuple[str, str],
|
127 |
+
layer_key: str,
|
128 |
+
figure_format: str = "png",
|
129 |
+
output_dir: Optional[str] = None,
|
130 |
+
pair_index: int = 0,
|
131 |
+
) -> str:
|
132 |
+
if output_dir is None:
|
133 |
+
output_dir = tempfile.mkdtemp(prefix="optipfair_heatmap_")
|
134 |
+
os.makedirs(output_dir, exist_ok=True)
|
135 |
+
|
136 |
+
model, tokenizer = load_model_tokenizer(model_name)
|
137 |
+
|
138 |
+
visualize_heatmap(
|
139 |
+
model=model,
|
140 |
+
tokenizer=tokenizer,
|
141 |
+
prompt_pair=prompt_pair,
|
142 |
+
layer_key=layer_key,
|
143 |
+
output_dir=output_dir,
|
144 |
+
figure_format=figure_format,
|
145 |
+
pair_index=pair_index
|
146 |
+
)
|
147 |
+
|
148 |
+
parts = layer_key.split("_")
|
149 |
+
layer_type = "_".join(parts[:-1])
|
150 |
+
layer_num = parts[-1]
|
151 |
+
filename = build_visualization_filename(
|
152 |
+
vis_type="heatmap",
|
153 |
+
layer_type=layer_type,
|
154 |
+
layer_num=layer_num,
|
155 |
+
pair_index=pair_index,
|
156 |
+
figure_format=figure_format
|
157 |
+
)
|
158 |
+
filepath = os.path.join(output_dir, filename)
|
159 |
+
if not os.path.isfile(filepath):
|
160 |
+
raise FileNotFoundError(f"Expected image file not found: {filepath}")
|
161 |
+
logger.info(f"Heatmap image saved at {filepath}")
|
162 |
+
return filepath
|
163 |
+
|
164 |
+
def build_visualization_filename(
|
165 |
+
vis_type: str,
|
166 |
+
layer_type: str,
|
167 |
+
layer_num: str = None,
|
168 |
+
layers: Union[str, List[int]] = None,
|
169 |
+
pair_index: int = 0,
|
170 |
+
figure_format: str = "png"
|
171 |
+
) -> str:
|
172 |
+
"""
|
173 |
+
Builds the filename for any visualization.
|
174 |
+
"""
|
175 |
+
if vis_type == "mean_diff":
|
176 |
+
# The visualize_mean_differences function does not include the layer number in the filename
|
177 |
+
return f"mean_diff_{layer_type}_pair{pair_index}.{figure_format}"
|
178 |
+
elif vis_type in ("pca", "heatmap"):
|
179 |
+
return f"{vis_type}_{layer_type}_{layer_num}_pair{pair_index}.{figure_format}"
|
180 |
+
else:
|
181 |
+
raise ValueError(f"Unknown visualization type: {vis_type}")
|
182 |
+
|