|
import pandas as pd |
|
import os |
|
import fnmatch |
|
import json |
|
import re |
|
import numpy as np |
|
import requests |
|
from urllib.parse import quote |
|
from datetime import datetime |
|
|
|
|
|
|
|
class DetailsDataProcessor: |
|
|
|
|
|
|
|
def __init__(self, directory='results', pattern='results*.json'): |
|
self.directory = directory |
|
self.pattern = pattern |
|
|
|
|
|
|
|
def _find_files(self, directory='results', pattern='results*.json'): |
|
matching_files = [] |
|
for root, dirs, files in os.walk(directory): |
|
for basename in files: |
|
if fnmatch.fnmatch(basename, pattern): |
|
filename = os.path.join(root, basename) |
|
matching_files.append(filename) |
|
return matching_files |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod |
|
def download_file(url, save_file_path): |
|
|
|
timestamp = datetime.now() |
|
|
|
|
|
filename_timestamp = timestamp.strftime("%Y-%m-%dT%H-%M-%S") |
|
|
|
|
|
save_file_path = save_file_path + filename_timestamp + ".json" |
|
|
|
print(save_file_path) |
|
|
|
try: |
|
|
|
r = requests.get(url, allow_redirects=True) |
|
r.raise_for_status() |
|
|
|
|
|
with open(save_file_path, 'wb') as file: |
|
file.write(r.content) |
|
|
|
print(f"Successfully downloaded file: {save_file_path}") |
|
except requests.ConnectionError as e: |
|
print(f"Failed to connect to the URL: {url}") |
|
raise e |
|
except requests.HTTPError as e: |
|
print(f"HTTP error occurred: {e}") |
|
raise e |
|
except FileNotFoundError as e: |
|
print(f"File not found at path: {save_file_path}") |
|
raise e |
|
except Exception as e: |
|
print(f"An unexpected error occurred: {e}") |
|
raise e |
|
|
|
return None |
|
|
|
|
|
|
|
@staticmethod |
|
def single_file_pipeline(url, filename): |
|
DetailsDataProcessor.download_file(url, filename) |
|
|
|
with open(filename) as f: |
|
data = json.load(f) |
|
|
|
df = pd.DataFrame(data) |
|
return df |
|
|
|
@staticmethod |
|
def build_url(file_path): |
|
segments = file_path.split('/') |
|
bits = segments[1] |
|
model_name = segments[2] |
|
timestamp = segments[3].split('_')[1] |
|
|
|
url = f'https://huggingface.co/datasets/open-llm-leaderboard/details/resolve/main/{bits}/{model_name}/details_harness%7ChendrycksTest-moral_scenarios%7C5_{quote(timestamp, safe="")}' |
|
print(url) |
|
return url |
|
|
|
def pipeline(self): |
|
dataframes = [] |
|
file_paths = self._find_files(self.directory, self.pattern) |
|
for file_path in file_paths: |
|
print(file_path) |
|
url = self.generate_url(file_path) |
|
file_path = file_path.split('/')[-1] |
|
df = self.single_file_pipeline(url, file_path) |
|
dataframes.append(df) |
|
return dataframes |
|
|