import requests import json import pandas as pd from tqdm.auto import tqdm import streamlit as st from huggingface_hub import HfApi, hf_hub_download from huggingface_hub.repocard import metadata_load def make_clickable(model_name): link = "https://huggingface.co/" + model_name return f'{model_name}' def get_model_ids(): api = HfApi() models = api.list_models(filter="llamaleaderboard") model_ids = [x.modelId for x in models] return model_ids def get_metadata(model_id): try: readme_path = hf_hub_download(model_id, filename="README.md") return metadata_load(readme_path) except requests.exceptions.HTTPError: # 404 README.md not found return None def parse_metrics_accuracy(meta): if "model-index" not in meta: return None result = meta["model-index"][0]["results"] metrics = result[0]["metrics"] accuracy = metrics[0]["value"] yield accuracy @st.cache(ttl=600) def get_data(): data = [] model_ids = get_model_ids() for model_id in tqdm(model_ids): meta = get_metadata(model_id) if meta is None: continue row = {} row["Model"] = model_id row["accuracy"] = parse_metrics_accuracy(meta) data.append(row) return pd.DataFrame.from_records(data) dataframe = get_data() dataframe = dataframe.fillna("") st.markdown("# The 🦙 Leaderboard") st.markdown( f"This is a leaderboard of **{len(dataframe)}** llama classification models.\n\n" ) st.markdown( "Please click on the model's name to be redirected to its model card which includes documentation and examples on how to use it." ) dataset_df = dataframe.reset_index(drop=True) dataset_df.index += 1 # turn the model ids into clickable links dataset_df["Model"] = dataset_df["Model"].apply(make_clickable) table_html = dataset_df.to_html(escape=False) table_html = table_html.replace("", '') # left-align the headers st.write(table_html, unsafe_allow_html=True)