alfraser commited on
Commit
dfe6290
·
1 Parent(s): 947148c

Added a page to see and control (pause/resume) the model inference endpoints

Browse files
Files changed (2) hide show
  1. pages/900_System_Status.py +47 -0
  2. src/common.py +37 -2
pages/900_System_Status.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from src.st_helpers import st_setup
4
+ from src.common import *
5
+
6
+
7
+ if st_setup('LLM Arch'):
8
+ st.write("# System Status")
9
+
10
+ st.write("## HF Inference Endpoint Statuses")
11
+ st.write("The following endpoints need to be running to run all the demonstrations.")
12
+
13
+ endpoints = st.secrets['endpoints'].split(',')
14
+
15
+ for i, e in enumerate(endpoints):
16
+ status = hf_endpoint_status('alfraser', e)
17
+ message = f'{e} ({status})'
18
+
19
+ if i != 0:
20
+ st.divider()
21
+
22
+ status_col, button_col = st.columns([2, 1])
23
+
24
+ if status == HF_RUNNING:
25
+ with status_col:
26
+ st.success(message)
27
+ with button_col:
28
+ if st.button("Pause", key=f'action_{i}'):
29
+ pause_hf_endpoint('alfraser', e)
30
+ st.rerun()
31
+ elif status == HF_SCALEDTOZERO:
32
+ with status_col:
33
+ st.error(message)
34
+ elif status == HF_PAUSED:
35
+ with status_col:
36
+ st.warning(message)
37
+ with button_col:
38
+ if st.button("Resume", key=f'action_{i}'):
39
+ resume_hf_endpoint('alfraser', e)
40
+ st.rerun()
41
+ else:
42
+ with status_col:
43
+ st.info(message)
44
+ with button_col:
45
+ if st.button("Pause", key=f'action_{i}'):
46
+ pause_hf_endpoint('alfraser', e)
47
+ st.rerun()
src/common.py CHANGED
@@ -1,7 +1,8 @@
1
  import os
 
2
  import streamlit as st
3
 
4
- from typing import List
5
 
6
 
7
  data_dir = os.path.join(os.path.dirname(__file__), '..', 'data')
@@ -19,6 +20,41 @@ def hf_api_token() -> str:
19
  return token
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def join_items_comma_and(items: List[str]) -> str:
23
  """
24
  Utility to convert a list of items to lowercase strings, comma separated and ending with and
@@ -39,4 +75,3 @@ def escape_dollars(text: str) -> str:
39
  if text is None:
40
  return text
41
  return text.replace("$", "\\$")
42
-
 
1
  import os
2
+ import requests
3
  import streamlit as st
4
 
5
+ from typing import List, Tuple
6
 
7
 
8
  data_dir = os.path.join(os.path.dirname(__file__), '..', 'data')
 
20
  return token
21
 
22
 
23
+ HF_RUNNING = 'running'
24
+ HF_SCALEDTOZERO = 'scaleToZero'
25
+ HF_PAUSED = 'paused'
26
+
27
+
28
+ def hf_endpoint_status(username: str, endpoint_name: str, api_token: str = None) -> Tuple[bool, bool]:
29
+ """
30
+ Utility to check the status of a hugging face inference endpoint
31
+ :return: tuple of booleans first is if it is available, second is if it fully shutdown. False, True indicates it is not fully available or fully shutdown so is in process of starting up or shutting down
32
+ """
33
+ url = f'https://api.endpoints.huggingface.cloud/v2/endpoint/{username}/{endpoint_name}'
34
+ if api_token is None:
35
+ api_token = hf_api_token()
36
+ response = requests.get(url, headers={"Authorization" : f"Bearer {api_token}"})
37
+ if response.status_code != 200:
38
+ raise ValueError(f"Likely config error. Received status code {response.status_code} - {response.text}")
39
+ else:
40
+ state = response.json()['status']['state']
41
+ return state
42
+
43
+
44
+ def pause_hf_endpoint(username: str, endpoint_name: str, api_token: str = None) -> None:
45
+ url = f'https://api.endpoints.huggingface.cloud/v2/endpoint/{username}/{endpoint_name}/pause'
46
+ if api_token is None:
47
+ api_token = hf_api_token()
48
+ requests.post(url, headers={"Authorization": f"Bearer {api_token}"})
49
+
50
+
51
+ def resume_hf_endpoint(username: str, endpoint_name: str, api_token: str = None) -> None:
52
+ url = f'https://api.endpoints.huggingface.cloud/v2/endpoint/{username}/{endpoint_name}/resume'
53
+ if api_token is None:
54
+ api_token = hf_api_token()
55
+ requests.post(url, headers={"Authorization": f"Bearer {api_token}"})
56
+
57
+
58
  def join_items_comma_and(items: List[str]) -> str:
59
  """
60
  Utility to convert a list of items to lowercase strings, comma separated and ending with and
 
75
  if text is None:
76
  return text
77
  return text.replace("$", "\\$")