from datetime import datetime import gradio as gr import pandas as pd from datasets import load_dataset # Load the dataset ds = load_dataset("harshildarji/openlegaldata", "cases", split="main") df = pd.DataFrame(ds) # Precompute additional columns for filtering df["state"] = df["court"].apply( lambda x: x.get("state", "Unknown") if isinstance(x, dict) else "Unknown" ) df["court_name"] = df["court"].apply( lambda x: x.get("name", "Unknown") if isinstance(x, dict) else "Unknown" ) # Build unique lists for the filters state_list = sorted(df["state"].dropna().unique().tolist()) state_list.insert(0, "All") court_list = sorted(df["court_name"].dropna().unique().tolist()) court_list.insert(0, "All") def filter_cases(state, court, date_from, date_to): """ Filter the cases based on state, court name, and date range. Returns a DataFrame with selected columns. """ filtered = df.copy() if state and state != "All": filtered = filtered[filtered["state"] == state] if court and court != "All": filtered = filtered[filtered["court_name"] == court] if date_from: try: date_from_dt = datetime.strptime(date_from, "%Y-%m-%d") filtered = filtered[pd.to_datetime(filtered["date"]) >= date_from_dt] except Exception: pass if date_to: try: date_to_dt = datetime.strptime(date_to, "%Y-%m-%d") filtered = filtered[pd.to_datetime(filtered["date"]) <= date_to_dt] except Exception: pass return filtered[["id", "file_number", "date", "court_name", "type"]] def get_case_details(case_id): """Return an HTML formatted string with details for a given case id.""" try: case_id = int(case_id) except: return "
Invalid case ID
" case_row = df[df["id"] == case_id] if case_row.empty: return "Case not found
" case_data = case_row.iloc[0].to_dict() html = "ID: {case_data.get('id')}
" html += f"File Number: {case_data.get('file_number')}
" html += f"Date: {case_data.get('date')}
" # Display court information court_info = case_data.get("court", {}) html += f"Court: {court_info.get('name', 'N/A')} ({court_info.get('state', 'N/A')})
" html += f"Type: {case_data.get('type', 'N/A')}
" html += '{t}
" html += '{t}
" html += '{g}
" html += '{e}
" html += '