Download app.py from merve/notebooks: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/spaces/merve/notebooks/resolve/main/app.py
- Command line
-
hf download hf://spaces/merve/notebooks/app.py
-
curl -L -o app.py https://huggingface.co/spaces/merve/notebooks/resolve/main/app.py
3.19 kB
| __all__ = ['block', 'make_clickable_model', 'make_clickable_user', 'get_submissions'] | |
| import gradio as gr | |
| import pandas as pd | |
| from huggingface_hub import HfApi, repocard | |
| #def is_duplicated(space_id:str)->None: | |
| # card = repocard.RepoCard.load(space_id, repo_type="space") | |
| # return getattr(card.data, "duplicated_from", None) is not None | |
| def make_clickable_model(model_name, repo_type, link=None): | |
| if link is None: | |
| if repo_type == "Dataset": | |
| link = "https://huggingface.co/" + "datasets/" + model_name | |
| else: | |
| link = "https://huggingface.co/" + model_name | |
| return f'<a target="_blank" href="{link}">{model_name.split("/")[-1]}</a>' | |
| def get_repo_ids(repo_type): | |
| api = HfApi() | |
| if repo_type == "Model": | |
| notebooks = api.list_models(filter=["notebook-favorites"]) | |
| elif repo_type == "Dataset": | |
| notebooks = api.list_datasets(filter=["notebook-favorites"]) | |
| print(notebooks) | |
| notebook_ids = [x for x in notebooks] | |
| return notebook_ids | |
| def make_clickable_user(user_id): | |
| link = "https://huggingface.co/" + user_id | |
| return f'<a target="_blank" href="{link}">{user_id}</a>' | |
| def get_submissions(repo_type): | |
| submissions = get_repo_ids(repo_type) | |
| leaderboard_models = [] | |
| for submission in submissions: | |
| # user, model, likes | |
| #if not is_duplicated(submission.id): | |
| user_id = submission.id.split("/")[0] | |
| leaderboard_models.append( | |
| ( | |
| make_clickable_user(user_id), | |
| make_clickable_model(submission.id, repo_type), | |
| submission.likes, | |
| ) | |
| ) | |
| df = pd.DataFrame(data=leaderboard_models, columns=["User", "Repository", "Likes"]) | |
| df.sort_values(by=["Likes"], ascending=False, inplace=True) | |
| df.insert(0, "Rank", list(range(1, len(df) + 1))) | |
| return df | |
| block = gr.Blocks() | |
| with block: | |
| gr.Markdown( | |
| """# Notebooks Leaderboard | |
| This Space compiles coolest model and dataset repositories that contain notebooks! | |
| """ | |
| ) | |
| with gr.Tabs(): | |
| with gr.TabItem("Notebooks in Model Repositories 🤖"): | |
| with gr.Row(): | |
| model_data = gr.components.Dataframe( | |
| type="pandas", datatype=["number", "markdown", "markdown", "number"] | |
| ) | |
| with gr.Row(): | |
| data_run = gr.Button("Refresh") | |
| data_run.click( | |
| get_submissions, inputs=gr.Variable("Model"), outputs=model_data | |
| ) | |
| with gr.TabItem("Notebooks in Dataset Repositories 📖"): | |
| with gr.Row(): | |
| dataset_data = gr.components.Dataframe( | |
| type="pandas", datatype=["number", "markdown", "markdown", "number"] | |
| ) | |
| with gr.Row(): | |
| data_run = gr.Button("Refresh") | |
| data_run.click( | |
| get_submissions, inputs=gr.Variable("Dataset"), outputs=dataset_data | |
| ) | |
| block.load(get_submissions, inputs=gr.Variable("Model"), outputs=model_data) | |
| block.load(get_submissions, inputs=gr.Variable("Dataset"), outputs=dataset_data) | |
| block.launch(debug=True) |