-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_app.py
767 lines (659 loc) · 25.3 KB
/
old_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
import streamlit as st
import requests
import re
import json
import os
import subprocess
from github import Github
from git import Repo
from datetime import date
from constants import *
from dotenv import load_dotenv
MASADER_BOT_URL = "https://masaderbot-production.up.railway.app/run"
st.set_page_config(
page_title="Masader Form",
page_icon="📮",
initial_sidebar_state="collapsed",
)
"# 📮 :rainbow[Masader Form]"
load_dotenv() # Load environment variables from a .env file
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
GIT_USER_NAME = os.getenv("GIT_USER_NAME")
GIT_USER_EMAIL = os.getenv("GIT_USER_EMAIL")
def validate_github(username):
response = requests.get(f"https://api.github.com/users/{username}")
if response.status_code == 200:
return True
else:
return False
def validate_url(url):
try:
response = requests.head(url, allow_redirects=True, timeout=5)
if response.status_code == 200:
return True
else:
return False
except requests.ConnectionError:
return False
def validate_dataname(name: str) -> bool:
"""
Validates the name of the dataset.
Args:
name (str): The name of the dataset.
Returns:
bool: True if valid, False otherwise.
"""
for char in name.lower():
if char not in VALID_SYMP_NAMES:
st.error(f"Invalid character in the dataset name {char}")
return False
return True
def validate_comma_separated_number(number: str) -> bool:
"""
Validates a number with commas separating thousands.
Args:
number (str): The number as a string.
Returns:
bool: True if valid, False otherwise.
"""
# Regular expression pattern to match numbers with comma-separated thousands
pattern = r"^\d{1,3}(,\d{3})*$"
# Match the pattern
return bool(re.fullmatch(pattern, number))
def update_session_config(json_data):
for key in json_data:
if key in ["Year"]:
try:
st.session_state[key] = int(json_data[key])
except:
st.session_state[key] = 2024
elif key in ["Collection Style", "Domain"]:
values = [val.strip() for val in json_data[key].split(",")]
acc_values = []
# if some values are not legitimate, use other instead
for value in values:
if value in column_options[key].split(","):
acc_values.append(value)
if len(values) > len(acc_values):
if "other" not in acc_values:
acc_values.append("other")
st.session_state[key] = acc_values
elif key == "Tasks":
tasks = []
other_tasks = []
for task in [task.strip() for task in json_data[key].split(",")]:
if task not in column_options["Tasks"].split(","):
other_tasks.append(task)
else:
tasks.append(task)
if len(other_tasks):
st.session_state["Other Tasks"] = ",".join(other_tasks)
if len(tasks):
st.session_state["Tasks"] = tasks
elif key == "Subsets":
for i, subset in enumerate(json_data[key]):
for subkey in subset:
st.session_state[f"subset_{i}_{subkey.lower()}"] = json_data[key][
i
][subkey]
else:
st.session_state[key] = json_data[key].strip()
def reload_config(json_data):
if "metadata" in json_data:
json_data = json_data["metadata"]
update_session_config(json_data)
st.session_state.show_form = True
def render_form():
i = 0
while True:
col1, col2, col3, col4 = st.columns([2, 2, 1, 1])
with col1:
name = st.text_input("Name:", key=f"subset_{i}_name")
with col3:
volume = st.text_input("Volume", key=f"subset_{i}_volume")
with col2:
dialect = st.selectbox(
"Dialect",
column_options["Dialect"].split(","),
key=f"subset_{i}_dialect",
)
with col4:
unit = st.selectbox(
"Unit", column_options["Unit"].split(","), key=f"subset_{i}_unit"
)
if name:
i += 1
else:
break
def update_pr(new_dataset):
PRS = []
if os.path.exists("prs.json"):
with open("prs.json", "r") as f:
PRS = json.load(f)
else:
with open("prs.json", "w") as f:
json.dump(PRS, f, indent=4)
# create a valid name for the dataset
data_name = new_dataset["Name"].lower().strip()
for symbol in VALID_PUNCT_NAMES:
data_name = data_name.replace(symbol, "_")
# Configuration
REPO_NAME = "ARBML/masader" # Format: "owner/repo"
BRANCH_NAME = f"add-{data_name}"
PR_TITLE = f"Adding {new_dataset['Name']} to the catalogue"
PR_BODY = f"This is a pull request by @{st.session_state['gh_username']} to add a {new_dataset['Name']} to the catalogue."
# Initialize GitHub client
g = Github(GITHUB_TOKEN)
repo = g.get_repo(REPO_NAME)
# setup name and email
os.system(f"git config --global user.email {GIT_USER_EMAIL}")
os.system(f"git config --global user.name {GIT_USER_NAME}")
# Clone repository
repo_url = f"https://{GITHUB_TOKEN}@github.com/{REPO_NAME}.git"
local_path = "./temp_repo"
pr_exists = False
# check the list of Pull Requests
for pr in PRS:
pr_obj = repo.get_pull(pr["number"])
# check the branch if it exists
if pr["branch"] == BRANCH_NAME:
print("PR already exists")
pr_exists = True
else:
# delete unused branches
if pr["state"] == "open":
if pr_obj.state == "closed":
# repo.get_git_ref(f"heads/{pr['branch']}").delete() # might be risky
pr["state"] = "closed"
if os.path.exists(local_path):
subprocess.run(["rm", "-rf", local_path]) # Clean up if exists
Repo.clone_from(repo_url, local_path)
# Modify file
local_repo = Repo(local_path)
FILE_PATH = f"datasets/{data_name}.json"
# if the branch exists
if pr_exists:
local_repo.git.checkout(BRANCH_NAME)
local_repo.git.pull("origin", BRANCH_NAME)
with open(f"{local_path}/{FILE_PATH}", "w") as f:
json.dump(new_dataset, f, indent=4)
local_repo.git.add(FILE_PATH)
# check if changes made
if local_repo.is_dirty():
local_repo.git.commit("-m", f"Updating {FILE_PATH}")
local_repo.git.push("origin", BRANCH_NAME)
else:
st.info("No changes made to the dataset")
return
else:
with open(f"{local_path}/{FILE_PATH}", "w") as f:
json.dump(new_dataset, f, indent=4)
local_repo.git.checkout("-b", BRANCH_NAME)
local_repo.git.pull("origin", "main")
# Commit and push changes
local_repo.git.add(FILE_PATH)
local_repo.git.commit("-m", f"Creating {FILE_PATH}.json")
local_repo.git.push("--set-upstream", "origin", BRANCH_NAME)
# if the PR doesn't exist
if not pr_exists:
pr = repo.create_pull(
title=PR_TITLE,
body=PR_BODY,
head=BRANCH_NAME,
base=repo.default_branch,
)
st.success(f"Pull request created: {pr.html_url}")
# add the pr
PRS.append(
{
"name": new_dataset["Name"],
"url": pr.html_url,
"branch": BRANCH_NAME,
"state": "open",
"number": pr.number,
}
)
else:
st.success(f"Pull request updated")
with open("prs.json", "w") as f:
json.dump(PRS, f, indent=4)
st.balloons()
def load_json(url, link="", pdf=None):
# Make the GET request to fetch the JSON data
if link != "":
response = requests.post(url, data={"link": link})
elif pdf:
response = requests.post(url, files={"file": pdf})
else:
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON content
json_data = response.json()
reload_config(json_data)
return True
else:
st.error(response.text)
return False
def reset_config():
with open("default.json", "r") as f:
reload_config(json.load(f))
st.session_state.show_form = False
@st.fragment()
def final_state():
col1, col2 = st.columns(2)
with col1:
submit = st.form_submit_button("Submit")
with col2:
save = st.form_submit_button("Save")
if submit or save:
if not validate_github(st.session_state["gh_username"].strip()):
st.error("Please enter a valid GitHub username.")
elif not validate_dataname(st.session_state["Name"]):
st.error("Please enter a valid dataset name.")
elif not validate_url(st.session_state["Link"]):
st.error("Please enter a valid repository link.")
elif not st.session_state["License"].strip():
st.error("Please select a valid license.")
elif not st.session_state["Dialect"]:
st.error("Please enter a valid dialect.")
elif not st.session_state["Domain"]:
st.error("Please select a valid domain.")
elif not st.session_state["Collection Style"]:
st.error("Please select a valid collection style")
elif (
not st.session_state["Description"].strip()
or len(st.session_state["Description"]) < 10
):
st.error("Please enter a non empty (detailed) description of the dataset")
elif not validate_comma_separated_number(st.session_state["Volume"].strip()):
st.error("Please enter a valid volume. for example 1,000")
elif not st.session_state["Unit"].strip():
st.error("Please select a valid unit.")
elif not st.session_state["Host"].strip():
st.error("Please select a valid host.")
elif not st.session_state["Tasks"]:
st.error("Please select the Tasks.")
elif not st.session_state["Added By"].strip():
st.error("Please enter your full name.")
else:
config = create_json()
if submit:
update_pr(config)
else:
save_path = st.text_input(
"Save Path",
value=f"/Users/zaidalyafeai/Documents/Development/masader_bot/validset/{st.session_state['Name'].lower()}.json",
help="Enter the directory path to save the JSON file",
)
if save_path:
with open(save_path, "w") as f:
json.dump(config, f, indent=4)
st.success(f"Form saved successfully to {save_path}")
def create_json():
config = {}
columns = [
"Name",
"Subsets",
"HF Link",
"Link",
"License",
"Year",
"Language",
"Dialect",
"Domain",
"Form",
"Collection Style",
"Description",
"Volume",
"Unit",
"Ethical Risks",
"Provider",
"Derived From",
"Paper Title",
"Paper Link",
"Script",
"Tokenized",
"Host",
"Access",
"Cost",
"Test Split",
"Tasks",
"Venue Title",
"Citations",
"Venue Type",
"Venue Name",
"Authors",
"Affiliations",
"Abstract",
"Added By",
]
for key in columns:
if key == "Subsets":
config["Subsets"] = []
i = 0
while True:
subset = {}
if f"subset_{i}_name" in st.session_state:
if st.session_state[f"subset_{i}_name"] != "":
subset["Name"] = st.session_state[f"subset_{i}_name"]
subset["Volume"] = st.session_state[f"subset_{i}_volume"]
subset["Dialect"] = st.session_state[f"subset_{i}_dialect"]
subset["Unit"] = st.session_state[f"subset_{i}_unit"]
config["Subsets"].append(subset)
i += 1
continue
break
elif key in ["Collection Style", "Domain"]:
config[key] = ",".join(st.session_state[key])
elif key == "Tasks":
tasks = st.session_state[key]
if st.session_state["Other Tasks"].strip() != "":
tasks += st.session_state["Other Tasks"].split(",")
config[key] = ",".join(tasks)
else:
config[key] = st.session_state[key]
return config
def create_element(label, placeholder="", help="", key="", value="", options=[]):
st.text(label)
if key in [
"Language",
"Form",
"Unit",
"Ethical Risks",
"Script",
"Access",
"Test Split",
"Venue Type",
]:
st.radio(key, options=options, key=key, label_visibility="collapsed")
elif key in ["License", "Dialect", "Host"]:
st.selectbox(key, options=options, key=key, label_visibility="collapsed")
elif key in ["Domain", "Collection Style", "Tasks"]:
if key == "Collection Style":
with st.expander("See description"):
st.caption(
"""
- **crawling** the data has been collected using scripts to collect the data
- **human annotaiton** the data has been labeled by humans
- **machine annotation** the data has been labeled by a software i.e. MT, OCR, ...
- **LLM Generated** LLMs have been used to collect or annotate the data
- **manual curation** the data has been created manually.
"""
)
st.multiselect(key, options=options, key=key, label_visibility="collapsed")
elif key in ["Description", "Abstract", "Affiliations", "Authors"]:
st.text_area(
key,
key=key,
placeholder=placeholder,
help=help,
label_visibility="collapsed",
)
else:
st.text_input(
key,
key=key,
placeholder=placeholder,
help=help,
value=value,
label_visibility="collapsed",
)
def main():
st.info(
"""
This is a the Masader form to add datasets to [Masader](https://arbml.github.io/masader/) catalogue.
Before starting, please make sure you read the following instructions:
- There are three options
- 🦚 Manual Annotation: You can have to insert all the metadata manually.
- 🤖 AI Annotation: Insert the pdf/arxiv link to extract the metadata automatically.
- 🚥 Load Annotation: Use this option to load a saved metadata annotation.
- Check the dataset does not exist in the catelouge using the search [Masader](https://arbml.github.io/masader/search)
- You have a valid GitHub username
- You have the direct link to the dataset repository
Once you submit the dataset, we will send a PR, make sure you follow up there if you have any questions.
If you have face any issues post them on [GitHub](https://github.com/arbml/masader/issues).
""",
icon="👾",
)
if "show_form" not in st.session_state:
reset_config()
if st.query_params:
if st.query_params["json_url"]:
load_json(st.query_params["json_url"])
options = st.selectbox(
"Annotation Options",
["🦚 Manual Annotation", "🤖 AI Annotation", "🚥 Load Annotation"],
on_change=reset_config,
)
if options == "🚥 Load Annotation":
upload_file = st.file_uploader(
"Upload Json",
help="You can use this widget to preload any dataset from https://github.com/ARBML/masader/tree/main/datasets",
)
json_url = st.text_input(
"Path to json",
placeholder="For example: https://raw.githubusercontent.com/ARBML/masader_form/refs/heads/main/shami.json",
)
if upload_file:
json_data = json.load(upload_file)
reload_config(json_data)
elif json_url:
load_json(json_url)
else:
reset_config()
elif options == "🤖 AI Annotation":
st.warning(
"‼️ AI annotation uses LLMs to extract the metadata form papers. However, this approach\
is not reliable as LLMs can hellucinate and extract untrustworthy informations. \
Make sure you revise the generated metadata before you submit."
)
paper_url = st.text_input("Insert arXiv or direct pdf link")
upload_pdf = st.file_uploader(
"Upload PDF of the paper",
help="You can use this widget to preload any dataset from https://github.com/ARBML/masader/tree/main/datasets",
)
if paper_url:
if "arxiv" in paper_url:
load_json(MASADER_BOT_URL, link=paper_url)
else:
response = requests.get(paper_url)
response.raise_for_status() # Raise an error for bad responses (e.g., 404)
if response.headers.get("Content-Type") == "application/pdf":
pdf = (
paper_url.split("/")[-1],
response.content,
response.headers.get("Content-Type", "application/pdf"),
)
load_json(MASADER_BOT_URL, pdf=pdf)
else:
st.error(
f"Cannot retrieve a pdf from the link. Make sure {paper_url} is a direct link to a valid pdf"
)
elif upload_pdf:
# Prepare the file for sending
pdf = (upload_pdf.name, upload_pdf.getvalue(), upload_pdf.type)
load_json(MASADER_BOT_URL, pdf=pdf)
else:
reset_config()
else:
st.session_state.show_form = True
if st.session_state.show_form:
with st.form(key="dataset_form"):
create_element("GitHub username*", key="gh_username", value="zaidalyafeai")
create_element(
"Name of the dataset*",
placeholder="Use a representative name of the dataset.",
help="For example CALLHOME: Egyptian Arabic Speech Translation Corpus",
key="Name",
)
with st.expander("Add dilaect subsets"):
st.caption(
"Use this field to add dialect subsets of the dataset. For example if the dataset has 1,000 sentences in the Yemeni dialect.\
For example take a look at the [shami subsets](https://github.com/ARBML/masader/tree/main/datasets/shami.json)."
)
render_form()
# Links
create_element(
"Link*", placeholder="The link must be accessible", key="Link"
)
create_element(
"Huggingface Link",
placeholder="for example https://huggingface.co/datasets/labr",
help="for example https://huggingface.co/datasets/labr",
key="HF Link",
)
# Dataset Properties
create_element(
"License*", options=column_options["License"].split(","), key="License"
)
current_year = date.today().year
st.number_input(
"Year*",
min_value=2000,
max_value=current_year,
help="Year of publishing the dataset/paper",
key="Year",
)
create_element(
"Language*",
options=column_options["Language"].split(","),
key="Language",
)
create_element(
"Dialect*",
options=column_options["Dialect"].split(","),
help="Used mixed if the dataset contains multiple dialects",
key="Dialect",
)
create_element(
"Domain*", options=column_options["Domain"].split(","), key="Domain"
)
create_element(
"Form*", options=column_options["Form"].split(","), key="Form"
)
create_element(
"Collection Style*",
options=column_options["Collection Style"].split(","),
key="Collection Style",
)
create_element(
"Description*",
placeholder="Description about the dataset and its contents.",
help="brief description of the dataset",
key="Description",
)
# Volume and Units
create_element(
"Volume*",
placeholder="For example 1,000.",
help="How many samples are in the dataset. Please don't use abbreviations like 10K",
key="Volume",
)
create_element(
"Unit*",
options=column_options["Unit"].split(","),
help="tokens usually used for ner, pos tagging, etc. sentences for sentiment analysis, documents for text modelling tasks",
key="Unit",
)
create_element(
"Ethical Risks",
options=column_options["Ethical Risks"].split(","),
help="social media datasets are considered mid risks as they might release personal information, others might contain hate speech as well so considered as high risk",
key="Ethical Risks",
)
create_element(
"Provider",
placeholder="Name of institution i.e. NYU Abu Dhabi",
key="Provider",
)
create_element(
"Derived From",
placeholder="What is the source dataset, i.e. Common Crawl",
key="Derived From",
)
# Paper Information
create_element(
"Paper Title", placeholder="Full title of the paper", key="Paper Title"
)
create_element(
"Paper Link",
placeholder="Link to the pdf i.e. https://arxiv.org/pdf/2110.06744.pdf",
key="Paper Link",
)
# Technical Details
create_element(
"Script*", options=column_options["Script"].split(","), key="Script"
)
create_element(
"Tokenized*",
options=column_options["Tokenized"].split(","),
help="Is the dataset tokenized i.e. الرجل = ال رجل",
key="Tokenized",
)
create_element(
"Host*",
options=column_options["Host"].split(","),
help="The name of the repository that hosts the data. Use other if not in the options.",
key="Host",
)
create_element(
"Access*", options=column_options["Access"].split(","), key="Access"
)
create_element(
"Cost",
placeholder="If the access is With-Fee inser the cost, i.e. 1750 $",
help="For example 1750 $",
key="Cost",
)
create_element(
"Test split*",
options=column_options["Test Split"].split(","),
help="Does the dataset have validation / test split",
key="Test Split",
)
create_element(
"Tasks*", options=column_options["Tasks"].split(","), key="Tasks"
)
create_element(
"Other Tasks*",
placeholder="Other tasks that don't exist in the Tasks options.",
help="Make sure the tasks don't appear in the Tasks field",
key="Other Tasks",
)
create_element(
"Venue Title", placeholder="Venue shortcut i.e. ACL", key="Venue Title"
)
# Venue Type
create_element(
"Venue Type",
options=column_options["Venue Type"].split(","),
help="Select the type of venue",
key="Venue Type",
)
# Venue Name
create_element(
"Venue Name",
placeholder="Full name i.e. Association of Computational Linguistics",
key="Venue Name",
)
# Authors
create_element(
"Authors", placeholder="Add all authors split by comma", key="Authors"
)
# Affiliations
create_element(
"Affiliations", placeholder="Enter affiliations", key="Affiliations"
)
# Abstract
create_element(
"Abstract",
placeholder="Abstract of the published paper",
key="Abstract",
)
create_element(
"Full Name*", placeholder="Please Enter your full name", key="Added By"
)
final_state()
if __name__ == "__main__":
main()