Spaces:
Runtime error
Runtime error
File size: 11,804 Bytes
9ac5ea2 ebe86df 38e70c4 ebe86df fe8da28 38e70c4 ebe86df c30da09 b5bfbc4 1bddee8 c30da09 fe8da28 cef1f1e fe8da28 cef1f1e 1bddee8 fe8da28 1bddee8 fe8da28 cef1f1e fe8da28 1bddee8 fe8da28 1bddee8 b5bfbc4 38e70c4 fe8da28 38e70c4 cef1f1e 38e70c4 c30da09 1bddee8 c30da09 e3dc221 1bddee8 38e70c4 9ac5ea2 38e70c4 1bddee8 38e70c4 1bddee8 cef1f1e fe8da28 cef1f1e fe8da28 1bddee8 fe8da28 1bddee8 fe8da28 38e70c4 9ac5ea2 38e70c4 cef1f1e 38e70c4 1bddee8 cef1f1e 38e70c4 b5bfbc4 1bddee8 cef1f1e 38e70c4 1bddee8 9ac5ea2 cef1f1e b5bfbc4 cef1f1e b5bfbc4 cef1f1e b5bfbc4 cef1f1e b5bfbc4 fe8da28 b5bfbc4 fe8da28 c16d907 b5bfbc4 cef1f1e b5bfbc4 1bddee8 cef1f1e 9ac5ea2 b5bfbc4 1bddee8 cef1f1e b5bfbc4 1bddee8 b5bfbc4 cef1f1e fe8da28 cef1f1e 38e70c4 cef1f1e 38e70c4 cef1f1e 38e70c4 9ac5ea2 fe8da28 38e70c4 fe8da28 c16d907 fe8da28 cef1f1e 38e70c4 cef1f1e 38e70c4 cef1f1e 38e70c4 cef1f1e 38e70c4 bd334dc 38e70c4 bd334dc 12e35a6 38e70c4 |
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import collections
import os
from datetime import datetime, timedelta
import json
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs, urlparse
from huggingface_hub import list_datasets, set_access_token, HfFolder
from datasets import load_dataset, DatasetDict, Dataset
import numpy as np
HF_TOKEN = os.environ['HF_TOKEN']
set_access_token(HF_TOKEN)
HfFolder.save_token(HF_TOKEN)
datasets = {
"stars": load_dataset("open-source-metrics/stars").sort('dates'),
"issues": load_dataset("open-source-metrics/issues").sort('dates'),
"pip": load_dataset("open-source-metrics/pip").sort('day'),
}
external_datasets = {
"stars": load_dataset("open-source-metrics/stars-external").sort('dates'),
"issues": load_dataset("open-source-metrics/issues-external").sort('dates'),
"pip": load_dataset("open-source-metrics/pip-external").sort('day')
}
val = 0
def _range(e):
global val
e['range'] = val
val += 1
current_date = datetime.strptime(e['dates'], "%Y-%m-%dT%H:%M:%SZ")
first_date = datetime.fromtimestamp(1)
week = abs(current_date - first_date).days // 7
e['week'] = week
return e
def _ignore_org_members(e):
global val
e['range_non_org'] = val
if e['type']['authorAssociation'] != 'MEMBER':
val += 1
return e
stars = {}
for k, v in datasets['stars'].items():
stars[k] = v.map(_range)
val = 0
stars_external = {}
for k, v in external_datasets['stars'].items():
stars_external[k] = v.map(_range)
val = 0
issues = {}
for k, v in datasets['issues'].items():
issues[k] = v.map(_range)
val = 0
issues[k] = issues[k].map(_ignore_org_members)
val = 0
issues_external = {}
for k, v in external_datasets['issues'].items():
issues_external[k] = v.map(_range)
val = 0
issues_external[k] = issues_external[k].map(_ignore_org_members)
val = 0
datasets['stars'] = DatasetDict(**stars)
datasets['issues'] = DatasetDict(**issues)
external_datasets['stars'] = DatasetDict(**stars_external)
external_datasets['issues'] = DatasetDict(**issues_external)
def link_values(library_names, returned_values):
previous_values = {library_name: None for library_name in library_names}
for library_name in library_names:
for i in returned_values.keys():
if library_name not in returned_values[i]:
returned_values[i][library_name] = previous_values[library_name]
else:
previous_values[library_name] = returned_values[i][library_name]
return returned_values
def running_mean(x, N, total_length=-1):
cumsum = np.cumsum(np.insert(x, 0, 0))
to_pad = max(total_length - len(cumsum), 0)
return np.pad(cumsum[N:] - cumsum[:-N], (to_pad, 0)) / float(N)
def parse_name_and_options(path):
url = urlparse(path)
query = parse_qs(url.query)
library_names = query.get("input", None)[0]
library_names = library_names.split(',')
options = query.get("options", None)[0]
options = options.split(',')
return library_names, options
class RequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
print(self.path)
if self.path == "/":
self.path = "index.html"
return SimpleHTTPRequestHandler.do_GET(self)
if self.path.startswith("/initialize"):
dataset_keys = {k: set(v.keys()) for k, v in datasets.items()}
dataset_with_most_splits = max([d for d in dataset_keys.values()], key=len)
external_dataset_keys = {k: set(v.keys()) for k, v in external_datasets.items()}
external_dataset_with_most_splits = max([d for d in external_dataset_keys.values()], key=len)
warnings = []
for k, v in dataset_keys.items():
if len(v) < len(dataset_with_most_splits):
warnings.append(
f"The {k} dataset does not contain all splits. Missing: {dataset_with_most_splits - v}."
f"\nSelecting that split to show the pip install numbers will not work."
)
for k, v in external_dataset_keys.items():
if len(v) < len(external_dataset_with_most_splits):
warnings.append(
f"The {k} dataset does not contain all splits. Missing: {external_dataset_with_most_splits - v}"
f".\nSelecting that split to show the pip install numbers will not work."
)
dataset_with_most_splits = list(dataset_with_most_splits)
dataset_with_most_splits.sort()
external_dataset_with_most_splits = list(external_dataset_with_most_splits)
external_dataset_with_most_splits.sort()
return self.response({
'internal': list(dataset_with_most_splits),
'external': external_dataset_with_most_splits,
'warnings': warnings
})
if self.path.startswith("/retrievePipInstalls"):
errors = []
library_names, options = parse_name_and_options(self.path)
if '1' in options:
returned_values = {}
for library_name in library_names:
ds = None
if library_name in datasets['pip']:
ds = datasets['pip'][library_name]
elif library_name in external_datasets['pip']:
ds = external_datasets['pip'][library_name]
else:
errors.append(f"No {library_name} found in internal or external datasets.")
for i in ds:
if i['day'] in returned_values:
returned_values[i['day']]['Cumulated'] += i['num_downloads']
else:
returned_values[i['day']] = {'Cumulated': i['num_downloads']}
library_names = ['Cumulated']
else:
returned_values = {}
for library_name in library_names:
if library_name in datasets['pip']:
ds = datasets['pip'][library_name]
elif library_name in external_datasets['pip']:
ds = external_datasets['pip'][library_name]
else:
errors.append(f"No {library_name} found in internal or external datasets for pip.")
return {'errors': errors}
for i in ds:
if i['day'] in returned_values:
returned_values[i['day']][library_name] = i['num_downloads']
else:
returned_values[i['day']] = {library_name: i['num_downloads']}
for library_name in library_names:
for i in returned_values.keys():
if library_name not in returned_values[i]:
returned_values[i][library_name] = None
returned_values = collections.OrderedDict(sorted(returned_values.items()))
output = {l: [k[l] for k in returned_values.values()] for l in library_names}
output['day'] = list(returned_values.keys())
return self.response(output)
if self.path.startswith("/retrieveStars"):
errors = []
library_names, options = parse_name_and_options(self.path)
returned_values = {}
dataset_dict = datasets['stars']
external_dataset_dict = external_datasets['stars']
week_over_week = '1' in options
for library_name in library_names:
if library_name in dataset_dict:
dataset = dataset_dict[library_name]
elif library_name in external_dataset_dict:
dataset = external_dataset_dict[library_name]
else:
errors.append(f"No {library_name} found in internal or external datasets for stars.")
return {'errors': errors}
last_value = 0
last_week = dataset[0]['week']
for i in dataset:
if week_over_week and last_week == i['week']:
continue
if i['dates'] in returned_values:
returned_values[i['dates']][library_name] = i['range'] - last_value
else:
returned_values[i['dates']] = {library_name: i['range'] - last_value}
last_value = i['range'] if week_over_week else 0
last_week = i['week']
returned_values = collections.OrderedDict(sorted(returned_values.items()))
returned_values = link_values(library_names, returned_values)
output = {l: [k[l] for k in returned_values.values()][::-1] for l in library_names}
output['day'] = list(returned_values.keys())[::-1]
# Trim down to a smaller number of points.
output = {k: [v for i, v in enumerate(value) if i % max(1, int(len(value) / 100)) == 0] for k, value in output.items()}
return self.response(output)
if self.path.startswith("/retrieveIssues"):
errors = []
library_names, options = parse_name_and_options(self.path)
exclude_org_members = '1' in options
week_over_week = '2' in options
returned_values = {}
dataset_dict = datasets['issues']
external_dataset_dict = external_datasets['issues']
range_id = 'range' if not exclude_org_members else 'range_non_org'
for library_name in library_names:
if library_name in dataset_dict:
dataset = dataset_dict[library_name]
elif library_name in external_dataset_dict:
dataset = external_dataset_dict[library_name]
else:
errors.append(f"No {library_name} found in internal or external datasets for stars.")
return {'errors': errors}
last_value = 0
last_week = dataset[0]['week']
for i in dataset:
if week_over_week and last_week == i['week']:
continue
if i['dates'] in returned_values:
returned_values[i['dates']][library_name] = i[range_id] - last_value
else:
returned_values[i['dates']] = {library_name: i[range_id] - last_value}
last_value = i[range_id] if week_over_week else 0
last_week = i['week']
returned_values = collections.OrderedDict(sorted(returned_values.items()))
returned_values = link_values(library_names, returned_values)
output = {l: [k[l] for k in returned_values.values()][::-1] for l in library_names}
output['day'] = list(returned_values.keys())[::-1]
# Trim down to a smaller number of points.
output = {k: [v for i, v in enumerate(value) if i % max(1, int(len(value) / 100)) == 0] for k, value in output.items()}
return self.response(output)
return SimpleHTTPRequestHandler.do_GET(self)
def response(self, output):
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(output).encode("utf-8"))
return SimpleHTTPRequestHandler
server = ThreadingHTTPServer(("", 7860), RequestHandler)
print("Running on port 7860")
server.serve_forever()
|