Rami Nasser
commited on
Commit
·
b27cd71
1
Parent(s):
f3b8201
remove years
Browse files- app.py +17 -48
- j_year.json +0 -323
- utils/__init__.py +2 -3
app.py
CHANGED
@@ -1,19 +1,23 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
from utils import *
|
4 |
from datasets import load_dataset
|
5 |
import json
|
6 |
|
7 |
pipe = pipeline(model="raminass/british", top_k=2, padding=True, truncation=True)
|
8 |
-
df = pd.read_csv(
|
9 |
choices = []
|
10 |
for index, row in df.iterrows():
|
11 |
choices.append((f"""{row["case"]}""", [row["text"], row["author"]]))
|
12 |
|
|
|
13 |
# https://www.gradio.app/guides/controlling-layout
|
14 |
-
def greet(opinion
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
return result[0]
|
19 |
|
@@ -22,73 +26,38 @@ def set_input(drop):
|
|
22 |
return drop[0], drop[1], gr.Slider(visible=True)
|
23 |
|
24 |
|
25 |
-
def update_year(year):
|
26 |
-
return gr.CheckboxGroup(
|
27 |
-
judges_by_year[year],
|
28 |
-
value=judges_by_year[year],
|
29 |
-
label="Select Justices",
|
30 |
-
)
|
31 |
-
|
32 |
-
|
33 |
with gr.Blocks() as demo:
|
34 |
with gr.Row():
|
35 |
with gr.Column(scale=2):
|
36 |
drop = gr.Dropdown(
|
37 |
choices=sorted(choices),
|
38 |
-
label="List of
|
39 |
-
info="Select a
|
40 |
-
)
|
41 |
-
year = gr.Slider(
|
42 |
-
1994,
|
43 |
-
2023,
|
44 |
-
step=1,
|
45 |
-
label="Year",
|
46 |
-
info="Select the year of the opinion if you manually paste the opinion below",
|
47 |
-
)
|
48 |
-
exc_judg = gr.CheckboxGroup(
|
49 |
-
judges_by_year[year.value],
|
50 |
-
value=judges_by_year[year.value],
|
51 |
-
label="Select Justices",
|
52 |
-
info="Select justices to consider in prediction",
|
53 |
)
|
54 |
|
55 |
opinion = gr.Textbox(
|
56 |
-
label="Opinion",
|
|
|
57 |
)
|
58 |
with gr.Column(scale=1):
|
59 |
with gr.Row():
|
60 |
clear_btn = gr.Button("Clear")
|
61 |
greet_btn = gr.Button("Predict")
|
62 |
-
op_level =
|
63 |
-
num_top_classes=9, label="Predicted author of opinion"
|
64 |
-
)
|
65 |
|
66 |
-
|
67 |
-
update_year,
|
68 |
-
inputs=[year],
|
69 |
-
outputs=[exc_judg],
|
70 |
-
)
|
71 |
-
year.change(
|
72 |
-
update_year,
|
73 |
-
inputs=[year],
|
74 |
-
outputs=[exc_judg],
|
75 |
-
)
|
76 |
-
drop.select(set_input, inputs=drop, outputs=[opinion, year, year])
|
77 |
|
78 |
greet_btn.click(
|
79 |
fn=greet,
|
80 |
-
inputs=[opinion
|
81 |
outputs=[op_level],
|
82 |
)
|
83 |
|
84 |
clear_btn.click(
|
85 |
fn=lambda: [None, 1994, gr.Slider(visible=True), None, None],
|
86 |
-
outputs=[opinion,
|
87 |
)
|
88 |
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
-
demo.launch(
|
92 |
-
auth=("sc2024", "sc2024"),
|
93 |
-
auth_message="To request access, please email [email protected]",
|
94 |
-
)
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio.components import Label, Textbox
|
3 |
+
|
4 |
from transformers import pipeline
|
5 |
from utils import *
|
6 |
from datasets import load_dataset
|
7 |
import json
|
8 |
|
9 |
pipe = pipeline(model="raminass/british", top_k=2, padding=True, truncation=True)
|
10 |
+
df = pd.read_csv("data.csv", sep="\t")
|
11 |
choices = []
|
12 |
for index, row in df.iterrows():
|
13 |
choices.append((f"""{row["case"]}""", [row["text"], row["author"]]))
|
14 |
|
15 |
+
|
16 |
# https://www.gradio.app/guides/controlling-layout
|
17 |
+
def greet(opinion):
|
18 |
+
opinion = opinion.replace("(", "").replace(")", "")
|
19 |
+
chunks = chunk_data(opinion)["text"].to_list()
|
20 |
+
result = average_text(chunks, pipe)
|
21 |
|
22 |
return result[0]
|
23 |
|
|
|
26 |
return drop[0], drop[1], gr.Slider(visible=True)
|
27 |
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
with gr.Blocks() as demo:
|
30 |
with gr.Row():
|
31 |
with gr.Column(scale=2):
|
32 |
drop = gr.Dropdown(
|
33 |
choices=sorted(choices),
|
34 |
+
label="List of Cases",
|
35 |
+
info="Select a case from the dropdown menu and press the Predict Button",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
)
|
37 |
|
38 |
opinion = gr.Textbox(
|
39 |
+
label="Opinion",
|
40 |
+
info="Paste opinion text here and press the Predict Button",
|
41 |
)
|
42 |
with gr.Column(scale=1):
|
43 |
with gr.Row():
|
44 |
clear_btn = gr.Button("Clear")
|
45 |
greet_btn = gr.Button("Predict")
|
46 |
+
op_level = Label(num_top_classes=9, label="Predicted author of opinion")
|
|
|
|
|
47 |
|
48 |
+
drop.select(set_input, inputs=drop, outputs=[opinion])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
greet_btn.click(
|
51 |
fn=greet,
|
52 |
+
inputs=[opinion],
|
53 |
outputs=[op_level],
|
54 |
)
|
55 |
|
56 |
clear_btn.click(
|
57 |
fn=lambda: [None, 1994, gr.Slider(visible=True), None, None],
|
58 |
+
outputs=[opinion, drop, op_level],
|
59 |
)
|
60 |
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
+
demo.launch()
|
|
|
|
|
|
j_year.json
DELETED
@@ -1,323 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"1994": [
|
3 |
-
"Rehnquist",
|
4 |
-
"Stevens",
|
5 |
-
"Scalia",
|
6 |
-
"O'Connor",
|
7 |
-
"Ginsburg",
|
8 |
-
"Thomas",
|
9 |
-
"Souter",
|
10 |
-
"Breyer",
|
11 |
-
"Kennedy"
|
12 |
-
],
|
13 |
-
"1995": [
|
14 |
-
"Scalia",
|
15 |
-
"Stevens",
|
16 |
-
"Ginsburg",
|
17 |
-
"Breyer",
|
18 |
-
"Souter",
|
19 |
-
"O'Connor",
|
20 |
-
"Thomas",
|
21 |
-
"Kennedy",
|
22 |
-
"Rehnquist"
|
23 |
-
],
|
24 |
-
"1996": [
|
25 |
-
"Scalia",
|
26 |
-
"Stevens",
|
27 |
-
"Rehnquist",
|
28 |
-
"Souter",
|
29 |
-
"O'Connor",
|
30 |
-
"Kennedy",
|
31 |
-
"Breyer",
|
32 |
-
"Ginsburg",
|
33 |
-
"Thomas"
|
34 |
-
],
|
35 |
-
"1997": [
|
36 |
-
"Ginsburg",
|
37 |
-
"Stevens",
|
38 |
-
"Souter",
|
39 |
-
"Thomas",
|
40 |
-
"Rehnquist",
|
41 |
-
"Breyer",
|
42 |
-
"Scalia",
|
43 |
-
"O'Connor",
|
44 |
-
"Kennedy"
|
45 |
-
],
|
46 |
-
"1998": [
|
47 |
-
"Scalia",
|
48 |
-
"Thomas",
|
49 |
-
"Stevens",
|
50 |
-
"Breyer",
|
51 |
-
"Rehnquist",
|
52 |
-
"Souter",
|
53 |
-
"Kennedy",
|
54 |
-
"O'Connor",
|
55 |
-
"Ginsburg"
|
56 |
-
],
|
57 |
-
"1999": [
|
58 |
-
"Breyer",
|
59 |
-
"Rehnquist",
|
60 |
-
"Souter",
|
61 |
-
"Thomas",
|
62 |
-
"Stevens",
|
63 |
-
"Kennedy",
|
64 |
-
"Scalia",
|
65 |
-
"Ginsburg",
|
66 |
-
"O'Connor"
|
67 |
-
],
|
68 |
-
"2000": [
|
69 |
-
"O'Connor",
|
70 |
-
"Breyer",
|
71 |
-
"Ginsburg",
|
72 |
-
"Kennedy",
|
73 |
-
"Rehnquist",
|
74 |
-
"Scalia",
|
75 |
-
"Souter",
|
76 |
-
"Stevens",
|
77 |
-
"Thomas"
|
78 |
-
],
|
79 |
-
"2001": [
|
80 |
-
"Stevens",
|
81 |
-
"Kennedy",
|
82 |
-
"Thomas",
|
83 |
-
"Scalia",
|
84 |
-
"Breyer",
|
85 |
-
"Ginsburg",
|
86 |
-
"Rehnquist",
|
87 |
-
"O'Connor",
|
88 |
-
"Souter"
|
89 |
-
],
|
90 |
-
"2002": [
|
91 |
-
"Rehnquist",
|
92 |
-
"Stevens",
|
93 |
-
"Thomas",
|
94 |
-
"Souter",
|
95 |
-
"Breyer",
|
96 |
-
"Ginsburg",
|
97 |
-
"Scalia",
|
98 |
-
"Kennedy",
|
99 |
-
"O'Connor"
|
100 |
-
],
|
101 |
-
"2003": [
|
102 |
-
"Scalia",
|
103 |
-
"Souter",
|
104 |
-
"Kennedy",
|
105 |
-
"Stevens",
|
106 |
-
"Thomas",
|
107 |
-
"O'Connor",
|
108 |
-
"Breyer",
|
109 |
-
"Rehnquist",
|
110 |
-
"Ginsburg"
|
111 |
-
],
|
112 |
-
"2004": [
|
113 |
-
"Ginsburg",
|
114 |
-
"Stevens",
|
115 |
-
"Kennedy",
|
116 |
-
"Thomas",
|
117 |
-
"Scalia",
|
118 |
-
"Breyer",
|
119 |
-
"Souter",
|
120 |
-
"Rehnquist",
|
121 |
-
"O'Connor"
|
122 |
-
],
|
123 |
-
"2005": [
|
124 |
-
"Ginsburg",
|
125 |
-
"Thomas",
|
126 |
-
"Stevens",
|
127 |
-
"Breyer",
|
128 |
-
"Souter",
|
129 |
-
"Kennedy",
|
130 |
-
"Roberts",
|
131 |
-
"Scalia",
|
132 |
-
"O'Connor",
|
133 |
-
"Rehnquist"
|
134 |
-
],
|
135 |
-
"2006": [
|
136 |
-
"Breyer",
|
137 |
-
"Stevens",
|
138 |
-
"Souter",
|
139 |
-
"Thomas",
|
140 |
-
"Ginsburg",
|
141 |
-
"Scalia",
|
142 |
-
"Alito",
|
143 |
-
"Kennedy",
|
144 |
-
"Roberts",
|
145 |
-
"O'Connor"
|
146 |
-
],
|
147 |
-
"2007": [
|
148 |
-
"Ginsburg",
|
149 |
-
"Scalia",
|
150 |
-
"Thomas",
|
151 |
-
"Alito",
|
152 |
-
"Stevens",
|
153 |
-
"Kennedy",
|
154 |
-
"Breyer",
|
155 |
-
"Souter",
|
156 |
-
"Roberts"
|
157 |
-
],
|
158 |
-
"2008": [
|
159 |
-
"Thomas",
|
160 |
-
"Breyer",
|
161 |
-
"Ginsburg",
|
162 |
-
"Kennedy",
|
163 |
-
"Roberts",
|
164 |
-
"Scalia",
|
165 |
-
"Stevens",
|
166 |
-
"Souter",
|
167 |
-
"Alito"
|
168 |
-
],
|
169 |
-
"2009": [
|
170 |
-
"Scalia",
|
171 |
-
"Kennedy",
|
172 |
-
"Breyer",
|
173 |
-
"Ginsburg",
|
174 |
-
"Thomas",
|
175 |
-
"Stevens",
|
176 |
-
"Alito",
|
177 |
-
"Sotomayor",
|
178 |
-
"Roberts",
|
179 |
-
"Souter"
|
180 |
-
],
|
181 |
-
"2010": [
|
182 |
-
"Breyer",
|
183 |
-
"Scalia",
|
184 |
-
"Kennedy",
|
185 |
-
"Stevens",
|
186 |
-
"Sotomayor",
|
187 |
-
"Alito",
|
188 |
-
"Ginsburg",
|
189 |
-
"Thomas",
|
190 |
-
"Roberts"
|
191 |
-
],
|
192 |
-
"2011": [
|
193 |
-
"Kennedy",
|
194 |
-
"Breyer",
|
195 |
-
"Scalia",
|
196 |
-
"Thomas",
|
197 |
-
"Ginsburg",
|
198 |
-
"Sotomayor",
|
199 |
-
"Alito",
|
200 |
-
"Kagan",
|
201 |
-
"Roberts"
|
202 |
-
],
|
203 |
-
"2012": [
|
204 |
-
"Kennedy",
|
205 |
-
"Ginsburg",
|
206 |
-
"Breyer",
|
207 |
-
"Roberts",
|
208 |
-
"Alito",
|
209 |
-
"Thomas",
|
210 |
-
"Sotomayor",
|
211 |
-
"Scalia",
|
212 |
-
"Kagan"
|
213 |
-
],
|
214 |
-
"2013": [
|
215 |
-
"Ginsburg",
|
216 |
-
"Alito",
|
217 |
-
"Breyer",
|
218 |
-
"Kennedy",
|
219 |
-
"Scalia",
|
220 |
-
"Roberts",
|
221 |
-
"Thomas",
|
222 |
-
"Sotomayor",
|
223 |
-
"Kagan"
|
224 |
-
],
|
225 |
-
"2014": [
|
226 |
-
"Roberts",
|
227 |
-
"Thomas",
|
228 |
-
"Breyer",
|
229 |
-
"Kagan",
|
230 |
-
"Ginsburg",
|
231 |
-
"Scalia",
|
232 |
-
"Sotomayor",
|
233 |
-
"Kennedy",
|
234 |
-
"Alito"
|
235 |
-
],
|
236 |
-
"2015": [
|
237 |
-
"Breyer",
|
238 |
-
"Sotomayor",
|
239 |
-
"Roberts",
|
240 |
-
"Thomas",
|
241 |
-
"Alito",
|
242 |
-
"Scalia",
|
243 |
-
"Kagan",
|
244 |
-
"Ginsburg",
|
245 |
-
"Kennedy"
|
246 |
-
],
|
247 |
-
"2016": [
|
248 |
-
"Ginsburg",
|
249 |
-
"Thomas",
|
250 |
-
"Roberts",
|
251 |
-
"Alito",
|
252 |
-
"Breyer",
|
253 |
-
"Sotomayor",
|
254 |
-
"Kagan",
|
255 |
-
"Kennedy",
|
256 |
-
"Scalia"
|
257 |
-
],
|
258 |
-
"2017": [
|
259 |
-
"Ginsburg",
|
260 |
-
"Thomas",
|
261 |
-
"Sotomayor",
|
262 |
-
"Kagan",
|
263 |
-
"Kennedy",
|
264 |
-
"Alito",
|
265 |
-
"Breyer",
|
266 |
-
"Roberts"
|
267 |
-
],
|
268 |
-
"2018": [
|
269 |
-
"Kennedy",
|
270 |
-
"Kagan",
|
271 |
-
"Ginsburg",
|
272 |
-
"Thomas",
|
273 |
-
"Breyer",
|
274 |
-
"Alito",
|
275 |
-
"Sotomayor",
|
276 |
-
"Roberts"
|
277 |
-
],
|
278 |
-
"2019": [
|
279 |
-
"Thomas",
|
280 |
-
"Ginsburg",
|
281 |
-
"Alito",
|
282 |
-
"Breyer",
|
283 |
-
"Sotomayor",
|
284 |
-
"Kagan",
|
285 |
-
"Roberts"
|
286 |
-
],
|
287 |
-
"2020": [
|
288 |
-
"Alito",
|
289 |
-
"Kagan",
|
290 |
-
"Breyer",
|
291 |
-
"Sotomayor",
|
292 |
-
"Ginsburg",
|
293 |
-
"Thomas",
|
294 |
-
"Roberts",
|
295 |
-
"Gorsuch"
|
296 |
-
],
|
297 |
-
"2021": [
|
298 |
-
"Kagan",
|
299 |
-
"Breyer",
|
300 |
-
"Sotomayor",
|
301 |
-
"Gorsuch",
|
302 |
-
"Roberts",
|
303 |
-
"Thomas",
|
304 |
-
"Alito"
|
305 |
-
],
|
306 |
-
"2022": [
|
307 |
-
"Kagan",
|
308 |
-
"Breyer",
|
309 |
-
"Sotomayor",
|
310 |
-
"Gorsuch",
|
311 |
-
"Roberts",
|
312 |
-
"Thomas",
|
313 |
-
"Alito"
|
314 |
-
],
|
315 |
-
"2023": [
|
316 |
-
"Kagan",
|
317 |
-
"Sotomayor",
|
318 |
-
"Gorsuch",
|
319 |
-
"Roberts",
|
320 |
-
"Thomas",
|
321 |
-
"Alito"
|
322 |
-
]
|
323 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils/__init__.py
CHANGED
@@ -16,14 +16,13 @@ def normaliz_dict(d, target=1.0):
|
|
16 |
return {key: value * factor for key, value in d.items()}
|
17 |
|
18 |
|
19 |
-
def average_text(text, model
|
20 |
result = model(text)
|
21 |
new_res = []
|
22 |
for d in result:
|
23 |
p = {}
|
24 |
for dicts in d:
|
25 |
-
|
26 |
-
p[dicts["label"]] = dicts["score"]
|
27 |
p = normaliz_dict(p)
|
28 |
new_res.append(p)
|
29 |
|
|
|
16 |
return {key: value * factor for key, value in d.items()}
|
17 |
|
18 |
|
19 |
+
def average_text(text, model):
|
20 |
result = model(text)
|
21 |
new_res = []
|
22 |
for d in result:
|
23 |
p = {}
|
24 |
for dicts in d:
|
25 |
+
p[dicts["label"]] = dicts["score"]
|
|
|
26 |
p = normaliz_dict(p)
|
27 |
new_res.append(p)
|
28 |
|