Spaces:
Runtime error
Runtime error
File size: 5,630 Bytes
e51e8f8 58113ed e51e8f8 58113ed 758ca0a 58113ed 850f439 58113ed 8410cc1 70a5579 58113ed 6fd31fb 7360ecc 6fd31fb e51e8f8 77df2c6 968af70 e51e8f8 968af70 e51e8f8 968af70 e51e8f8 968af70 e51e8f8 968af70 850f439 e51e8f8 968af70 e51e8f8 968af70 6fd31fb 70a5579 968af70 e51e8f8 968af70 e51e8f8 968af70 e51e8f8 968af70 58113ed 8410cc1 58113ed 968af70 70a5579 968af70 850f439 968af70 6fd31fb 70a5579 968af70 e51e8f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import datetime
import operator
import pathlib
import pandas as pd
import tqdm.auto
import yaml
from huggingface_hub import HfApi
repo_dir = pathlib.Path(__file__).parent
class DemoList:
COLUMN_INFO = [
['status', 'markdown'],
['hardware', 'markdown'],
['title', 'markdown'],
['owner', 'markdown'],
['arxiv', 'markdown'],
['github', 'markdown'],
['likes', 'number'],
['tags', 'str'],
['last_modified', 'str'],
['created', 'str'],
['sdk', 'markdown'],
['sdk_version', 'str'],
['suggested_hardware', 'markdown'],
['sleep_time', 'markdown'],
['replicas', 'markdown'],
]
TO_TIME_STR = {
-1: 'null',
300: '5 minutes',
600: '10 minutes',
900: '15 minutes',
1800: '30 minutes',
3600: '1 hour',
36000: '10 hours',
86400: '24 hours',
172800: '48 hours',
259200: '72 hours',
604800: '1 week',
}
def __init__(self):
self.api = HfApi()
self._raw_data = self.load_data()
self.df_raw = pd.DataFrame(self._raw_data)
self.df = self.prettify_df()
@property
def column_names(self):
return list(map(operator.itemgetter(0), self.COLUMN_INFO))
@property
def column_datatype(self):
return list(map(operator.itemgetter(1), self.COLUMN_INFO))
@staticmethod
def get_space_id(url: str) -> str:
return '/'.join(url.split('/')[-2:])
def load_data(self) -> list[dict]:
with open(repo_dir / 'list.yaml') as f:
data = yaml.safe_load(f)
res = []
for url in tqdm.auto.tqdm(list(data)):
space_id = self.get_space_id(url)
space_info = self.api.space_info(repo_id=space_id)
card = space_info.cardData
info = data[url]
for tag in ['arxiv', 'github', 'tags']:
if tag not in info:
info[tag] = []
info['url'] = url
info['owner'] = space_id.split('/')[0]
info['title'] = card['title']
info['sdk'] = card['sdk']
info['sdk_version'] = card.get('sdk_version', '')
info['likes'] = space_info.likes
info['last_modified'] = space_info.lastModified
info['status'] = space_info.runtime['stage']
info['sleep_time'] = space_info.runtime['gcTimeout'] or -1
resources = space_info.runtime['resources']
info['replicas'] = -1 if resources is None else resources[
'replicas']
info['suggested_hardware'] = card.get('suggested_hardware', '')
info['hardware'] = space_info.runtime['hardware']['current']
if info['hardware'] is None:
info['hardware'] = space_info.runtime['hardware']['requested']
res.append(info)
return res
def get_arxiv_link(self, links: list[str]) -> str:
links = [self.create_link(link.split('/')[-1], link) for link in links]
return '\n'.join(links)
def get_github_link(self, links: list[str]) -> str:
links = [self.create_link('github', link) for link in links]
return '\n'.join(links)
def get_tag_list(self, tags: list[str]) -> str:
return ', '.join(tags)
@staticmethod
def create_link(text: str, url: str) -> str:
return f'<a href={url} target="_blank">{text}</a>'
def to_div(self, text: str | None, category_name: str) -> str:
if text is None:
text = ''
class_name = f'{category_name}-{text.lower()}'
return f'<div class="{class_name}">{text}</div>'
@staticmethod
def format_timestamp(timestamp: str) -> str:
s = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.000Z')
return s.strftime('%Y/%m/%d %H:%M:%S')
@staticmethod
def add_div_tag_to_replicas(replicas: int) -> str:
if replicas == -1:
return ''
if replicas == 1:
return '1'
return f'<div class="multiple-replicas">{replicas}</div>'
def prettify_df(self) -> pd.DataFrame:
new_rows = []
for _, row in self.df_raw.copy().iterrows():
new_row = {
'status':
self.to_div(row.status, 'status'),
'hardware':
self.to_div(row.hardware, 'hardware'),
'suggested_hardware':
self.to_div(row.suggested_hardware, 'hardware'),
'title':
self.create_link(row.title, row.url),
'owner':
self.create_link(row.owner,
f'https://huggingface.co./{row.owner}'),
'arxiv':
self.get_arxiv_link(row.arxiv),
'github':
self.get_github_link(row.github),
'likes':
row.likes,
'tags':
self.get_tag_list(row.tags),
'last_modified':
self.format_timestamp(row.last_modified),
'created':
self.format_timestamp(row.created),
'sdk':
self.to_div(row.sdk, 'sdk'),
'sdk_version':
row.sdk_version,
'sleep_time':
self.TO_TIME_STR[row.sleep_time],
'replicas':
self.add_div_tag_to_replicas(row.replicas),
}
new_rows.append(new_row)
df = pd.DataFrame(new_rows).loc[:, self.column_names]
return df
|