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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
from envs import API, EVAL_REQUESTS_PATH, TOKEN, QUEUE_REPO
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
custom_css = """
|
| 13 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn&display=swap');
|
| 14 |
+
body, .gradio-container, .gr-button, .gr-input, .gr-slider, .gr-dropdown, .gr-markdown {
|
| 15 |
+
font-family: 'Vazirmatn', sans-serif !important;
|
| 16 |
+
}
|
| 17 |
+
.markdown-text {
|
| 18 |
+
font-size: 16px !important;
|
| 19 |
+
}
|
| 20 |
+
#models-to-add-text {
|
| 21 |
+
font-size: 18px !important;
|
| 22 |
+
}
|
| 23 |
+
#citation-button span {
|
| 24 |
+
font-size: 16px !important;
|
| 25 |
+
}
|
| 26 |
+
#citation-button textarea {
|
| 27 |
+
font-size: 16px !important;
|
| 28 |
+
}
|
| 29 |
+
#citation-button > label > button {
|
| 30 |
+
margin: 6px;
|
| 31 |
+
transform: scale(1.3);
|
| 32 |
+
}
|
| 33 |
+
#leaderboard-table {
|
| 34 |
+
margin-top: 15px;
|
| 35 |
+
text-align: center;
|
| 36 |
+
}
|
| 37 |
+
#leaderboard-table,
|
| 38 |
+
#leaderboard-table th,
|
| 39 |
+
#leaderboard-table td {
|
| 40 |
+
text-align: center;
|
| 41 |
+
vertical-align: middle;
|
| 42 |
+
border-collapse: collapse;
|
| 43 |
+
}
|
| 44 |
+
#leaderboard-table td:first-child,
|
| 45 |
+
#leaderboard-table th:first-child {
|
| 46 |
+
text-align: left;
|
| 47 |
+
max-width: 600px;
|
| 48 |
+
}
|
| 49 |
+
table > thead {
|
| 50 |
+
white-space: normal;
|
| 51 |
+
}
|
| 52 |
+
table > thead th,
|
| 53 |
+
table > tbody td {
|
| 54 |
+
text-align: center;
|
| 55 |
+
vertical-align: middle;
|
| 56 |
+
}
|
| 57 |
+
table > tbody td:first-child {
|
| 58 |
+
text-align: left;
|
| 59 |
+
max-width: 600px;
|
| 60 |
+
}
|
| 61 |
+
#leaderboard-table-lite {
|
| 62 |
+
margin-top: 15px;
|
| 63 |
+
}
|
| 64 |
+
#search-bar-table-box > div:first-child {
|
| 65 |
+
background: none;
|
| 66 |
+
border: none;
|
| 67 |
+
}
|
| 68 |
+
#search-bar {
|
| 69 |
+
padding: 0px;
|
| 70 |
+
}
|
| 71 |
+
.tab-buttons button {
|
| 72 |
+
font-size: 20px;
|
| 73 |
+
}
|
| 74 |
+
#scale-logo {
|
| 75 |
+
border-style: none !important;
|
| 76 |
+
box-shadow: none;
|
| 77 |
+
display: block;
|
| 78 |
+
margin-left: auto;
|
| 79 |
+
margin-right: auto;
|
| 80 |
+
max-width: 600px;
|
| 81 |
+
}
|
| 82 |
+
#scale-logo .download {
|
| 83 |
+
display: none;
|
| 84 |
+
}
|
| 85 |
+
#filter_type {
|
| 86 |
+
border: 0;
|
| 87 |
+
padding-left: 0;
|
| 88 |
+
padding-top: 0;
|
| 89 |
+
}
|
| 90 |
+
#filter_type label {
|
| 91 |
+
display: flex;
|
| 92 |
+
}
|
| 93 |
+
#filter_type label > span {
|
| 94 |
+
margin-top: var(--spacing-lg);
|
| 95 |
+
margin-right: 0.5em;
|
| 96 |
+
}
|
| 97 |
+
#filter_type label > .wrap {
|
| 98 |
+
width: 103px;
|
| 99 |
+
}
|
| 100 |
+
#filter_type label > .wrap .wrap-inner {
|
| 101 |
+
padding: 2px;
|
| 102 |
+
}
|
| 103 |
+
#filter_type label > .wrap .wrap-inner input {
|
| 104 |
+
width: 1px;
|
| 105 |
+
}
|
| 106 |
+
#filter-columns-type {
|
| 107 |
+
border: 0;
|
| 108 |
+
padding: 0.5;
|
| 109 |
+
}
|
| 110 |
+
#filter-columns-size {
|
| 111 |
+
border: 0;
|
| 112 |
+
padding: 0.5;
|
| 113 |
+
}
|
| 114 |
+
#box-filter > .form {
|
| 115 |
+
border: 0;
|
| 116 |
+
}
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
ABOUT_TEXT = f"""
|
| 121 |
+
# Persian Text Embedding Benchmark (v1.0.0)
|
| 122 |
+
"""
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
SUBMIT_TEXT = """## Submitting a Model for Evaluation
|
| 126 |
+
|
| 127 |
+
> To submit your open-source model for evaluation, follow these steps:
|
| 128 |
+
>
|
| 129 |
+
> 1. **Ensure your model is on Hugging Face**: Your model must be publicly available on [Hugging Face](https://huggingface.co/).
|
| 130 |
+
>
|
| 131 |
+
> 2. **Submit Request**: Send a request with your model's Hugging Face identifier.
|
| 132 |
+
>
|
| 133 |
+
> 3. **Manual Queue**: Please note that the evaluation process is currently handled manually. Submissions will be queued and processed as soon as possible.
|
| 134 |
+
>
|
| 135 |
+
> 4. **Results**: Once the evaluation is complete, your model’s results will be updated on the leaderboard.
|
| 136 |
+
>
|
| 137 |
+
> We appreciate your patience and contributions to the Persian LM ecosystem!
|
| 138 |
+
"""
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
PART_LOGO = """
|
| 142 |
+
<img src="https://avatars.githubusercontent.com/u/39557177?v=4" style="width:30%;display:block;margin-left:auto;margin-right:auto">
|
| 143 |
+
<h1 style="font-size: 28px; margin-bottom: 2px;">Part DP AI</h1>
|
| 144 |
+
"""
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def load_jsonl(input_file):
|
| 148 |
+
data = []
|
| 149 |
+
with open(input_file, 'r') as f:
|
| 150 |
+
for line in f:
|
| 151 |
+
data.append(json.loads(line))
|
| 152 |
+
return data
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def jsonl_to_dataframe(input_file):
|
| 156 |
+
data = load_jsonl(input_file)
|
| 157 |
+
return pd.DataFrame(data)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def sort_dataframe_by_column(df, column_name):
|
| 161 |
+
if column_name not in df.columns:
|
| 162 |
+
raise ValueError(f"Column '{column_name}' does not exist in the DataFrame.")
|
| 163 |
+
return df.sort_values(by=column_name, ascending=False).reset_index(drop=True)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def add_average_column_to_df(df,columns_to_average, index=3, average_column_name="Average Accuracy"):
|
| 167 |
+
average_column = df[columns_to_average].mean(axis=1)
|
| 168 |
+
df.insert(index, average_column_name, average_column)
|
| 169 |
+
return df
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def model_hyperlink(link, model_name):
|
| 173 |
+
return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{model_name}</a>'
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def make_clickable_model(model_name):
|
| 177 |
+
link = f"https://huggingface.co/{model_name}"
|
| 178 |
+
return model_hyperlink(link, model_name)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def center_align_markdown(text):
|
| 182 |
+
return f'<div align="center">{text}</div>'
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def apply_markdown_format_for_columns(df, model_column_name):
|
| 186 |
+
columns = list(df.columns)
|
| 187 |
+
df[model_column_name] = df[model_column_name].apply(make_clickable_model)
|
| 188 |
+
# for column in columns:
|
| 189 |
+
# if column != model_column_name:
|
| 190 |
+
# df[column] = df[column].apply(center_align_markdown)
|
| 191 |
+
return df
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def submit(model_name, model_id, contact_email):
|
| 195 |
+
if model_name == "" or model_id == "" or contact_email == "":
|
| 196 |
+
gr.Info("Please fill all the fields")
|
| 197 |
+
return
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
try:
|
| 201 |
+
user_name = ""
|
| 202 |
+
if "/" in model_id:
|
| 203 |
+
user_name = model_id.split("/")[0]
|
| 204 |
+
model_path = model_id.split("/")[1]
|
| 205 |
+
|
| 206 |
+
eval_entry = {
|
| 207 |
+
"model_name": model_name,
|
| 208 |
+
"model_id": model_id,
|
| 209 |
+
"contact_email": contact_email,
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
# Get the current timestamp to add to the filename
|
| 213 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 214 |
+
|
| 215 |
+
OUT_DIR = f"{EVAL_REQUESTS_PATH}/{user_name}"
|
| 216 |
+
os.makedirs(OUT_DIR, exist_ok=True)
|
| 217 |
+
|
| 218 |
+
# Add the timestamp to the filename
|
| 219 |
+
out_path = f"{OUT_DIR}/{user_name}_{model_path}_{timestamp}.json"
|
| 220 |
+
|
| 221 |
+
with open(out_path, "w") as f:
|
| 222 |
+
f.write(json.dumps(eval_entry))
|
| 223 |
+
|
| 224 |
+
print("Uploading eval file")
|
| 225 |
+
API.upload_file(
|
| 226 |
+
path_or_fileobj=out_path,
|
| 227 |
+
path_in_repo=out_path.split("eval-queue/")[1],
|
| 228 |
+
repo_id=QUEUE_REPO,
|
| 229 |
+
repo_type="dataset",
|
| 230 |
+
commit_message=f"Add {model_name} to eval queue",
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
gr.Info("Successfully submitted", duration=10)
|
| 234 |
+
# Remove the local file
|
| 235 |
+
os.remove(out_path)
|
| 236 |
+
except Exception as e:
|
| 237 |
+
gr.Error(f"Error submitting the model: {e}")
|