-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
757 lines (617 loc) · 22.2 KB
/
utils.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
# Standard library imports
import base64
import difflib
import json
import logging
import os
import random
import re
import sys
import threading
import time
from base64 import b64decode
from datetime import date
from functools import wraps
from glob import glob
# Third-party imports
import pandas as pd
import requests
from bs4 import BeautifulSoup
from google.oauth2 import service_account
from vertexai.generative_models import GenerativeModel, GenerationConfig # type: ignore
# Local imports
from constants import *
random.seed(0)
# masader_dataset = load_dataset("arbml/masader", download_mode="")["train"]
eval_datasets = {
"ar": {
"valid": [json.load(open(f)) for f in glob("evals/ar/validset/**.json")],
"test": [json.load(open(f)) for f in glob("evals/ar/testset/**.json")],
},
"en": {"test": [json.load(open(f)) for f in glob("evals/en/testset/**.json")]},
"ru": {"test": [json.load(open(f)) for f in glob("evals/ru/testset/**.json")]},
"jp": {"test": [json.load(open(f)) for f in glob("evals/jp/testset/**.json")]},
"fr": {"test": [json.load(open(f)) for f in glob("evals/fr/testset/**.json")]},
"multi": {"test": [json.load(open(f)) for f in glob("evals/multi/testset/**.json")]}
}
def extract_and_generate_readme(url):
"""
Extracts a web page from a URL and uses Gemini-1.5-Flash to convert it into a structured README.
Args:
url (str): The URL of the web page to extract.
Returns:
str: Generated README text.
"""
# Step 1: Extract Web Page Content
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
# Extract the main content (you can customize this for better results)
title = soup.title.string if soup.title else "Untitled Page"
body = " ".join([p.get_text() for p in soup.find_all("p")])
content = f"Title: {title}\n\n{body}"
except Exception as e:
return f"Failed to fetch or parse the URL: {e}"
try:
model = GenerativeModel(
"gemini-1.5-flash",
system_instruction="Generate a structured README summarizing this content.",
)
message = model.generate_content(
content,
generation_config=GenerationConfig(
max_output_tokens=1000,
temperature=0.0,
),
)
return message.text
except Exception as e:
return f"Failed to generate README: {e}"
def get_google_credentials():
decoded_config = base64.b64decode(
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
).decode("utf-8")
config_data = json.loads(decoded_config)
credentials = service_account.Credentials.from_service_account_info(config_data)
return credentials
def compute_cost(message, model):
default = {
"cost": -1,
"input_tokens": -1,
"output_tokens": -1,
}
if message is None:
return default
try:
if "gpt" in model:
num_inp_tokens = message.usage.input_tokens
num_out_tokens = message.usage.output_tokens
elif "DeepSeek-V3" in model:
num_inp_tokens = message.usage.prompt_tokens
num_out_tokens = message.usage.completion_tokens
elif "DeepSeek-R1" in model:
num_inp_tokens = message.usage.prompt_tokens
num_out_tokens = message.usage.completion_tokens
elif "claude" in model:
num_inp_tokens = message.usage.prompt_tokens
num_out_tokens = message.usage.completion_tokens
elif "gemini" in model:
num_inp_tokens = message.usage_metadata.prompt_token_count
num_out_tokens = message.usage_metadata.candidates_token_count
cost = (num_inp_tokens / 1e6) * costs[model]["input"] + (
num_out_tokens / 1e6
) * costs[model]["output"]
except:
print("Cannot compute the cost ...")
return default
return {
"cost": cost,
"input_tokens": num_inp_tokens,
"output_tokens": num_out_tokens,
}
def spinner_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
# Event to stop the spinner
stop_event = threading.Event()
# Spinner thread
spinner_thread = threading.Thread(target=spinner_animation, args=(stop_event,))
spinner_thread.start()
# Run the wrapped function
try:
result = func(*args, **kwargs)
finally:
# Stop the spinner and wait for the thread to finish
stop_event.set()
spinner_thread.join()
clear_line() # Clear the spinner from the terminal
return result
return wrapper
def spinner_animation(stop_event):
spinner = ["|", "/", "-", "\\"]
idx = 0
while not stop_event.is_set():
sys.stdout.write("\r" + spinner[idx])
sys.stdout.flush()
idx = (idx + 1) % len(spinner)
time.sleep(0.1)
def clear_line():
"""Clear the current line in the terminal."""
sys.stdout.write("\r\033[K") # Move to the start of the line and clear it
sys.stdout.flush()
def has_common(d1, d2):
if d1 == [] and d2 == []:
return True
d1 = [d.lower().strip() for d in d1]
d2 = [d.lower().strip() for d in d2]
if len(set(d1).intersection(d2)):
return True
else:
return False
def all_same(d1, d2):
d1 = [d.lower().strip() for d in d1.split(",")]
d2 = [d.lower().strip() for d in d2.split(",")]
if len(set(d1).intersection(d2)) == len(d1):
return True
else:
return False
def setup_logger() -> logging.Logger:
"""Set up logging configuration."""
logger = logging.getLogger("results")
logger.setLevel(logging.INFO)
if not logger.handlers:
handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def match_titles(title, masader_title):
if isinstance(masader_title, float):
return 0
return difflib.SequenceMatcher(None, title, masader_title).ratio()
def get_predictions(
gold_metadata, pred_metadata, use_annotations_paper=False, schema="ar"
):
validation_columns = schemata[schema]["validation_columns"]
column_types = schemata[schema]["column_types"]
results = {c: 0 for c in validation_columns}
if gold_metadata is None:
return results
for column in validation_columns:
gold_answer = gold_metadata[column]
if str(gold_answer) == "nan":
gold_answer = ""
pred_answer = pred_metadata[column]
if use_annotations_paper:
if gold_metadata["annotations_from_paper"][column] == 0:
results[column] = 1
continue
if "List[Dict" in column_types[column]:
try:
if len(pred_answer) != len(gold_answer):
continue
except:
print(pred_metadata)
raise
matched = True
for i, subset in enumerate(gold_answer):
for key in subset:
if key not in pred_answer[i]:
matched = False
if str(subset[key]) != str(pred_answer[i][key]):
matched = False
if matched:
results[column] = 1
continue
elif column in schemata[schema]["columns_with_lists"]:
assert isinstance(
pred_answer, list
), f"pred_answer is not a list: {pred_answer}"
if has_common(gold_answer, pred_answer):
results[column] = 1
elif pred_answer == gold_answer:
results[column] = 1
else:
pass
# print(pred_answer, gold_answer)
return results
def evaluate_metadata(
gold_metadata, pred_metadata, use_annotations_paper=False, schema="ar"
):
evaluation_subsets = schemata[schema]["evaluation_subsets"]
results = {c: 0 for c in evaluation_subsets}
predictions = get_predictions(
gold_metadata,
pred_metadata,
use_annotations_paper=use_annotations_paper,
schema=schema,
)
for subset in evaluation_subsets:
for column in evaluation_subsets[subset]:
results[subset] += predictions[column]
results[subset] = results[subset] / len(evaluation_subsets[subset])
results["AVERAGE"] = sum(predictions.values()) / len(predictions)
return results
def validate(metadata, use_split=None, title="", link="", schema="ar"):
matched_row = None
if use_split is not None:
dataset = eval_datasets[schema][use_split]
else:
pass
for row in dataset:
if title != "":
if title == row["Paper Title"]:
matched_row = row
elif link != "":
if link == fix_arxiv_link(row["Paper Link"]):
matched_row = row
else:
if match_titles(str(metadata["Paper Title"]), row["Paper Title"]) > 0.8:
matched_row = row
if matched_row is None and use_split is not None:
raise ()
return evaluate_metadata(matched_row, metadata, schema=schema)
from collections import Counter
def majority_vote(dicts, schema="ar"):
column_types = schemata[schema]["column_types"]
result = {}
for key in schemata[schema]["columns"]:
if "List[Dict" in column_types[key]:
result[key] = []
continue
# only use smarter models as a judge
values = []
for model in dicts:
value = dicts[model][key]
if isinstance(value, list):
values.extend(value)
else:
values.append(value)
if len(values) == 0:
result[key] = []
continue
# Count the occurrences of each value
value_counts = Counter(values)
# Find the value with the highest count (majority vote)
value_counts = value_counts.most_common(3)
if column_types[key] == "List[str]":
majority_value, max_score = value_counts[0]
majority_value = [
value for value, score in value_counts if score == max_score
]
elif column_types[key] in ["str", "int", "float", "url", "date[year]"]:
majority_value, _ = value_counts[0]
else:
print(column_types[key])
raise
result[key] = majority_value
return result
def compose(dicts, schema="ar"):
column_types = schemata[schema]["column_types"]
result = {}
for key in schemata[schema]["columns"]:
if "List[Dict" in column_types[key]:
result[key] = []
continue
# only use smarter models as a judge
if key in schemata[schema]["evaluation_subsets"]["ACCESSABILITY"]:
models_to_use = ["browsing"]
else:
models_to_use = ["pro", "deepseek", "jury"]
# only use smarter models as a judge
values = []
for model in dicts:
if any([m.lower() in model.lower() for m in models_to_use]):
value = dicts[model][key]
if isinstance(value, list):
values.extend(value)
else:
values.append(value)
if len(values) == 0:
if column_types[key] == "List[str]":
result[key] = []
else:
result[key] = ""
continue
value_counts = Counter(values)
value_counts = value_counts.most_common(3)
if column_types[key] == "List[str]":
majority_value, max_score = value_counts[0]
majority_value = [
value for value, score in value_counts if score == max_score
]
elif column_types[key] in ["str", "int", "float", "url", "date[year]"]:
majority_value, _ = value_counts[0]
else:
print(column_types[key])
raise
result[key] = majority_value
return result
def get_metadata_judge(dicts, type="jury", schema="ar"):
all_metadata = {d["config"]["model_name"]: d["metadata"] for d in dicts}
if type == "jury":
return "", majority_vote(all_metadata, schema=schema)
elif type == "composer":
return "", compose(all_metadata, schema=schema)
else:
raise (f"Unrecognized judge type {type}")
def get_paper_id(link):
return link.split("/")[-1]
def get_dummy_results():
results = {}
results["metadata"] = json.load(open("dummy.json", "r"))
results["cost"] = {
"cost": 0,
"input_tokens": 0,
"output_tokens": 0,
}
results["config"] = {
"model_name": "dummy",
"month": 0,
"year": 0,
"keywords": [],
"link": "",
}
results["ratio_filling"] = 1
results['dummy'] = results
return results
def get_metadata_human(title="", link="", use_split="test", schema="ar"):
dataset = eval_datasets[schema][use_split]
for row in dataset:
if title != "":
if title == row["Paper Title"]:
return row
elif link != "":
if link == fix_arxiv_link(row["Paper Link"]):
return row
else:
raise ()
def compare_results(rs, show_diff=False, schema="ar"):
results = {}
for c in schemata[schema]["columns"]:
for r in rs:
model_name = r["config"]["model_name"]
value = r["metadata"][c]
if c not in results:
results[c] = {}
results[c][model_name] = value
if show_diff:
if all([results[c][m] == value for m in results[c]]):
del results[c]
df = pd.DataFrame(results)
return df.transpose()
def find_best_match(text, options):
"""
Find the option from the provided list that is most similar to the given text.
Args:
text (str): The text to be compared.
options (list): A list of strings to compare the text against.
Returns:
str: The option from the list that is most similar to the text.
"""
# Create a SequenceMatcher object to compare the text with each option
matcher = difflib.SequenceMatcher(None, text.lower(), None)
# Initialize variables to track the best match
best_match = None
best_ratio = 0
# Iterate through the options and find the best match
for option in options:
matcher.set_seq2(option.lower())
ratio = matcher.ratio()
if ratio > best_ratio:
best_match = option
best_ratio = ratio
return best_match
def fix_arxiv_link(link):
for version in range(1, 5):
link = link.replace(f"v{version}", "")
if link.endswith(".pdf"):
link = link.replace(".pdf", "")
_id = link.split("/")[-1]
return f"https://arxiv.org/abs/{_id}"
else:
_id = link.split("/")[-1]
return f"https://arxiv.org/abs/{_id}"
def get_arxiv_id(arxiv_link):
arxiv_link = fix_arxiv_link(arxiv_link)
return arxiv_link.split("/")[-1]
def pick_choice(options, method="last"):
if method == "random":
return random.choice(options)
elif method == "first":
return options[0]
else:
return options[-1]
def fix_options(metadata, method="last", schema="ar"):
fixed_metadata = {}
columns_with_options = [
c
for c in schemata[schema]["schema"]
if "options" in schemata[schema]["schema"][c]
]
for column in metadata:
if column in columns_with_options:
options = [o for o in schemata[schema]["schema"][column]["options"]]
pred_option = metadata[column]
if isinstance(pred_option, list):
new_pred_option = []
for option in pred_option:
if option in options:
new_pred_option.append(option)
else:
new_pred_option.append(find_best_match(option, options))
if new_pred_option == []:
fixed_metadata[column] = [pick_choice(options, method=method)]
else:
fixed_metadata[column] = new_pred_option
elif pred_option in options:
fixed_metadata[column] = pred_option
elif pred_option == "":
fixed_metadata[column] = pick_choice(options, method=method)
else:
fixed_metadata[column] = find_best_match(pred_option, options)
else:
fixed_metadata[column] = metadata[column]
return fixed_metadata
import re
def process_url(url):
url = re.sub(r"\\url\{(.*?)\}", r"\1", url).strip()
url = re.sub("huggingface", "hf", url)
return url
def cast(metadata, schema="ar"):
column_types = schemata[schema]["column_types"]
for c in metadata:
type = column_types[c]
if type == "str":
try:
metadata[c] = str(metadata[c])
except:
metadata[c] = ""
elif type == "int":
try:
metadata[c] = int(metadata[c])
except:
metadata[c] = 0
elif type == "float":
try:
metadata[c] = float(metadata[c])
except:
metadata[c] = 0.0
elif type == "date[year]":
try:
metadata[c] = int(metadata[c])
except:
metadata[c] = date.year
elif type == "url":
try:
metadata[c] = str(metadata[c])
except:
metadata[c] = ""
elif "List" in type:
if not isinstance(metadata[c], list):
raise (f"Error: {metadata[c]} is not a list")
else:
print(c, type)
raise (f"Unrecognized column type {type}")
return metadata
def fill_missing(metadata, schema="ar"):
column_types = schemata[schema]["column_types"]
for c in schemata[schema]["columns"]:
if c not in metadata or metadata[c] is None:
if "List" in column_types[c]:
metadata[c] = []
elif "str" in column_types[c]:
metadata[c] = ""
elif "int" in column_types[c]:
metadata[c] = 0
elif "float" in column_types[c]:
metadata[c] = 0.0
elif "date[year]" in column_types[c]:
metadata[c] = date.today().year
elif "url" in column_types[c]:
metadata[c] = ""
else:
raise (f"Unrecognized column type {column_types[c]}")
return metadata
def postprocess(metadata, method="last", schema="ar"):
metadata = fill_missing(metadata, schema=schema)
metadata = cast(metadata, schema=schema)
metadata = fix_options(metadata, method=method, schema=schema)
return metadata
def removeStartAndEndQuotes(json_str):
if json_str.startswith('"') and json_str.endswith('"'):
print("fixing")
return json_str[1:-1]
else:
return json_str
def singleQuoteToDoubleQuote(singleQuoted):
"""
convert a single quoted string to a double quoted one
Args:
singleQuoted(string): a single quoted string e.g. {'cities': [{'name': "Upper Hell's Gate"}]}
Returns:
string: the double quoted version of the string e.g.
see
- https://stackoverflow.com/questions/55600788/python-replace-single-quotes-with-double-quotes-but-leave-ones-within-double-q
"""
cList = list(singleQuoted)
inDouble = False
inSingle = False
for i, c in enumerate(cList):
# print ("%d:%s %r %r" %(i,c,inSingle,inDouble))
if c == "'":
if not inDouble:
inSingle = not inSingle
cList[i] = '"'
elif c == '"':
inDouble = not inDouble
doubleQuoted = "".join(cList)
return doubleQuoted
def fix_json(json_str: str) -> str:
"""
Attempts to fix common issues in a malformed JSON string.
Args:
broken_json (str): The malformed JSON string.
Returns:
str: The corrected JSON string if fixable, or an error message.
"""
try:
# remove \escaping cahracters
json_str = json_str.replace("\\", "")
# remove start and end quotes
json_str = removeStartAndEndQuotes(json_str)
# replace single quotes to double quotes
json_str = singleQuoteToDoubleQuote(json_str)
loaded_json = json.loads(json_str)
return loaded_json
except json.JSONDecodeError as e:
print(json_str)
print(e)
print("⚠ warning: can not read the josn, using empty {}")
return {}
def read_json(text_json):
text_json = text_json.replace("```json", "").replace("```", "")
fixed_json = fix_json(text_json)
if not isinstance(fixed_json, dict):
raise ("Must be json")
return fixed_json
def get_repo_link(metadata, repo_link=""):
link = ""
if repo_link != "":
link = repo_link
elif "Link" in metadata:
link = metadata["Link"]
elif "HF Link" in metadata:
if metadata["HF Link"] != "":
link = metadata["Link"]
return link
def fetch_repository_metadata(link):
if "hf" in link or "huggingface" in link:
api_url = f"{link}/raw/main/README.md"
response = requests.get(api_url)
readme = response.text
return readme
elif "github" in link:
parts = link.rstrip("/").split("/")
owner = parts[-2]
repo = parts[-1]
base_url = f"https://api.github.com/repos/{owner}/{repo}"
# Fetch repository information
repo_info = requests.get(base_url).json()
# Fetch README
readme_url = f"{base_url}/readme"
readme_response = requests.get(readme_url).json()
readme_content = (
b64decode(readme_response["content"]).decode("utf-8")
if "content" in readme_response
else None
)
# Fetch license
try:
license_info = repo_info.get("license", {}).get("name", "No license found")
except:
license_info = "unknown"
return f"License: {license_info}\nReadme: {readme_content}".strip()
else:
return extract_and_generate_readme(link)