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#Intalls Argilla
|
5 |
+
|
6 |
+
!huggingface-cli login #Logs on Hugging Face
|
7 |
+
|
8 |
+
#Dataset creation on Argila Application
|
9 |
+
|
10 |
+
import argilla as rg
|
11 |
+
|
12 |
+
client = rg.Argilla(
|
13 |
+
api_url="https://javierbarbaesparcia-spanish-legal-ner.hf.space",
|
14 |
+
api_key="LSm85ZCk-G_RWZlp4YOrz8T8xY9T4ygjypJXKApS9N9yZsbHoSFIlqZyFqLmTitPncnUSaQBFqRBOBvVxFlJemNoj0HcrsJhjP2of-1tOzw",
|
15 |
+
)
|
16 |
+
|
17 |
+
settings = rg.Settings(
|
18 |
+
guidelines="These are some guidelines.",
|
19 |
+
fields=[
|
20 |
+
rg.TextField(
|
21 |
+
name="law_cont"
|
22 |
+
),
|
23 |
+
],
|
24 |
+
questions=[
|
25 |
+
rg.SpanQuestion(
|
26 |
+
field="law_cont",
|
27 |
+
name="label",
|
28 |
+
labels={
|
29 |
+
"PERSON": "Person",
|
30 |
+
"ORG": "Organization",
|
31 |
+
"LOC": "Location",
|
32 |
+
"DATE": "Date",
|
33 |
+
"REF": "Reference"
|
34 |
+
}
|
35 |
+
),
|
36 |
+
rg.MultiLabelQuestion(
|
37 |
+
name="label_multi",
|
38 |
+
labels={
|
39 |
+
"RIGHT": "Right",
|
40 |
+
"DUTY": "Duty",
|
41 |
+
"SANC": "Sanction"
|
42 |
+
}
|
43 |
+
)
|
44 |
+
],
|
45 |
+
metadata=[
|
46 |
+
rg.TermsMetadataProperty(
|
47 |
+
name="fecha_actualizacion",
|
48 |
+
title="Update date",
|
49 |
+
visible_for_annotators="true"
|
50 |
+
),
|
51 |
+
rg.TermsMetadataProperty(
|
52 |
+
name="identificador",
|
53 |
+
title="Identifier",
|
54 |
+
visible_for_annotators="true"
|
55 |
+
),
|
56 |
+
rg.TermsMetadataProperty(
|
57 |
+
name="ambito",
|
58 |
+
title="Domain",
|
59 |
+
visible_for_annotators="true"
|
60 |
+
),
|
61 |
+
rg.TermsMetadataProperty(
|
62 |
+
name="departamento",
|
63 |
+
title="Department",
|
64 |
+
visible_for_annotators="true"
|
65 |
+
),
|
66 |
+
rg.TermsMetadataProperty(
|
67 |
+
name="rango",
|
68 |
+
title="Type of legislation",
|
69 |
+
visible_for_annotators="true"
|
70 |
+
),
|
71 |
+
rg.TermsMetadataProperty(
|
72 |
+
name="fecha_disposicion",
|
73 |
+
title="Provision date",
|
74 |
+
visible_for_annotators="true"
|
75 |
+
),
|
76 |
+
rg.TermsMetadataProperty(
|
77 |
+
name="numero_oficial",
|
78 |
+
title="Official number",
|
79 |
+
visible_for_annotators="true"
|
80 |
+
),
|
81 |
+
rg.TermsMetadataProperty(
|
82 |
+
name="titulo",
|
83 |
+
title="Title",
|
84 |
+
visible_for_annotators="true"
|
85 |
+
),
|
86 |
+
rg.TermsMetadataProperty(
|
87 |
+
name="diario",
|
88 |
+
title="Paper",
|
89 |
+
visible_for_annotators="true"
|
90 |
+
),
|
91 |
+
rg.TermsMetadataProperty(
|
92 |
+
name="fecha_publicacion",
|
93 |
+
title="Publication date",
|
94 |
+
visible_for_annotators="true"
|
95 |
+
),
|
96 |
+
rg.TermsMetadataProperty(
|
97 |
+
name="diario_numero",
|
98 |
+
title="Paper number",
|
99 |
+
visible_for_annotators="true"
|
100 |
+
),
|
101 |
+
rg.TermsMetadataProperty(
|
102 |
+
name="fecha_vigencia",
|
103 |
+
title="Validity date",
|
104 |
+
visible_for_annotators="true",
|
105 |
+
),
|
106 |
+
rg.TermsMetadataProperty(
|
107 |
+
name="estatus_derogacion",
|
108 |
+
title="Update date",
|
109 |
+
visible_for_annotators="true"
|
110 |
+
),
|
111 |
+
rg.TermsMetadataProperty(
|
112 |
+
name="fecha_derogacion",
|
113 |
+
title="Repeal date",
|
114 |
+
visible_for_annotators="true",
|
115 |
+
),
|
116 |
+
rg.TermsMetadataProperty(
|
117 |
+
name="estatus_anulacion",
|
118 |
+
title="Cancellation state",
|
119 |
+
visible_for_annotators="true"
|
120 |
+
),
|
121 |
+
rg.TermsMetadataProperty(
|
122 |
+
name="vigencia_agotada",
|
123 |
+
title="Validity runned out",
|
124 |
+
visible_for_annotators="true"
|
125 |
+
),
|
126 |
+
rg.TermsMetadataProperty(
|
127 |
+
name="estado_consolidacion",
|
128 |
+
title="Consolidation state",
|
129 |
+
visible_for_annotators="true"
|
130 |
+
),
|
131 |
+
rg.TermsMetadataProperty(
|
132 |
+
name="url_eli",
|
133 |
+
title="Link",
|
134 |
+
visible_for_annotators="true"
|
135 |
+
),
|
136 |
+
rg.TermsMetadataProperty(
|
137 |
+
name="url_html_consolidada",
|
138 |
+
title="Html link",
|
139 |
+
visible_for_annotators="true"
|
140 |
+
)
|
141 |
+
]
|
142 |
+
)
|
143 |
+
|
144 |
+
dataset = rg.Dataset(
|
145 |
+
name="spanish_legal_ner",
|
146 |
+
settings=settings,
|
147 |
+
)
|
148 |
+
|
149 |
+
dataset.create()
|
150 |
+
|
151 |
+
#Retrieval of data from BOE API
|
152 |
+
|
153 |
+
pip install requests
|
154 |
+
import argilla as rg
|
155 |
+
|
156 |
+
client = rg.Argilla(
|
157 |
+
api_url="https://javierbarbaesparcia-spanish-legal-ner.hf.space",
|
158 |
+
api_key="" #For security reasons API key is ofuscated
|
159 |
+
)
|
160 |
+
import requests
|
161 |
+
import json
|
162 |
+
import xml.etree.ElementTree as ET
|
163 |
+
dataset=client.datasets("spanish_legal_ner")
|
164 |
+
law_list = json.loads(response.text)
|
165 |
+
for item in law_list.get("data"):
|
166 |
+
identifier = item.get("identificador")
|
167 |
+
print("Retrieving metadata of "+identifier)
|
168 |
+
print("Retrieving block list of "+identifier)
|
169 |
+
api_url="https://boe.es/datosabiertos/api/legislacion-consolidada/id/"+identifier+"/texto/indice"
|
170 |
+
respbloc = requests.get(api_url,headers = {'Accept': 'application/json'})
|
171 |
+
print(respbloc.status_code)
|
172 |
+
block_list = json.loads(respbloc.text)
|
173 |
+
for item1 in block_list.get("data"):
|
174 |
+
item2=item1.get("bloque")
|
175 |
+
for item3 in item2:
|
176 |
+
block = item3.get("id")
|
177 |
+
print("Retrieving data in block " +block)
|
178 |
+
api_url="https://boe.es/datosabiertos/api/legislacion-consolidada/id/"+identifier+"/texto/bloque/"+block
|
179 |
+
|
180 |
+
data = requests.get(api_url,headers = {'Accept': 'application/xml'})
|
181 |
+
|
182 |
+
metadata_url="https://boe.es/datosabiertos/api/legislacion-consolidada/id/"+identifier+"/metadatos"
|
183 |
+
|
184 |
+
metadata = requests.get(metadata_url,headers = {'Accept': 'application/json'})
|
185 |
+
|
186 |
+
datatext = data
|
187 |
+
metadatatext = json.loads(metadata.text).get("data")[0]
|
188 |
+
print("Data: "+str(data.status_code))
|
189 |
+
print("Metadata: "+str(metadata.status_code))
|
190 |
+
final = ""
|
191 |
+
root = ET.fromstring(data.content)
|
192 |
+
for desc in root.iter('p'):
|
193 |
+
final = final + ("" if desc.text is None else desc.text)
|
194 |
+
|
195 |
+
record = [rg.Record(
|
196 |
+
fields={
|
197 |
+
"law_cont": final
|
198 |
+
},
|
199 |
+
metadata={
|
200 |
+
"fecha_actualizacion": metadatatext.get("fecha_actualizacion"),
|
201 |
+
"identificador": metadatatext.get("identificador"),
|
202 |
+
"ambito": metadatatext.get("ambito").get("texto"),
|
203 |
+
"departamento": metadatatext.get("departamento").get("texto"),
|
204 |
+
"rango": metadatatext.get("rango").get("texto"),
|
205 |
+
"fecha_disposicion": metadatatext.get("fecha_disposicion"),
|
206 |
+
"numero_oficial": metadatatext.get("numero_oficial"),
|
207 |
+
"titulo": metadatatext.get("titulo"),
|
208 |
+
"diario": metadatatext.get("diario"),
|
209 |
+
"fecha_publicacion": metadatatext.get("fecha_publicacion"),
|
210 |
+
"diario_numero": metadatatext.get("diario_numero"),
|
211 |
+
"fecha_vigencia": metadatatext.get("fecha_vigencia"),
|
212 |
+
"estatus_derogacion": metadatatext.get("estatus_derogacion"),
|
213 |
+
"fecha_derogacion": metadatatext.get("fecha_derogacion"),
|
214 |
+
"estatus_anulacion": metadatatext.get("estatus_anulacion"),
|
215 |
+
"vigencia_agotada": metadatatext.get("vigencia_agotada"),
|
216 |
+
"estado_consolidacion": metadatatext.get("estado_consolidacion").get("texto"),
|
217 |
+
"url_eli": metadatatext.get("url_eli"),
|
218 |
+
"url_html_consolidada": metadatatext.get("url_html_consolidada")
|
219 |
+
},
|
220 |
+
)
|
221 |
+
]
|
222 |
+
|
223 |
+
dataset.records.log(record)
|