Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from datasets import ClassLabel
|
3 |
+
from datasets import load_dataset
|
4 |
+
import random
|
5 |
+
import pandas as pd
|
6 |
+
from huggingface_hub import login
|
7 |
+
|
8 |
+
def remove_space(example):
|
9 |
+
'''
|
10 |
+
移除資料集當中「犯罪事實」欄 (Corpus-Delicti) 當中作為斷詞字元的空白字元,以及每句開頭的「ㄧ、」。
|
11 |
+
並且在文章的開頭跟結尾加入 bos_token = '<s>', eos_token = '</s>'
|
12 |
+
'''
|
13 |
+
return {'Corpus-Delicti': example['Corpus-Delicti'].replace(" ", "").split('一、')[1]}
|
14 |
+
|
15 |
+
def download_file(content, filename):
|
16 |
+
# print(filename)
|
17 |
+
# print(content)
|
18 |
+
with open(filename, "w", encoding="utf-8") as f:
|
19 |
+
f.write(content)
|
20 |
+
|
21 |
+
def random_elements(dataset, num_examples=5):
|
22 |
+
assert num_examples <= len(dataset), "Can't pick more elements than there are in the dataset."
|
23 |
+
picks = []
|
24 |
+
for _ in range(num_examples):
|
25 |
+
pick = random.randint(0, len(dataset)-1)
|
26 |
+
while pick in picks:
|
27 |
+
pick = random.randint(0, len(dataset)-1)
|
28 |
+
picks.append(pick)
|
29 |
+
|
30 |
+
df = pd.DataFrame(dataset[picks])
|
31 |
+
for column, typ in dataset.features.items():
|
32 |
+
if isinstance(typ, ClassLabel):
|
33 |
+
df[column] = df[column].transform(lambda i: typ.names[i])
|
34 |
+
return df
|
35 |
+
|
36 |
+
def random_next(num_examples=5):
|
37 |
+
random_selected = random_elements(dataset["train"], num_examples=num_examples)
|
38 |
+
court_name = random_selected['Court'][0]
|
39 |
+
case_no = random_selected['CaseNo'][0]
|
40 |
+
crime_descrip = random_selected['Corpus-Delicti'][0]
|
41 |
+
title = court_name + "_" + case_no
|
42 |
+
data_tuple = (court_name, case_no, crime_descrip, title)
|
43 |
+
return data_tuple
|
44 |
+
|
45 |
+
def gen_template(crime_descrip, element, tag):
|
46 |
+
INTRO_BLURB = "The following is a description of the crime in the verdict. Write a response for the element of crime and its tag that appropriately completes the request."
|
47 |
+
DESCRIPT_KEY = "### Description:"
|
48 |
+
ELEMENT_KEY = "### Element:"
|
49 |
+
TAG_KEY = "### Tag:"
|
50 |
+
END_KEY = "### End"
|
51 |
+
# assert tag == None, "未選取構成要件要素標籤"
|
52 |
+
try:
|
53 |
+
tag_name = tag.split(",")[1].strip(")").strip().strip("'")
|
54 |
+
except IndexError: # 防呆用的。如果什麼資料都沒填就按下按鈕,就會觸發以下程式碼,並傳回空樣板。
|
55 |
+
# 改為調適 Alpaca 格式的資料
|
56 |
+
blurb = f"{INTRO_BLURB}\n"
|
57 |
+
descript = f"{DESCRIPT_KEY}\n{crime_descrip}\n"
|
58 |
+
element = f"{ELEMENT_KEY}\n{element}\n" if element else f"{ELEMENT_KEY}\n<未填寫構成要件要素>\n"
|
59 |
+
tag = f"{TAG_KEY}\n{tag_name}\n" if tag else f"{TAG_KEY}\n<未選取構成要件要素標籤>\n"
|
60 |
+
end = f"{END_KEY}"
|
61 |
+
template = blurb + '\n' + descript + '\n' + element + '\n' + tag + '\n' + end
|
62 |
+
return template
|
63 |
+
blurb = f"{INTRO_BLURB}\n"
|
64 |
+
# 改為調適 Alpaca 格式的資料
|
65 |
+
descript = f"{DESCRIPT_KEY}\n{crime_descrip}\n"
|
66 |
+
element = f"{ELEMENT_KEY}\n{element}\n" if element else f"{ELEMENT_KEY}\n<未填寫構成要件要素>\n"
|
67 |
+
tag = f"{TAG_KEY}\n{tag_name}\n" if tag else f"{TAG_KEY}\n<未選取構成要件要素標籤>\n"
|
68 |
+
end = f"{END_KEY}"
|
69 |
+
template = blurb + '\n' + descript + '\n' + element + '\n' + tag + '\n' + end
|
70 |
+
return template
|
71 |
+
|
72 |
+
#random_selected = random_elements(dataset["train"])
|
73 |
+
random_selected = random_next()
|
74 |
+
court_name = random_selected[0]
|
75 |
+
case_no = random_selected[1]
|
76 |
+
crime_descrip = random_selected[2]
|
77 |
+
title = random_selected[3]
|
78 |
+
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown(
|
81 |
+
"""
|
82 |
+
<h1 style="text-align: center;">Legal Document Annotation</h1>
|
83 |
+
""")
|
84 |
+
with gr.Row():
|
85 |
+
with gr.Column(): # 犯罪事實段
|
86 |
+
# court_name = random_selected[0]
|
87 |
+
# case_no = random_selected[1]
|
88 |
+
# crime_descrip = random_selected[2]
|
89 |
+
with gr.Row(): # 抬頭段
|
90 |
+
# courtName = gr.Label(label='法院名稱', value=court_name)
|
91 |
+
# caseNo = gr.Label(label='案號', value=case_no)
|
92 |
+
title = gr.components.Textbox(label='案號',value=title)
|
93 |
+
prompt = gr.components.Textbox(lines=5, label='犯罪事實',value=crime_descrip)
|
94 |
+
with gr.Row():
|
95 |
+
with gr.Column():
|
96 |
+
btn = gr.Button("隨機選擇")
|
97 |
+
# gr.Examples(examples, inputs=[prompt])
|
98 |
+
with gr.Column():
|
99 |
+
with gr.Row():
|
100 |
+
element = gr.components.Textbox(lines=2, label="構成要件要素")
|
101 |
+
# tag = gr.components.Textbox(label="標籤")
|
102 |
+
tag = gr.Dropdown(
|
103 |
+
choices = [("被告(犯罪主體)","<LEO_SOC>"), ("主觀犯意", "<LEO_SLE>"), ("不法行為","<LEO_ACT>"), ("因果關係","<LEO_CAU>"),
|
104 |
+
("被害人/告訴人","<LEO_VIC>"), ("危害結果","<LEO_ROH>"), ("未遂","<LEO_ATP>"), ("既遂","<LEO_ACC>"),
|
105 |
+
("中止","<LEO_ABA>"), ("預備","<LEO_PRP>")],
|
106 |
+
label="標籤", info="構成要件要素的標籤")
|
107 |
+
with gr.Row():
|
108 |
+
with gr.Column():
|
109 |
+
btn2 = gr.Button("產生標註語料內容")
|
110 |
+
result = gr.components.Textbox(lines=5, label="語料內容", show_copy_button=True)
|
111 |
+
# btn3 = gr.Button("下載")
|
112 |
+
btn.click(random_next, inputs=[], outputs=[courtName, caseNo, prompt, title])
|
113 |
+
btn2.click(gen_template, inputs=[prompt, element, tag], outputs=[result])
|
114 |
+
# btn3.click(download_file, inputs=[result, title], outputs=[])
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
+
# 下載判決書資料集
|
118 |
+
login(token = os.environ['HUB_TOKEN'])
|
119 |
+
use_auth_token=os.environ['HUB_TOKEN'] # 下載判決書資料集所需要的 token。
|
120 |
+
dataset = load_dataset("jslin09/Fraud_Case_Verdicts", use_auth_token=use_auth_token, revision="main")
|
121 |
+
dataset = dataset.map(remove_space)
|
122 |
+
demo.launch(share=True) # 在遠端啟動時,需要 share=True 。
|