-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge_analysis_api.py
197 lines (164 loc) · 7.08 KB
/
judge_analysis_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
Judge Analysis API Endpoints
This module provides API endpoints for the judge analysis functionality.
It interfaces with the judge_analysis.py module to analyze judges based on
their opinions.
"""
import logging
import os
from typing import Dict, List, Optional, Any
from fastapi import APIRouter, Depends, HTTPException, Query, Body
from pydantic import BaseModel, Field
from sqlalchemy.orm import Session
from database import get_db
from judge_analysis import JudgeProfiler
from auth import get_current_user
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Initialize the judge profiler
MODEL_DIR = os.getenv("MODEL_DIR", "./models")
try:
judge_profiler = JudgeProfiler(model_dir=MODEL_DIR)
logger.info("Judge profiler initialized successfully")
except (ImportError, RuntimeError) as e:
logger.error(f"Failed to initialize judge profiler: {str(e)}")
judge_profiler = None
# Create router
router = APIRouter(
prefix="/api/judge-analysis",
tags=["judge-analysis"],
responses={404: {"description": "Not found"}},
)
# Pydantic models for request/response validation
class OpinionData(BaseModel):
"""Model for opinion data."""
text: str
outcome: str = Field(..., description="Outcome of the case (e.g., 'affirmed', 'reversed', 'granted', 'denied')")
case_type: str = Field(..., description="Type of case (e.g., 'civil', 'criminal', 'administrative')")
date_filed: Optional[str] = None
class JudgeAnalysisRequest(BaseModel):
"""Model for judge analysis request."""
judge_id: str
opinions: List[OpinionData]
class JudgeProfile(BaseModel):
"""Model for judge profile response."""
judge_id: str
statistics: Dict[str, Any]
writing_style: Dict[str, Any]
topics: Dict[str, Any]
ruling_patterns: Dict[str, Any]
@router.post("/analyze", response_model=JudgeProfile)
async def analyze_judge(
request: JudgeAnalysisRequest,
current_user: Dict = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""
Analyze a judge based on their opinions.
This endpoint processes a collection of opinions authored by a judge
and returns a comprehensive analysis of the judge's writing style,
topics they discuss, and patterns in their rulings.
Args:
request: Judge analysis request data
current_user: Current authenticated user
db: Database session
Returns:
JudgeProfile: Comprehensive profile of the judge
"""
if judge_profiler is None:
raise HTTPException(status_code=503, detail="Judge analysis service unavailable. Required dependencies missing.")
try:
logger.info(f"Analyzing judge {request.judge_id} with {len(request.opinions)} opinions")
# Convert opinions to the format expected by JudgeProfiler
opinions_data = [opinion.dict() for opinion in request.opinions]
# Analyze judge
profile = judge_profiler.analyze_judge(request.judge_id, opinions_data)
# Save results to database (optional)
# This would be implemented with SQLAlchemy
return profile
except Exception as e:
logger.error(f"Error analyzing judge: {str(e)}")
raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")
@router.get("/profile/{judge_id}", response_model=JudgeProfile)
async def get_judge_profile(
judge_id: str,
current_user: Dict = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""
Get the profile of a judge by ID.
This endpoint retrieves a previously generated judge profile from the database.
If no profile exists, it returns a 404 error.
Args:
judge_id: ID of the judge
current_user: Current authenticated user
db: Database session
Returns:
JudgeProfile: Profile of the judge
"""
if judge_profiler is None:
raise HTTPException(status_code=503, detail="Judge analysis service unavailable. Required dependencies missing.")
try:
# Here we would normally fetch from database
# For now, we'll just return an error because we haven't stored anything
raise HTTPException(status_code=404, detail=f"Judge profile for ID {judge_id} not found")
except Exception as e:
logger.error(f"Error fetching judge profile: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to fetch profile: {str(e)}")
@router.get("/search", response_model=List[Dict[str, Any]])
async def search_judges(
query: str = Query(..., description="Search query for judges"),
limit: int = Query(10, description="Maximum number of results to return"),
current_user: Dict = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""
Search for judges matching the query.
This endpoint searches for judges whose names or other properties
match the provided query string.
Args:
query: Search query
limit: Maximum number of results to return
current_user: Current authenticated user
db: Database session
Returns:
List[Dict]: List of matching judges
"""
if judge_profiler is None:
raise HTTPException(status_code=503, detail="Judge analysis service unavailable. Required dependencies missing.")
try:
# Here we would normally search in database
# For demonstration, return empty list
return []
except Exception as e:
logger.error(f"Error searching judges: {str(e)}")
raise HTTPException(status_code=500, detail=f"Search failed: {str(e)}")
@router.get("/similar-judges/{judge_id}", response_model=List[Dict[str, Any]])
async def get_similar_judges(
judge_id: str,
limit: int = Query(5, description="Maximum number of results to return"),
current_user: Dict = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""
Find judges similar to the specified judge.
This endpoint identifies judges with similar ruling patterns,
writing styles, or topic distributions to the specified judge.
Args:
judge_id: ID of the reference judge
limit: Maximum number of similar judges to return
current_user: Current authenticated user
db: Database session
Returns:
List[Dict]: List of similar judges with similarity scores
"""
if judge_profiler is None:
raise HTTPException(status_code=503, detail="Judge analysis service unavailable. Required dependencies missing.")
try:
# This would require additional logic to compute similarity
# For demonstration, return empty list
return []
except Exception as e:
logger.error(f"Error finding similar judges: {str(e)}")
raise HTTPException(status_code=500, detail=f"Similarity search failed: {str(e)}")