upload app and requirements
Browse files- app.py +155 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import pandas as pd
|
5 |
+
from datasets import load_dataset
|
6 |
+
|
7 |
+
# Load the dataset
|
8 |
+
ds = load_dataset("harshildarji/openlegaldata", "cases", split="main")
|
9 |
+
df = pd.DataFrame(ds)
|
10 |
+
|
11 |
+
# Precompute additional columns for filtering
|
12 |
+
df["state"] = df["court"].apply(
|
13 |
+
lambda x: x.get("state", "Unknown") if isinstance(x, dict) else "Unknown"
|
14 |
+
)
|
15 |
+
df["court_name"] = df["court"].apply(
|
16 |
+
lambda x: x.get("name", "Unknown") if isinstance(x, dict) else "Unknown"
|
17 |
+
)
|
18 |
+
|
19 |
+
# Build unique lists for the filters
|
20 |
+
state_list = sorted(df["state"].dropna().unique().tolist())
|
21 |
+
state_list.insert(0, "All")
|
22 |
+
court_list = sorted(df["court_name"].dropna().unique().tolist())
|
23 |
+
court_list.insert(0, "All")
|
24 |
+
|
25 |
+
|
26 |
+
def filter_cases(state, court, date_from, date_to):
|
27 |
+
"""
|
28 |
+
Filter the cases based on state, court name, and date range.
|
29 |
+
Returns a DataFrame with selected columns.
|
30 |
+
"""
|
31 |
+
filtered = df.copy()
|
32 |
+
if state and state != "All":
|
33 |
+
filtered = filtered[filtered["state"] == state]
|
34 |
+
if court and court != "All":
|
35 |
+
filtered = filtered[filtered["court_name"] == court]
|
36 |
+
if date_from:
|
37 |
+
try:
|
38 |
+
date_from_dt = datetime.strptime(date_from, "%Y-%m-%d")
|
39 |
+
filtered = filtered[pd.to_datetime(filtered["date"]) >= date_from_dt]
|
40 |
+
except Exception:
|
41 |
+
pass
|
42 |
+
if date_to:
|
43 |
+
try:
|
44 |
+
date_to_dt = datetime.strptime(date_to, "%Y-%m-%d")
|
45 |
+
filtered = filtered[pd.to_datetime(filtered["date"]) <= date_to_dt]
|
46 |
+
except Exception:
|
47 |
+
pass
|
48 |
+
|
49 |
+
return filtered[["id", "file_number", "date", "court_name", "type"]]
|
50 |
+
|
51 |
+
|
52 |
+
def get_case_details(case_id):
|
53 |
+
"""Return an HTML formatted string with details for a given case id."""
|
54 |
+
try:
|
55 |
+
case_id = int(case_id)
|
56 |
+
except:
|
57 |
+
return "<p style='color:red;'>Invalid case ID</p>"
|
58 |
+
case_row = df[df["id"] == case_id]
|
59 |
+
if case_row.empty:
|
60 |
+
return "<p style='color:red;'>Case not found</p>"
|
61 |
+
case_data = case_row.iloc[0].to_dict()
|
62 |
+
|
63 |
+
html = "<h2>Case Details</h2>"
|
64 |
+
html += f"<p><strong>ID:</strong> {case_data.get('id')}</p>"
|
65 |
+
html += f"<p><strong>File Number:</strong> {case_data.get('file_number')}</p>"
|
66 |
+
html += f"<p><strong>Date:</strong> {case_data.get('date')}</p>"
|
67 |
+
|
68 |
+
# Display court information
|
69 |
+
court_info = case_data.get("court", {})
|
70 |
+
html += f"<p><strong>Court:</strong> {court_info.get('name', 'N/A')} ({court_info.get('state', 'N/A')})</p>"
|
71 |
+
html += f"<p><strong>Type:</strong> {case_data.get('type', 'N/A')}</p>"
|
72 |
+
html += '<hr style="margin: 15px 0;">'
|
73 |
+
|
74 |
+
# Display tenor if available
|
75 |
+
tenors = case_data.get("tenor", [])
|
76 |
+
if tenors:
|
77 |
+
html += "<h3>Tenor</h3>"
|
78 |
+
for t in tenors:
|
79 |
+
html += f"<p>{t}</p>"
|
80 |
+
html += '<hr style="margin: 15px 0;">'
|
81 |
+
|
82 |
+
# Display tatbestand if available
|
83 |
+
tatbestand = case_data.get("tatbestand", [])
|
84 |
+
if tatbestand:
|
85 |
+
html += "<h3>Tatbestand</h3>"
|
86 |
+
for t in tatbestand:
|
87 |
+
html += f"<p>{t}</p>"
|
88 |
+
html += '<hr style="margin: 15px 0;">'
|
89 |
+
|
90 |
+
# Display Gründe if available
|
91 |
+
gründe = case_data.get("gründe", [])
|
92 |
+
if gründe:
|
93 |
+
html += "<h3>Gründe</h3>"
|
94 |
+
for g in gründe:
|
95 |
+
html += f"<p>{g}</p>"
|
96 |
+
html += '<hr style="margin: 15px 0;">'
|
97 |
+
|
98 |
+
# Display Entscheidungsgründe if available
|
99 |
+
entscheidungsgründe = case_data.get("entscheidungsgründe", [])
|
100 |
+
if entscheidungsgründe:
|
101 |
+
html += "<h3>Entscheidungsgründe</h3>"
|
102 |
+
for e in entscheidungsgründe:
|
103 |
+
html += f"<p>{e}</p>"
|
104 |
+
html += '<hr style="margin: 15px 0;">'
|
105 |
+
|
106 |
+
return html
|
107 |
+
|
108 |
+
|
109 |
+
# Build multi-tabs interface
|
110 |
+
with gr.Blocks(title="German Legal Case Viewer", fill_width=True) as demo:
|
111 |
+
gr.Markdown("# German Legal Case Viewer")
|
112 |
+
gr.Markdown(
|
113 |
+
"Explore case information from the processed [Open Legal Data dataset](https://huggingface.co/datasets/harshildarji/openlegaldata)."
|
114 |
+
)
|
115 |
+
|
116 |
+
with gr.Tabs():
|
117 |
+
# First Tab - Browse and filter cases
|
118 |
+
with gr.Tab("Browse Cases"):
|
119 |
+
gr.Markdown("## Filter Cases")
|
120 |
+
with gr.Row():
|
121 |
+
state_input = gr.Dropdown(
|
122 |
+
choices=state_list, label="State", value="All"
|
123 |
+
)
|
124 |
+
court_input = gr.Dropdown(
|
125 |
+
choices=court_list, label="Court", value="All"
|
126 |
+
)
|
127 |
+
date_from_input = gr.Textbox(
|
128 |
+
label="From Date (YYYY-MM-DD)", placeholder="2022-01-01"
|
129 |
+
)
|
130 |
+
date_to_input = gr.Textbox(
|
131 |
+
label="To Date (YYYY-MM-DD)", placeholder="2022-12-31"
|
132 |
+
)
|
133 |
+
filter_button = gr.Button("Apply Filters")
|
134 |
+
output_table = gr.DataFrame(label="Cases Overview")
|
135 |
+
filter_button.click(
|
136 |
+
fn=filter_cases,
|
137 |
+
inputs=[state_input, court_input, date_from_input, date_to_input],
|
138 |
+
outputs=output_table,
|
139 |
+
)
|
140 |
+
|
141 |
+
# Second Tab - Show details of a specific case
|
142 |
+
with gr.Tab("Case Details"):
|
143 |
+
with gr.Row():
|
144 |
+
case_id_input = gr.Textbox(
|
145 |
+
label="Enter Case ID", placeholder="e.g., 346915"
|
146 |
+
)
|
147 |
+
details_button = gr.Button("Get Details")
|
148 |
+
case_details_output = gr.HTML(label="Case Details")
|
149 |
+
details_button.click(
|
150 |
+
fn=get_case_details,
|
151 |
+
inputs=case_id_input,
|
152 |
+
outputs=case_details_output,
|
153 |
+
)
|
154 |
+
|
155 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
datasets
|
3 |
+
pandas
|