HITL Bias Detection - Quick Reference
API Endpoints Summary
| Endpoint |
Method |
Purpose |
Auth Required |
/api/v1/bias-detection-hitl/start-review |
POST |
Upload PDF & start review |
β |
/api/v1/bias-detection-hitl/approve-suggestion |
POST |
Approve/reject suggestion |
β |
/api/v1/bias-detection-hitl/regenerate-suggestion |
POST |
Get new suggestion |
β |
/api/v1/bias-detection-hitl/session/{session_id} |
GET |
Get session status |
β |
/api/v1/bias-detection-hitl/generate-pdf |
POST |
Generate final PDF |
β |
/api/v1/bias-detection-hitl/health |
GET |
Health check |
β |
Sentence Status Flow
ββββββββββββ
β pending β ββββ New biased sentence with suggestion
ββββββ¬ββββββ
β
ββββΊ approve βββββ
β β
ββββΊ reject β
β β
ββββββββββββββββββββ
βneeds_regenerationβ
ββββββ¬ββββββββββββββ
β
ββββΊ regenerate βββΊ pending (with new suggestion)
β
ββββΊ give up βββΊ manual intervention needed
β
β
ββββββββββββ
β approved β ββββ All approved? β Generate PDF
ββββββββββββ
Quick Start (cURL)
SESSION_ID=$(curl -X POST "http://localhost:8000/api/v1/bias-detection-hitl/start-review" \
-H "Authorization: Bearer $TOKEN" \
-F "file=@document.pdf" | jq -r '.session_id')
curl -X GET "http://localhost:8000/api/v1/bias-detection-hitl/session/$SESSION_ID" \
-H "Authorization: Bearer $TOKEN" | jq .
curl -X POST "http://localhost:8000/api/v1/bias-detection-hitl/approve-suggestion" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"session_id\":\"$SESSION_ID\",\"sentence_id\":\"SENTENCE_ID\",\"action\":\"approve\",\"approved_suggestion\":\"approved text here\"}"
curl -X POST "http://localhost:8000/api/v1/bias-detection-hitl/generate-pdf" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"session_id\":\"$SESSION_ID\"}" \
--output debiased.pdf
Key Response Fields
StartReviewResponse
{
session_id: "uuid",
total_sentences: 25,
biased_count: 5,
neutral_count: 20,
sentences: [...]
}
BiasReviewItem
{
sentence_id: "uuid",
original_sentence: "...",
is_biased: true,
category: "gender",
confidence: 0.92,
suggestion: "...",
approved_suggestion: null,
status: "pending"
}
SessionStatusResponse
{
status: "in_progress",
pending_count: 2,
approved_count: 22,
needs_regeneration_count: 1
}
Common Patterns
Pattern 1: Simple Approval Loop
session = start_review(pdf_file)
for sentence in session['sentences']:
if sentence['is_biased']:
approve_suggestion(
session_id=session['session_id'],
sentence_id=sentence['sentence_id'],
approved_text=sentence['suggestion']
)
generate_pdf(session['session_id'])
Pattern 2: Reject & Regenerate
approve_suggestion(
session_id=SESSION_ID,
sentence_id=SENTENCE_ID,
action='reject'
)
new_suggestion = regenerate_suggestion(
session_id=SESSION_ID,
sentence_id=SENTENCE_ID
)
approve_suggestion(
session_id=SESSION_ID,
sentence_id=SENTENCE_ID,
action='approve',
approved_text=new_suggestion
)
Pattern 3: Check Progress Before PDF
status = get_session_status(SESSION_ID)
if status['pending_count'] == 0 and status['needs_regeneration_count'] == 0:
pdf = generate_pdf(SESSION_ID)
else:
print(f"Still need to review {status['pending_count']} sentences")
Error Codes
| Code |
Error |
Solution |
| 400 |
Not all sentences reviewed |
Review all pending/rejected sentences |
| 404 |
Session not found |
Check session_id or start new session |
| 404 |
Sentence not found |
Verify sentence_id from session data |
| 500 |
PDF generation failed |
Check logs, may use fallback method |
| 500 |
LLM unavailable |
Verify MISTRAL_API_KEY is set |
| 503 |
Model not loaded |
Check bias detection model status |
Testing Checklist
Files Structure
api/
βββ routes/
β βββ bias_detection.py # Original bias detection
β βββ bias_detection_hitl.py # New HITL endpoints
βββ schemas.py # Data models (updated)
βββ main.py # Router registration (updated)
utility/
βββ pdf_processor.py # PDF extraction (existing)
βββ hitl_session_manager.py # Session management (new)
βββ pdf_regenerator.py # PDF regeneration (new)
docs/
βββ HITL_BIAS_DETECTION_GUIDE.md # Full documentation
βββ HITL_QUICK_REFERENCE.md # This file
Environment Variables
MISTRAL_API_KEY=your_mistral_api_key_here
SUPABASE_URL=your_supabase_url
SUPABASE_KEY=your_supabase_key
Deployment Notes
Before Deploying:
- Set MISTRAL_API_KEY environment variable
- Test with sample PDFs
- Consider adding rate limiting
- Monitor memory usage
- Plan session cleanup strategy
Production Considerations:
Support Resources
- Full Guide:
docs/HITL_BIAS_DETECTION_GUIDE.md
- Health Check:
GET /api/v1/bias-detection-hitl/health
- Original Bias Detection:
GET /api/v1/health
- API Docs:
http://localhost:8000/docs (when server running)