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 import streamlit.components.v1 as components 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="llama-leaderboard") 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"] return 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( "This is the most comprehensive leaderboard of llama image classifier models published. You can try out the different models below" ) st.markdown( "Please click on the model's name to be redirected to its model card which includes documentation." ) # turn the model ids into clickable links dataframe["Model"] = dataframe["Model"].apply(make_clickable) dataframe = dataframe.sort_values(by=['Accuracy'], ascending=False) table_html = dataframe.to_html(escape=False) table_html = table_html.replace("", '') # left-align the headers st.write(table_html, unsafe_allow_html=True) embed_gradio = components.html( """ """, height=1200, width=700 )