Hugues Sibille commited on
Commit
187990b
1 Parent(s): b931cb1

refactor : break app.py in different files

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .venv
2
+ *.json
3
+ *.pyc
app/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+
app/utils.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from data.model_handler import ModelHandler
2
+
3
+ def make_clickable_model(model_name, link=None):
4
+ if link is None:
5
+ desanitized_model_name = model_name.replace("_", "/")
6
+
7
+ if '/captioning' in desanitized_model_name:
8
+ desanitized_model_name = desanitized_model_name.replace('/captioning', '')
9
+ if '/ocr' in desanitized_model_name:
10
+ desanitized_model_name = desanitized_model_name.replace('/ocr', '')
11
+
12
+ link = "https://huggingface.co/" + desanitized_model_name
13
+
14
+ return f'<a target="_blank" style="text-decoration: underline" href="{link}">{model_name}</a>'
15
+
16
+
17
+ def add_rank_and_format(df):
18
+ df = df.reset_index()
19
+ df = df.rename(columns={"index": "Model"})
20
+ df = ModelHandler.add_rank(df)
21
+ df["Model"] = df["Model"].apply(make_clickable_model)
22
+ return df
23
+
24
+ def get_refresh_function():
25
+ def _refresh(metric):
26
+ model_handler = ModelHandler()
27
+ data_task_category = model_handler.get_vidore_data(metric)
28
+ df = add_rank_and_format(data_task_category)
29
+ return df
30
+
31
+ return _refresh
data/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+
data/dataset_handler.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict
2
+ from huggingface_hub import get_collection
3
+
4
+
5
+ def get_datasets_nickname() -> Dict:
6
+ datasets_nickname = {}
7
+
8
+ collection = get_collection("vidore/vidore-benchmark-667173f98e70a1c0fa4db00d")
9
+ collection_items = collection.items
10
+
11
+ for item in collection_items:
12
+ dataset_name = item.item_id
13
+
14
+ if 'arxivqa' in dataset_name:
15
+ datasets_nickname[dataset_name] = 'ArxivQA'
16
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'ArxivQA'
17
+ datasets_nickname[dataset_name + '_captioning'] = 'ArxivQA'
18
+
19
+ elif 'docvqa' in dataset_name:
20
+ datasets_nickname[dataset_name] = 'DocVQA'
21
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'DocVQA'
22
+ datasets_nickname[dataset_name + '_captioning'] = 'DocVQA'
23
+
24
+ elif 'infovqa' in dataset_name:
25
+ datasets_nickname[dataset_name] = 'InfoVQA'
26
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'InfoVQA'
27
+ datasets_nickname[dataset_name + '_captioning'] = 'InfoVQA'
28
+
29
+ elif 'tabfquad' in dataset_name:
30
+ datasets_nickname[dataset_name] = 'TabFQuad'
31
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'TabFQuad'
32
+ datasets_nickname[dataset_name + '_captioning'] = 'TabFQuad'
33
+
34
+ elif 'tatdqa' in dataset_name:
35
+ datasets_nickname[dataset_name] = 'TATDQA'
36
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'TATDQA'
37
+ datasets_nickname[dataset_name + '_captioning'] = 'TATDQA'
38
+
39
+ elif 'shiftproject' in dataset_name:
40
+ datasets_nickname[dataset_name] = 'ShiftProject'
41
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'ShiftProject'
42
+ datasets_nickname[dataset_name + '_captioning'] = 'ShiftProject'
43
+
44
+ elif 'artificial_intelligence' in dataset_name:
45
+ datasets_nickname[dataset_name] = 'Artificial Intelligence'
46
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'Artificial Intelligence'
47
+ datasets_nickname[dataset_name + '_captioning'] = 'Artificial Intelligence'
48
+
49
+ elif 'energy' in dataset_name:
50
+ datasets_nickname[dataset_name] = 'Energy'
51
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'Energy'
52
+ datasets_nickname[dataset_name + '_captioning'] = 'Energy'
53
+
54
+ elif 'government_reports' in dataset_name:
55
+ datasets_nickname[dataset_name] = 'Government Reports'
56
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'Government Reports'
57
+ datasets_nickname[dataset_name + '_captioning'] = 'Government Reports'
58
+
59
+ elif 'healthcare' in dataset_name:
60
+ datasets_nickname[dataset_name] = 'Healthcare'
61
+ datasets_nickname[dataset_name + '_ocr_chunk'] = 'Healthcare'
62
+ datasets_nickname[dataset_name + '_captioning'] = 'Healthcare'
63
+
64
+ return datasets_nickname
data/model_handler.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from typing import Dict
4
+ from huggingface_hub import HfApi, hf_hub_download, metadata_load
5
+ import pandas as pd
6
+ from .dataset_handler import get_datasets_nickname
7
+
8
+ class ModelHandler:
9
+ def __init__(self, model_infos_path="model_infos.json"):
10
+ self.api = HfApi()
11
+ self.model_infos_path = model_infos_path
12
+ self.model_infos = self._load_model_infos()
13
+
14
+ def _load_model_infos(self) -> Dict:
15
+ if os.path.exists(self.model_infos_path):
16
+ with open(self.model_infos_path) as f:
17
+ return json.load(f)
18
+ return {}
19
+
20
+ def _save_model_infos(self):
21
+ with open(self.model_infos_path, "w") as f:
22
+ json.dump(self.model_infos, f)
23
+
24
+ def get_vidore_data(self, metric="ndcg_at_5"):
25
+ models = self.api.list_models(filter="vidore")
26
+ repositories = [model.modelId for model in models] # type: ignore
27
+
28
+ datasets_nickname = get_datasets_nickname()
29
+ for repo_id in repositories:
30
+ files = [f for f in self.api.list_repo_files(repo_id) if f.endswith('_metrics.json')]
31
+ if len(files) == 0:
32
+ continue
33
+ else:
34
+ for file in files:
35
+ model_name = file.split('_metrics.json')[0]
36
+
37
+ if model_name not in self.model_infos:
38
+ readme_path = hf_hub_download(repo_id, filename="README.md")
39
+ meta = metadata_load(readme_path)
40
+ try:
41
+ result_path = hf_hub_download(repo_id, filename=file)
42
+
43
+ with open(result_path) as f:
44
+ results = json.load(f)
45
+
46
+ for dataset in results:
47
+ results[dataset] = {key: value for key, value in results[dataset].items()}
48
+
49
+ self.model_infos[model_name] = {"meta": meta, "results": results}
50
+ except Exception as e:
51
+ print(f"Error loading {model_name} - {e}")
52
+ continue
53
+
54
+ #self._save_model_infos()
55
+
56
+ model_res = {}
57
+ if len(self.model_infos) > 0:
58
+ for model in self.model_infos.keys():
59
+ res = self.model_infos[model]["results"]
60
+ dataset_res = {}
61
+ for dataset in res.keys():
62
+ if "validation_set" == dataset:
63
+ continue
64
+ dataset_res[datasets_nickname[dataset]] = res[dataset][metric]
65
+ model_res[model] = dataset_res
66
+
67
+ df = pd.DataFrame(model_res).T
68
+ return df
69
+ return pd.DataFrame()
70
+
71
+ @staticmethod
72
+ def add_rank(df):
73
+ cols_to_rank = [
74
+ col
75
+ for col in df.columns
76
+ if col
77
+ not in [
78
+ "Model",
79
+ "Model Size (Million Parameters)",
80
+ "Memory Usage (GB, fp32)",
81
+ "Embedding Dimensions",
82
+ "Max Tokens",
83
+ ]
84
+ ]
85
+ if len(cols_to_rank) == 1:
86
+ df.sort_values(cols_to_rank[0], ascending=False, inplace=True)
87
+ else:
88
+ df.insert(len(df.columns) - len(cols_to_rank), "Average", df[cols_to_rank].mean(axis=1, skipna=False))
89
+ df.sort_values("Average", ascending=False, inplace=True)
90
+ df.insert(0, "Rank", list(range(1, len(df) + 1)))
91
+ df = df.round(2)
92
+ # Fill NaN after averaging
93
+ df.fillna("", inplace=True)
94
+ return df