Spaces:
Runtime error
Runtime error
File size: 22,140 Bytes
d79f30b 7270b48 d79f30b 5106d98 910992a 5106d98 8e2e411 5106d98 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 5b45a90 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 65dd1ad 3406bb2 d79f30b dc2f37a d79f30b 0fbe88b d79f30b d55732b 0fbe88b d55732b 0fbe88b d55732b 4b3dc53 8236d91 4b3dc53 8236d91 28464d2 8236d91 4b3dc53 8236d91 6739d34 4b3dc53 9eb771a 8236d91 9eb771a 8236d91 9eb771a 0fbe88b 9eb771a 0fbe88b 9eb771a eedad6f 5b45a90 d79f30b 23bfd0f d79f30b b583175 d79f30b 3708fca d16c208 6739d34 8236d91 6739d34 3406bb2 d16c208 d79f30b 3406bb2 d79f30b 3406bb2 7476383 d79f30b 3406bb2 d79f30b 3406bb2 7270b48 61b37a4 3406bb2 3708fca 3406bb2 3708fca 3406bb2 b7308c2 3406bb2 b7308c2 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 5106d98 f5f3d31 3406bb2 d79f30b 5106d98 f5f3d31 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 3406bb2 d79f30b 9eb771a d16c208 8236d91 3406bb2 7270b48 61b37a4 3406bb2 3708fca 3406bb2 9eb771a 3406bb2 8236d91 3406bb2 9eb771a 3406bb2 9eb771a d55732b d16c208 6445654 3406bb2 6445654 3406bb2 d55732b 3406bb2 d55732b eedad6f c95d2f7 eedad6f 932b7fe eedad6f 3406bb2 d16c208 3406bb2 d79f30b 3406bb2 d79f30b a71f661 8236d91 a71f661 5106d98 a71f661 8236d91 a71f661 6445654 a71f661 eedad6f e1fb6ee 5b45a90 |
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
import gradio as gr
import json
import requests
import pandas as pd
def update_task_options(framework):
config = {
"Custom":["Custom"],
"Diffusers":["text-to-image"],
"Transformers":[
"text-classification",
"zero-shot-classification",
"token-classification",
"question-answering",
"fill-mask",
"summarization",
"translation",
"text2text-generation",
"text-generation",
"feature-extraction",
"image-classification",
"automatic-speech-recognition",
"audio-classification",
"object-detection",
"image-segmentation",
"table-question-answering"
]
}
return gr.Dropdown.update(
choices=config[framework],
value=config[framework][0] if len(config[framework]) > 0 else None
)
def update_regions(provider):
available_regions = []
headers = {
"Content-Type": "application/json",
}
endpoint_url = f"https://api.endpoints.huggingface.cloud/provider/{provider}/region"
response = requests.get(endpoint_url, headers=headers)
for region in response.json()['items']:
if region['status'] == 'available':
available_regions.append(f"{region['region']}/{region['label']}")
return gr.Dropdown.update(
choices=available_regions,
value=available_regions[0] if len(available_regions) > 0 else None
)
def update_compute_options(provider, region):
region = region.split("/")[0]
available_compute_choices = []
headers = {
"Content-Type": "application/json",
}
endpoint_url = f"https://api.endpoints.huggingface.cloud/provider/{provider}/region/{region}/compute"
response = requests.get(endpoint_url, headers=headers)
for compute in response.json()['items']:
if compute['status'] == 'available':
accelerator = compute['accelerator']
numAccelerators = compute['numAccelerators']
memoryGb = compute['memoryGb'].replace("Gi", "GB")
architecture = compute['architecture']
instanceType = compute['instanceType']
type = f"{numAccelerators}vCPU {memoryGb} · {architecture}" if accelerator == "cpu" else f"{numAccelerators}x {architecture}"
available_compute_choices.append(
f"{compute['accelerator'].upper()} [{compute['instanceSize']}] · {type} · {instanceType}"
)
return gr.Dropdown.update(
choices=available_compute_choices,
value=available_compute_choices[0] if len(available_compute_choices) > 0 else None
)
def submit(
hf_token_input,
endpoint_name_input,
provider_selector,
region_selector,
repository_selector,
revision_selector,
task_selector,
framework_selector,
compute_selector,
min_node_selector,
max_node_selector,
security_selector
):
compute_resources = compute_selector.split("·")
accelerator = compute_resources[0][:3].strip()
size_l_index = compute_resources[0].index("[") - 1
size_r_index = compute_resources[0].index("]")
size = compute_resources[0][size_l_index : size_r_index].strip()
type = compute_resources[-1].strip()
payload = {
"accountId": repository_selector.split("/")[0],
"compute": {
"accelerator": accelerator.lower(),
"instanceSize": size[1:],
"instanceType": type,
"scaling": {
"maxReplica": int(max_node_selector),
"minReplica": int(min_node_selector)
}
},
"model": {
"framework": "custom",
"image": {
"huggingface": {}
},
"repository": repository_selector.lower(),
"revision": revision_selector,
"task": task_selector.lower()
},
"name": endpoint_name_input.strip(),
"provider": {
"region": region_selector.split("/")[0].lower(),
"vendor": provider_selector.lower()
},
"type": security_selector.lower()
}
payload = json.dumps(payload)
headers = {
"Authorization": f"Bearer {hf_token_input.strip()}",
"Content-Type": "application/json",
}
endpoint_url = f"https://api.endpoints.huggingface.cloud/endpoint"
print(f"Endpoint: {endpoint_url}")
response = requests.post(endpoint_url, headers=headers, data=payload)
if response.status_code == 400:
return f"{response.text}. Malformed data in {payload}"
elif response.status_code == 401:
return "Invalid token"
elif response.status_code == 409:
return f"Error: {response.text}"
elif response.status_code == 202:
return f"Endpoint {endpoint_name_input} created successfully on {provider_selector.lower()} using {repository_selector.lower()}@{revision_selector}. \n Please check out the progress at https://ui.endpoints.huggingface.co/endpoints."
else:
return f"Something went wrong! - StatusCode:{response.status_code} - Error: {response.text}"
def delete_endpoint(
hf_token_input,
endpoint_name_input
):
response = requests.delete(
f"https://api.endpoints.huggingface.cloud/endpoint/{endpoint_name_input}",
headers = {
"Authorization": f"Bearer {hf_token_input.strip()}",
"Content-Type": "application/json",
}
)
if response.status_code == 401:
return "Invalid token"
elif response.status_code == 404:
return f"Error: {response.text}"
elif response.status_code == 202 or response.status_code == 200:
return f"Endpoint {endpoint_name_input} deleted successfully."
else:
return f"Something went wrong! - StatusCode:{response.status_code} - Error: {response.text}"
def get_all_endpoints(
hf_token_input,
method,
):
response = requests.get(
f"https://api.endpoints.huggingface.cloud/endpoint",
headers = {
"Authorization": f"Bearer {hf_token_input.strip()}",
"Content-Type": "application/json",
})
if response.status_code == 401:
if method == "info":
return gr.DataFrame.update(
value=pd.DataFrame({"Error":["Invalid Token -OR- No Endpoint Found !"]}),
)
else:
return gr.Dropdown.update(
value="Invalid token or No endpoints found!",
)
elif response.status_code == 200:
endpoints_json = response.json()
if method == "info":
endpoints_df = pd.DataFrame(columns=["name", "model", "provider", "compute", "status", "minReplica", "maxReplica", "createdAt", "updatedAt"])
for endpoint in endpoints_json["items"]:
endpoints_df = endpoints_df.append({
"name": endpoint["name"],
"model": endpoint["model"]["repository"] + "@" + endpoint["model"]["revision"],
"provider": endpoint["provider"]["vendor"] + "/" + endpoint["provider"]["region"],
"compute": endpoint["compute"]["instanceType"] + "·" + endpoint["compute"]["instanceSize"] + " [" + endpoint["compute"]["accelerator"] + "]",
"status": endpoint["status"]["state"],
"minReplica": endpoint["compute"]["scaling"]["minReplica"],
"maxReplica": endpoint["compute"]["scaling"]["maxReplica"],
"createdAt": endpoint["status"]["createdAt"],
"updatedAt": endpoint["status"]["updatedAt"],
}, ignore_index=True)
endpoints_df.columns = ["Endpoint Name", "Model Name @ Revision", "Provider", "Instance Type", "Status", "Min Replica", "Max Replica", "Created At", "Updated At"]
return gr.DataFrame.update(
value=endpoints_df,
)
else:
return gr.Dropdown.update(
choices=[endpoint["name"] for endpoint in endpoints_json["items"]],
value=endpoints_json["items"][0]["name"],
)
def update_endpoint(
hf_token_input,
endpoint_name_input,
min_node_selector,
max_node_selector,
instance_type,
):
payload ={
"compute": {
"instanceSize": instance_type.split("·")[0].split("[")[1].split("]")[0],
"instanceType": instance_type.split("·")[-1].strip(),
"scaling": {
"maxReplica": int(max_node_selector),
"minReplica": int(min_node_selector)
}
}}
payload = json.dumps(payload)
response = requests.put(
f"https://api.endpoints.huggingface.cloud/endpoint/{endpoint_name_input}",
headers = {
"Authorization": f"Bearer {hf_token_input.strip()}",
"Content-Type": "application/json",
},
data=payload,
)
if response.status_code == 401:
return "Invalid token"
elif response.status_code == 404:
return f"Error: {response.text}"
elif response.status_code == 202 or response.status_code == 200:
return f"Endpoint {endpoint_name_input} updated successfully."
else:
return f"Something went wrong! - StatusCode:{response.status_code} - Error: {response.text}"
def get_endpoint_logs(
hf_token_input,
endpoint_name_input,
):
response = requests.get(
f"https://api.endpoints.huggingface.cloud/endpoint/{endpoint_name_input}/logs",
headers = {
"Authorization": f"Bearer {hf_token_input.strip()}",
"Content-Type": "application/json",
})
if response.status_code == 401:
return "Invalid token or No logs found!"
elif response.status_code == 200:
return response.text
elif response.status_code == 404:
return f"Error: {response.text}"
with gr.Blocks() as interface:
gr.Markdown("""
#### Your 🤗 Access Token <span style="color:red;">(Required)</span>
""")
hf_token_input = gr.Textbox(
show_label=False,
placeholder="Your personal/organization Huggingface Access Token. Get it from (https://huggingface.co/settings/tokens)",
type="password"
)
# Get All Endpoints Info
with gr.Tab("Info"):
gr.Markdown("""
### All Deployed Endpoints
""")
endpoints_table = gr.Dataframe(
headers=["Endpoint Name", "Model Name", "Provider", "Instance Type", "Status", "Min Replica", "Max Replica", "Created At", "Updated At"],
col_count=(9, "fixed"),
)
endpoint_info_button = gr.Button(value="Get Info")
# Deploy Endpoint
with gr.Tab("Deploy Endpoint"):
gr.Markdown(
"""
### <br><center style="color:green">(Deploy Your Model on 🤗 Endpoint)</center>
""")
gr.Markdown("""
#### Endpoint Name
""")
endpoint_name_input = gr.Textbox(
show_label=False
)
with gr.Row():
gr.Markdown("""
#### Cloud Provider
""")
gr.Markdown("""
#### Cloud Region
""")
with gr.Row():
provider_selector = gr.Dropdown(
choices=["aws", "azure"],
value="",
interactive=True,
show_label=False,
)
region_selector = gr.Dropdown(
[],
value="",
interactive=True,
show_label=False,
)
with gr.Row():
gr.Markdown("""
#### Target Model e.g (openai/whisper-tiny)
""")
gr.Markdown("""
#### Branch commit hash e.g (ada5a5d516772e41f9aeb0f984df6ecc4620001f)
""")
with gr.Row():
repository_selector = gr.Textbox(
value="",
interactive=True,
show_label=False,
)
revision_selector = gr.Textbox(
value="",
interactive=True,
show_label=False,
)
with gr.Row():
gr.Markdown("""
#### Task
""")
gr.Markdown("""
#### Framework
""")
with gr.Row():
framework_selector = gr.Dropdown(
choices = ["Custom", "Diffusers", "Transformers"],
value="",
interactive=True,
show_label=False,
)
task_selector = gr.Dropdown(
[],
value="",
interactive=True,
show_label=False,
)
gr.Markdown("""
#### Select Compute Instance Type
""")
compute_selector = gr.Dropdown(
[],
value="",
interactive=True,
show_label=False,
)
with gr.Row():
gr.Markdown("""
#### Min Number of Nodes
""")
gr.Markdown("""
#### Max Number of Nodes
""")
gr.Markdown("""
#### Security Level
""")
with gr.Row():
min_node_selector = gr.Number(
value=1,
interactive=True,
show_label=False,
)
max_node_selector = gr.Number(
value=1,
interactive=True,
show_label=False,
)
security_selector = gr.Radio(
choices=["Protected", "Public"],
value="Protected",
interactive=True,
show_label=False,
)
submit_button = gr.Button(
value="Submit",
)
status_txt = gr.Textbox(
value="status",
interactive=False
)
# Update Endpoint
with gr.Tab("Update Endpoint"):
gr.Markdown("""
### <br><center style="color:green">(Update 🔁 Endpoint)</center>
""")
update_endpoint_info_button = gr.Button(value="Load Endpoints 🔃")
with gr.Row():
gr.Markdown("""
#### Cloud Provider
""")
gr.Markdown("""
#### Cloud Region
""")
with gr.Row():
update_provider_selector = gr.Dropdown(
choices=["aws", "azure"],
value="",
interactive=True,
show_label=False,
)
update_region_selector = gr.Dropdown(
[],
value="",
interactive=True,
show_label=False,
)
with gr.Row():
gr.Markdown("""
#### Endpoint Name
""")
gr.Markdown("""
#### Instance Type
""")
with gr.Row():
update_endpoint_name_input = gr.Dropdown(
[],
value="",
show_label=False
)
update_compute_selector = gr.Dropdown(
[],
value="",
interactive=True,
show_label=False,
)
with gr.Row():
gr.Markdown("""
#### Min Number of Nodes
""")
gr.Markdown("""
#### Max Number of Nodes
""")
with gr.Row():
update_min_node_input = gr.Number(
value=1,
interactive=True,
show_label=False,
)
update_max_node_input = gr.Number(
value=1,
interactive=True,
show_label=False,
)
update_button = gr.Button(
value="Update",
)
update_status_txt = gr.Textbox(
value="status",
interactive=False
)
# Delete Endpoint
with gr.Tab("Delete Endpoint"):
gr.Markdown("""
### <br><center style="color:green">(Delete 🗑️ Endpoint)</center>
""")
delete_endpoint_info_button = gr.Button(value="Load Endpoints 🔃")
gr.Markdown("""
#### Endpoint Name
""")
delete_endpoint_name_input = gr.Dropdown(
[],
value="",
show_label=False
)
delete_button = gr.Button(
value="Delete",
)
delete_status_txt = gr.Textbox(
value="status",
interactive=False
)
# Endpoint logs
with gr.Tab("Endpoint Logs"):
gr.Markdown("""
### <br><center style="color:green">(Endpoint 📖 Logs)</center>
""")
endpoint_logs_load_button = gr.Button(value="Load Endpoints 🔃")
gr.Markdown("""
#### Endpoint Name
""")
endpoint_logs_selector = gr.Dropdown(
[],
value="",
show_label=False
)
endpoint_logs = gr.Textbox(
value="",
interactive=False
)
endpoint_logs_button = gr.Button(value="Get Logs")
# Pricing Table
with gr.Tab("Pricing Table"):
gr.Markdown("""
### <br><center style="color:green">(Instance Pricing Table)</center>
#### Pricing Table(CPU) - 2023/2/22
""")
gr.Dataframe(
headers=["provider", "size", "$/h", "vCPUs", "Memory", "Architecture"],
datatype=["str", "str", "str", "number", "str", "str"],
row_count=8,
col_count=(6, "fixed"),
value=[
["aws", "small", "$0.06", 1, "2GB", "Intel Xeon - Ice Lake"],
["aws", "medium", "$0.12", 2, "4GB", "Intel Xeon - Ice Lake"],
["aws", "large", "$0.24", 4, "8GB", "Intel Xeon - Ice Lake"],
["aws", "xlarge", "$0.48", 8, "16GB", "Intel Xeon - Ice Lake"],
["azure", "small", "$0.06", 1, "2GB", "Intel Xeon"],
["azure", "medium", "$0.12", 2, "4GB", "Intel Xeon"],
["azure", "large", "$0.24", 4, "8GB", "Intel Xeon"],
["azure", "xlarge", "$0.48", 8, "16GB", "Intel Xeon"],
]
)
gr.Markdown("""
#### Pricing Table(GPU) - 2023/2/22
""")
gr.Dataframe(
headers=["provider", "size", "$/h", "GPUs", "Memory", "Architecture"],
datatype=["str", "str", "str", "number", "str", "str"],
row_count=6,
col_count=(6, "fixed"),
value=[
["aws", "small", "$0.60", 1, "14GB", "NVIDIA T4"],
["aws", "medium", "$1.30", 1, "24GB", "NVIDIA A10G"],
["aws", "large", "$4.50", 4, "56GB", "NVIDIA T4"],
["aws", "xlarge", "$6.50", 1, "80GB", "NVIDIA A100"],
["aws", "xxlarge", "$7.00", 4, "96GB", "NVIDIA A10G"],
["aws", "xxxlarge", "$45.0", 8, "640GB", "NVIDIA A100"],
]
)
# Info Tab Events
endpoint_info_button.click(
get_all_endpoints,
inputs=[hf_token_input, gr.TextArea(value="info", interactive=False, visible=False)],
outputs=endpoints_table
)
# Deploy Tab Events
framework_selector.change(update_task_options, inputs=framework_selector, outputs=task_selector)
provider_selector.change(update_regions, inputs=provider_selector, outputs=region_selector)
region_selector.change(update_compute_options, inputs=[provider_selector, region_selector], outputs=compute_selector)
submit_button.click(
submit,
inputs=[
hf_token_input,
endpoint_name_input,
provider_selector,
region_selector,
repository_selector,
revision_selector,
task_selector,
framework_selector,
compute_selector,
min_node_selector,
max_node_selector,
security_selector],
outputs=status_txt)
# Update Tab Events
update_endpoint_info_button.click(
get_all_endpoints,
inputs=[hf_token_input, gr.TextArea(value="update", interactive=False, visible=False)],
outputs=update_endpoint_name_input
)
update_provider_selector.change(update_regions, inputs=update_provider_selector, outputs=update_region_selector)
update_region_selector.change(update_compute_options, inputs=[update_provider_selector, update_region_selector], outputs=update_compute_selector)
update_button.click(
update_endpoint,
inputs=[
hf_token_input,
update_endpoint_name_input,
update_min_node_input,
update_max_node_input,
update_compute_selector
],
outputs=update_status_txt
)
# Delete Tab Events
delete_endpoint_info_button.click(
get_all_endpoints,
inputs=[hf_token_input, gr.TextArea(value="delete", interactive=False, visible=False)],
outputs=delete_endpoint_name_input
)
delete_button.click(
delete_endpoint,
inputs=[
hf_token_input,
delete_endpoint_name_input
],
outputs=delete_status_txt
)
# Endpoint Logs Tab Events
endpoint_logs_load_button.click(
get_all_endpoints,
inputs=[hf_token_input, gr.TextArea(value="logs", interactive=False, visible=False)],
outputs=endpoint_logs_selector
)
endpoint_logs_button.click(
get_endpoint_logs,
inputs=[
hf_token_input,
endpoint_logs_selector
],
outputs=endpoint_logs
)
#interface.queue(concurrency_count=5)
interface.launch() |