llm-arch / src /common.py
alfraser's picture
Added a page to see and control (pause/resume) the model inference endpoints
dfe6290
raw
history blame
2.73 kB
import os
import requests
import streamlit as st
from typing import List, Tuple
data_dir = os.path.join(os.path.dirname(__file__), '..', 'data')
img_dir = os.path.join(os.path.dirname(__file__), '..', 'img')
config_dir = os.path.join(os.path.dirname(__file__), '..', 'config')
def hf_api_token() -> str:
"""
Utility single access point to look up the hugging face access token.
"""
token = st.secrets['hf_token']
if token is None:
raise ValueError('No HF access token found in streamlit secrets')
return token
HF_RUNNING = 'running'
HF_SCALEDTOZERO = 'scaleToZero'
HF_PAUSED = 'paused'
def hf_endpoint_status(username: str, endpoint_name: str, api_token: str = None) -> Tuple[bool, bool]:
"""
Utility to check the status of a hugging face inference endpoint
: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
"""
url = f'https://api.endpoints.huggingface.cloud/v2/endpoint/{username}/{endpoint_name}'
if api_token is None:
api_token = hf_api_token()
response = requests.get(url, headers={"Authorization" : f"Bearer {api_token}"})
if response.status_code != 200:
raise ValueError(f"Likely config error. Received status code {response.status_code} - {response.text}")
else:
state = response.json()['status']['state']
return state
def pause_hf_endpoint(username: str, endpoint_name: str, api_token: str = None) -> None:
url = f'https://api.endpoints.huggingface.cloud/v2/endpoint/{username}/{endpoint_name}/pause'
if api_token is None:
api_token = hf_api_token()
requests.post(url, headers={"Authorization": f"Bearer {api_token}"})
def resume_hf_endpoint(username: str, endpoint_name: str, api_token: str = None) -> None:
url = f'https://api.endpoints.huggingface.cloud/v2/endpoint/{username}/{endpoint_name}/resume'
if api_token is None:
api_token = hf_api_token()
requests.post(url, headers={"Authorization": f"Bearer {api_token}"})
def join_items_comma_and(items: List[str]) -> str:
"""
Utility to convert a list of items to lowercase strings, comma separated and ending with and
"""
items = [str(i).strip() for i in items]
string_count = len(items)
if string_count == 0:
return ""
if string_count == 1:
return items[0]
return f"{', '.join(items[:-1])} and {items[-1]}"
def escape_dollars(text: str) -> str:
"""
Convenience function to escape dollar signs for prices in markdown
"""
if text is None:
return text
return text.replace("$", "\\$")