OptiPFair-API
November 18, 2025 ยท View on GitHub
A REST API built with FastAPI that exposes the bias visualization capabilities of the OptiPFair library.
โจ Key Features
- ๐ Three Visualization Types - PCA, Mean Difference, and Heatmap analysis
- ๐ FastAPI Backend - High-performance REST API with automatic documentation
- ๐จ Gradio Frontend - User-friendly web interface for interactive analysis
- ๐ณ Docker Ready - One-command deployment with Docker Compose
- ๐ Online Demo - Try it instantly on Hugging Face Spaces
- ๐ง Production Ready - CI/CD, tests, and quality checks included
๐ฏ Quick Start
Option 1: Try Online (Zero Setup)
Option 2: Docker (Recommended)
git clone https://github.com/peremartra/optipfair-api.git
cd optipfair-api
docker-compose up -d
# Access at:
# Frontend: http://localhost:7860
# API Docs: http://localhost:8000/docs
Option 3: LLM-Assisted Development
Using ChatGPT, Claude, or other AI assistants? ๐ Give them the LLM Reference Manual to generate perfect integration code!
๐ Bias Analysis Examples
Visualizations obtained with OptiPFair analyzing the LLaMA 3.2-1B model for racial bias:
![]() | ![]() | ![]() |
|---|---|---|
| Mean Differences | Layer Activation | PCA Analysis |
๐ Overview
This microservice provides endpoints to generate and download visualizations of activation patterns in transformer-based LLMs (e.g., LLaMA) using the OptiPFair toolkit.
๐ฏ Main Endpoints
POST /visualize/pca- PCA visualization of activationsPOST /visualize/mean-diff- Mean activation difference across layersPOST /visualize/heatmap- Heatmap of activation differences- Response: Binary image (PNG, SVG, or PDF) showing the requested visualization
๐ง What's New in This Version
- โ CI/CD Pipeline - Automated testing and code quality checks
- โ Enhanced Validation - Better error handling and input validation
- โ Docker Optimization - Faster builds and improved caching
- โ Code Quality - Black, isort, and flake8 integration
โ๏ธ Requirements
- Python 3.11 or higher
- Git
- Docker & Docker Compose (recommended for easy deployment)
- Optional: Mac (Apple Silicon) or NVIDIA GPU for hardware acceleration (MPS/CUDA)
- Internet connection to download models from Hugging Face Hub
๐ณ Docker Deployment (Recommended)
The easiest way to run OptiPFair-API is using Docker Compose, which automatically handles all dependencies and services.
Quick Start with Docker
# 1. Clone the repository
git clone https://github.com/peremartra/optipfair-api.git
cd optipfair-api
# 2. Start the entire stack
docker-compose up -d
# 3. Access the application
# Frontend (Gradio): http://localhost:7860
# Backend API docs: http://localhost:8000/docs
Docker Commands
# Start services (detached mode)
docker-compose up -d
# View logs in real-time
docker-compose logs -f
# Stop services
docker-compose down
# Rebuild after code changes
docker-compose up --build
# Check service status
docker-compose ps
Docker Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ docker-compose โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโค
โ Backend โ Frontend โ
โ (FastAPI) โ (Gradio) โ
โ Port: 8000 โ Port: 7860 โ
โโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโ
Features:
- โ Automatic model caching - Downloads models once, reuses them
- โ Health monitoring - Services restart automatically if they fail
- โ Persistent storage - Model cache survives container restarts
- โ Production ready - Secure, non-root containers
- โ Cross-platform - Works on Mac, Linux, Windows
๐ Try Online (HF Spaces)
๐ Zero Installation Required!
You can try OptiPFair-API directly in your browser without any setup:
๐ OptipFair Bias Analyzer on Hugging Face Spaces
Features:
- โ Instant access - No installation required
- โ GPU acceleration - Faster model loading and processing
- โ Pre-loaded models - Ready to use immediately
- โ Full functionality - All three visualization types (PCA, Mean Diff, Heatmap)
- โ Public sharing - Share results with colleagues
Perfect for:
- ๐งช Quick testing of bias analysis concepts
- ๐ Learning and experimentation
- ๐ฏ Demos and presentations
- ๐ Comparing with local deployment
๐ Manual Installation (Alternative)
If you prefer to run without Docker:
# 1. Clone the repository
git clone https://github.com/peremartra/optipfair-api.git
cd optipfair-api
# 2. Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate
# 3. Install dependencies
pip install -r requirements-docker.txt
pip install pytest httpx # For testing
# 4. (Optional) Verify PyTorch MPS/CUDA support
python3 - <<'EOF'
import torch
print("MPS built?", torch.backends.mps.is_built(), "Available?", torch.backends.mps.is_available())
print("CUDA available?", torch.cuda.is_available())
EOF
Running Manually
# Terminal 1: Start FastAPI backend
uvicorn main:app --reload
# Terminal 2: Start Gradio frontend
python gradio_app.py
- Backend:
http://127.0.0.1:8000 - Frontend:
http://127.0.0.1:7860
๐งช Testing & Development
Run Tests
# Run the test suite
pytest tests/test_api_endpoints.py tests/test_validation.py -v
# Run with coverage
pytest --cov=. tests/
Code Quality
# Format code
black .
isort .
# Check code quality
flake8 .
# Run all quality checks (same as CI/CD)
black . && isort . && flake8 . && pytest tests/test_api_endpoints.py tests/test_validation.py
๐ API Documentation
Interactive API Docs
- Swagger UI:
http://localhost:8000/docs(when running locally) - ReDoc:
http://localhost:8000/redoc(alternative documentation)
For LLM-Assisted Development
๐ OptipFair-API LLM Reference Manual - Perfect for ChatGPT, Claude, and other AI assistants to generate integration code.
Health Check
GET /ping
Response:
{ "message": "pong" }
1. PCA Visualization
POST /visualize/pca
Generates a PCA scatter plot comparing activations for two prompts.
Request JSON Schema:
| Field | Type | Description |
|---|---|---|
model_name | string | Hugging Face model identifier (e.g. "meta-llama/Llama-3.2-1B"). |
prompt_pair | string[2] | Array of exactly two prompts to compare. |
layer_key | string | Exact layer name (e.g. "attention_output_layer_0"). |
highlight_diff | boolean | (Optional) Highlight differing tokens (default: true). |
figure_format | string | (Optional) Output format: png, svg, or pdf (default: png). |
pair_index | integer | (Optional) Index for naming output file (default: 0). |
output_dir | string | (Optional) Custom directory to save the image. |
Example using curl:
curl -X POST http://localhost:8000/visualize/pca \
-H "Content-Type: application/json" \
-d '{
"model_name": "meta-llama/Llama-3.2-1B",
"prompt_pair": [
"The white doctor examined the patient. The nurse thought",
"The Black doctor examined the patient. The nurse thought"
],
"layer_key": "attention_output_layer_0",
"figure_format": "png"
}' \
--output pca.png
Example using Python (requests):
import requests
payload = {
"model_name": "meta-llama/Llama-3.2-1B",
"prompt_pair": [
"The white doctor examined the patient. The nurse thought",
"The Black doctor examined the patient. The nurse thought"
],
"layer_key": "attention_output_layer_0",
"figure_format": "png"
}
resp = requests.post("http://localhost:8000/visualize/pca", json=payload)
resp.raise_for_status()
with open("pca_result.png", "wb") as f:
f.write(resp.content)
print("Saved PCA visualization to pca_result.png")
2. Mean Difference Visualization
POST /visualize/mean-diff
Generates a bar chart showing mean activation differences across layers for a specific component type.
Request JSON Schema:
| Field | Type | Description |
|---|---|---|
model_name | string | Hugging Face model identifier (e.g. "meta-llama/Llama-3.2-1B"). |
prompt_pair | string[2] | Array of exactly two prompts to compare. |
layer_type | string | Component type to analyze (e.g. "attention_output"). |
figure_format | string | (Optional) Output format (default: png). |
output_dir | string | (Optional) Custom directory to save the image. |
pair_index | integer | (Optional) Index for naming output file (default: 0). |
Valid layer types for layer_type:
mlp_output- Output of the MLP blockattention_output- Output of the attention mechanismgate_proj- Output of the gate projection in GLUup_proj- Output of the up projection in GLUdown_proj- Output of the down projection in GLUinput_norm- Output of the input normalization
Example using Python (requests):
import requests
payload = {
"model_name": "meta-llama/Llama-3.2-1B",
"prompt_pair": [
"The white doctor examined the patient. The nurse thought",
"The Black doctor examined the patient. The nurse thought"
],
"layer_type": "attention_output",
"figure_format": "png"
}
resp = requests.post("http://localhost:8000/visualize/mean-diff", json=payload)
resp.raise_for_status()
with open("mean_diff_result.png", "wb") as f:
f.write(resp.content)
print("Saved mean difference visualization to mean_diff_result.png")
3. Heatmap Visualization
POST /visualize/heatmap
Generates a heatmap showing activation differences for a specific layer.
Request JSON Schema:
| Field | Type | Description |
|---|---|---|
model_name | string | Hugging Face model identifier (e.g. "meta-llama/Llama-3.2-1B"). |
prompt_pair | string[2] | Array of exactly two prompts to compare. |
layer_key | string | Exact layer name (e.g. "attention_output_layer_0"). |
figure_format | string | (Optional) Output format (default: png). |
output_dir | string | (Optional) Custom directory to save the image. |
pair_index | integer | (Optional) Index for naming output file (default: 0). |
Example using Python (requests):
import requests
payload = {
"model_name": "meta-llama/Llama-3.2-1B",
"prompt_pair": [
"The white doctor examined the patient. The nurse thought",
"The Black doctor examined the patient. The nurse thought"
],
"layer_key": "attention_output_layer_2",
"figure_format": "png"
}
resp = requests.post("http://localhost:8000/visualize/heatmap", json=payload)
resp.raise_for_status()
with open("heatmap_result.png", "wb") as f:
f.write(resp.content)
print("Saved heatmap visualization to heatmap_result.png")
๐ Project Structure
optipfair-api/ # Repository root
โโโ main.py # FastAPI application entrypoint
โโโ gradio_app.py # Gradio frontend application
โโโ docker-compose.yml # Docker orchestration configuration
โโโ Dockerfile.backend # Backend container definition
โโโ Dockerfile.frontend # Frontend container definition
โโโ requirements-docker.txt # Optimized dependencies for containers
โโโ .github/ # CI/CD workflows
โ โโโ workflows/
โ โโโ test.yml # Automated testing
โ โโโ code-quality.yml # Code quality checks
โโโ routers/ # API route modules
โ โโโ visualize.py # Routes for /visualize/*
โโโ schemas/ # Pydantic request/response models
โ โโโ visualize.py # Request schemas for visualizations
โโโ utils/ # Internal utility functions
โ โโโ visualize_pca.py # Wrappers for optipfair visualization functions
โโโ tests/ # Test suite
โ โโโ test_api_endpoints.py # API endpoint tests
โ โโโ test_validation.py # Input validation tests
โโโ hf-spaces/ # Hugging Face Spaces deployment
โโโ README.md # Project documentation
๐ Deployment Options
1. ๐ Hugging Face Spaces (Try Now!)
Pros: Zero setup, GPU acceleration, instant access, public sharing
Cons: Limited to HF Spaces platform, shared resources
2. ๐ณ Docker (Recommended for Local/Production)
docker-compose up -d
Pros: Easy setup, automatic dependencies, production-ready, full control
Cons: Requires Docker installation
3. ๐ฑ Manual Installation
Traditional Python virtual environment setup.
Pros: Full control, native performance (MPS on Mac), development flexibility
Cons: Manual dependency management, longer setup time
๐ค LLM Integration Guide
For developers using AI assistants (ChatGPT, Claude, etc.) to generate code:
๐ OptipFair-API LLM Reference Manual
This comprehensive guide helps Large Language Models understand and generate correct code for OptipFair-API integration:
# Give this file to your favorite LLM assistant:
# "Please use the OptipFair-API LLM Reference Manual to help me integrate with this API"
What the manual includes:
- โ Complete API schemas - All endpoints with examples
- โ Request/response formats - Exact JSON structures
- โ Code generation templates - Python, JavaScript, curl examples
- โ Common patterns - Best practices for integration
- โ Error handling - How to handle validation and server errors
Perfect for:
- ๐ค AI-assisted development - ChatGPT, Claude, GitHub Copilot
- ๐ Quick integration - Generate client code in minutes
- ๐ ๏ธ Prototyping - Rapid API testing and exploration
- ๐ Learning - Understand API patterns and usage
๐ค Contributing
We welcome contributions! Please see our contributing guidelines:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and add tests
- Ensure code quality:
black . && isort . && flake8 . - Run tests:
pytest tests/ - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
Development Setup
git clone https://github.com/peremartra/optipfair-api.git
cd optipfair-api
python -m venv .venv
source .venv/bin/activate
pip install -r requirements-docker.txt
pip install black isort flake8 pytest httpx
๐ Citation
If you use OptipFair-API in your research or projects, please cite both the API and the underlying library:
OptipFair Library (Core Implementation)
@software{optipfair,
author = {Pere Martra},
title = {OptipFair: Structured Pruning and Bias Visualization for Large Language Models},
url = {https://github.com/peremartra/optipfair},
version = {0.1.3},
year = {2024}
}
OptipFair-API (REST Interface)
@software{optipfair_api,
author = {Pere Martra},
title = {OptipFair-API: REST API for LLM Bias Analysis and Visualization},
url = {https://github.com/peremartra/optipfair-api},
year = {2025}
}
๐ License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
๐ Acknowledgments
- OptipFair Library - Core bias visualization toolkit
- FastAPI - Modern web framework for building APIs
- Gradio - User-friendly machine learning interfaces
- Hugging Face - Transformers and model hosting platform
- LLM Reference Manual - AI-assisted development guide
โญ Star this repo | ๐ Report Bug | ๐ก Request Feature
Made with โค๏ธ by Pere Martra


