Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
#!/usr/bin/env python | |
from __future__ import annotations | |
import dataclasses | |
import datetime | |
import pathlib | |
import datasets | |
import gradio as gr | |
import pandas as pd | |
import tqdm.auto | |
from gradio_calendar import Calendar | |
from huggingface_hub import HfApi | |
from constants import HARDWARE_CHOICES, SDK_CHOICES, SLEEP_TIME_CHOICES, STATUS_CHOICES | |
from demo_list import DemoInfo, DemoList | |
TITLE = """\ | |
# Spaces of the Week | |
This Space provides a list of past Spaces of the Week with some filtering options. | |
You can find the dataset [here](https://huggingface.co/datasets/hysts-bot/spaces-of-the-week). | |
Also, check out [this Space](https://huggingface.co/spaces/mvaloatto/ASOTW) by [mvaloatto](https://huggingface.co/mvaloatto). | |
""" | |
repo_dir = pathlib.Path(__file__).parent.absolute() | |
api = HfApi() | |
df_orig = datasets.load_dataset("hysts-bot/spaces-of-the-week", split="train").to_pandas() | |
def get_space_info(df: pd.DataFrame, sort_by: list[str], ascending: list[bool]) -> pd.DataFrame: | |
data = [] | |
for _, row in tqdm.auto.tqdm(df.iterrows(), total=len(df)): | |
space_id = row["space_id"] | |
try: | |
info = DemoInfo.from_space_id(space_id) | |
info_dict = dataclasses.asdict(info) | |
info_dict["space_id"] = row["space_id"] | |
info_dict["title"] = row["title"] | |
data.append({"featured_week": row["date"]} | info_dict) | |
except Exception as e: | |
print(f"Failed to load {space_id}: {e}") | |
if row["created_at"]: | |
created = datetime.datetime.fromisoformat(row["created_at"]).strftime("%Y/%m/%d %H:%M:%S") | |
else: | |
created = "" | |
data.append( | |
{ | |
"featured_week": row["date"], | |
"space_id": space_id, | |
"url": f"https://huggingface.co/spaces/{space_id}", | |
"title": row["title"], | |
"owner": space_id.split("/")[0], | |
"sdk": "", | |
"sdk_version": "", | |
"likes": None, | |
"status": "", | |
"last_modified": "", | |
"sleep_time": 0, | |
"replicas": 0, | |
"private": True, | |
"hardware": "", | |
"suggested_hardware": "", | |
"created": created, | |
} | |
) | |
df = pd.DataFrame(data).sort_values(sort_by, ascending=ascending).reset_index(drop=True) | |
df["featured_week"] = pd.to_datetime(df["featured_week"]) | |
return df | |
df_all = get_space_info(df_orig, sort_by=["featured_week", "space_id"], ascending=[False, True]) | |
demo_list = DemoList(df_all) | |
def update_status_checkboxes(choices: list[str]) -> list[str]: | |
if "(ALL)" in choices: | |
return STATUS_CHOICES | |
elif "(NONE)" in choices: | |
return [] | |
else: | |
return choices | |
def update_hardware_checkboxes(choices: list[str]) -> list[str]: | |
if "(ALL)" in choices: | |
return HARDWARE_CHOICES | |
elif "(NONE)" in choices: | |
return [] | |
else: | |
return choices | |
def update_sdk_checkboxes(choices: list[str]) -> list[str]: | |
if "(ALL)" in choices: | |
return SDK_CHOICES | |
elif "(NONE)" in choices: | |
return [] | |
else: | |
return choices | |
def update_sleep_time_checkboxes(choices: list[str]) -> list[str]: | |
if "(ALL)" in choices: | |
return SLEEP_TIME_CHOICES | |
elif "(NONE)" in choices: | |
return [] | |
else: | |
return choices | |
DEFAULT_COLUMNS = [ | |
"featured_week", | |
"status", | |
"hardware", | |
"title", | |
"owner", | |
"likes", | |
"created", | |
"sdk", | |
] | |
FEATURED_WEEKS = demo_list.df_raw.featured_week.unique().tolist() | |
DEFAULT_FEATURED_WEEKS = FEATURED_WEEKS[:4] | |
def update_df( | |
status: list[str], | |
hardware: list[str], | |
sdk: list[str], | |
sleep_time: list[str], | |
multiple_replicas: bool, | |
owner: str, | |
start_date: datetime.datetime, | |
end_date: datetime.datetime, | |
column_names: list[str], | |
) -> pd.DataFrame: | |
return gr.DataFrame( | |
value=demo_list.filter( | |
status, | |
hardware, | |
sdk, | |
sleep_time, | |
multiple_replicas, | |
owner, | |
start_date, | |
end_date, | |
column_names, | |
), | |
datatype=demo_list.get_column_datatypes(column_names), | |
) | |
def update_num_spaces(df: pd.DataFrame) -> str: | |
return f"{len(df)} / {len(demo_list.df_raw)}" | |
with gr.Blocks(css="style.css") as demo: | |
gr.Markdown(TITLE) | |
with gr.Accordion(label="Filter", open=True): | |
with gr.Group(): | |
with gr.Row(): | |
start_date = Calendar(label="Start date", type="datetime", value="2021-10-18") | |
end_date = Calendar(label="End date", type="datetime") | |
with gr.Accordion(label="Advanced", open=False): | |
status = gr.CheckboxGroup( | |
label="Status", | |
choices=["(ALL)", "(NONE)"] + STATUS_CHOICES, | |
value=STATUS_CHOICES, | |
type="value", | |
) | |
hardware = gr.CheckboxGroup( | |
label="Hardware", | |
choices=["(ALL)", "(NONE)"] + HARDWARE_CHOICES, | |
value=HARDWARE_CHOICES, | |
type="value", | |
) | |
sdk = gr.CheckboxGroup( | |
label="SDK", | |
choices=["(ALL)", "(NONE)"] + SDK_CHOICES, | |
value=SDK_CHOICES, | |
type="value", | |
) | |
sleep_time = gr.CheckboxGroup( | |
label="Sleep time", | |
choices=["(ALL)", "(NONE)"] + SLEEP_TIME_CHOICES, | |
value=SLEEP_TIME_CHOICES, | |
type="value", | |
) | |
multiple_replicas = gr.Checkbox(label="Multiple replicas", value=False) | |
owner = gr.Dropdown( | |
label="Owner", | |
choices=["(ALL)"] + sorted(demo_list.df_raw.owner.unique().tolist()), | |
value="(ALL)", | |
) | |
with gr.Group(): | |
column_names = gr.CheckboxGroup(label="Columns", choices=demo_list.column_names, value=DEFAULT_COLUMNS) | |
apply_button = gr.Button("Apply") | |
num_spaces = gr.Textbox(label="Number of Spaces", interactive=False) | |
df = gr.Dataframe( | |
value=demo_list.df_prettified, | |
datatype=demo_list.get_column_datatypes(demo_list.column_names), | |
type="pandas", | |
row_count=(0, "dynamic"), | |
height=1000, | |
elem_id="table", | |
interactive=False, | |
) | |
status.input( | |
fn=update_status_checkboxes, | |
inputs=status, | |
outputs=status, | |
queue=False, | |
show_progress=False, | |
api_name=False, | |
) | |
hardware.input( | |
fn=update_hardware_checkboxes, | |
inputs=hardware, | |
outputs=hardware, | |
queue=False, | |
show_progress=False, | |
api_name=False, | |
) | |
sdk.input( | |
fn=update_sdk_checkboxes, | |
inputs=sdk, | |
outputs=sdk, | |
queue=False, | |
show_progress=False, | |
api_name=False, | |
) | |
sleep_time.input( | |
fn=update_sleep_time_checkboxes, | |
inputs=sleep_time, | |
outputs=sleep_time, | |
queue=False, | |
show_progress=False, | |
api_name=False, | |
) | |
inputs = [ | |
status, | |
hardware, | |
sdk, | |
sleep_time, | |
multiple_replicas, | |
owner, | |
start_date, | |
end_date, | |
column_names, | |
] | |
apply_button.click( | |
fn=update_df, | |
inputs=inputs, | |
outputs=df, | |
api_name=False, | |
).then( | |
fn=update_num_spaces, | |
inputs=df, | |
outputs=num_spaces, | |
queue=False, | |
api_name=False, | |
) | |
demo.load( | |
fn=update_df, | |
inputs=inputs, | |
outputs=df, | |
api_name=False, | |
).then( | |
fn=update_num_spaces, | |
inputs=df, | |
outputs=num_spaces, | |
queue=False, | |
api_name=False, | |
) | |
if __name__ == "__main__": | |
demo.queue(api_open=False).launch(show_api=False) | |