File size: 4,774 Bytes
8766bc5 | 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 | import type { Config } from "./types.js";
export function determine_protocol(endpoint: string): {
ws_protocol: "ws" | "wss";
http_protocol: "http:" | "https:";
host: string;
} {
if (endpoint.startsWith("http")) {
const { protocol, host } = new URL(endpoint);
if (host.endsWith("hf.space")) {
return {
ws_protocol: "wss",
host: host,
http_protocol: protocol as "http:" | "https:"
};
}
return {
ws_protocol: protocol === "https:" ? "wss" : "ws",
http_protocol: protocol as "http:" | "https:",
host
};
}
// default to secure if no protocol is provided
return {
ws_protocol: "wss",
http_protocol: "https:",
host: endpoint
};
}
export const RE_SPACE_NAME = /^[^\/]*\/[^\/]*$/;
export const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/;
export async function process_endpoint(
app_reference: string,
token?: `hf_${string}`
): Promise<{
space_id: string | false;
host: string;
ws_protocol: "ws" | "wss";
http_protocol: "http:" | "https:";
}> {
const headers: { Authorization?: string } = {};
if (token) {
headers.Authorization = `Bearer ${token}`;
}
const _app_reference = app_reference.trim();
if (RE_SPACE_NAME.test(_app_reference)) {
try {
const res = await fetch(
`https://huggingface.co/api/spaces/${_app_reference}/host`,
{ headers }
);
if (res.status !== 200)
throw new Error("Space metadata could not be loaded.");
const _host = (await res.json()).host;
return {
space_id: app_reference,
...determine_protocol(_host)
};
} catch (e) {
throw new Error("Space metadata could not be loaded." + e.message);
}
}
if (RE_SPACE_DOMAIN.test(_app_reference)) {
const { ws_protocol, http_protocol, host } =
determine_protocol(_app_reference);
return {
space_id: host.replace(".hf.space", ""),
ws_protocol,
http_protocol,
host
};
}
return {
space_id: false,
...determine_protocol(_app_reference)
};
}
export function map_names_to_ids(
fns: Config["dependencies"]
): Record<string, number> {
let apis: Record<string, number> = {};
fns.forEach(({ api_name }, i) => {
if (api_name) apis[api_name] = i;
});
return apis;
}
const RE_DISABLED_DISCUSSION =
/^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
export async function discussions_enabled(space_id: string): Promise<boolean> {
try {
const r = await fetch(
`https://huggingface.co/api/spaces/${space_id}/discussions`,
{
method: "HEAD"
}
);
const error = r.headers.get("x-error-message");
if (error && RE_DISABLED_DISCUSSION.test(error)) return false;
return true;
} catch (e) {
return false;
}
}
export async function get_space_hardware(
space_id: string,
token: `hf_${string}`
): Promise<(typeof hardware_types)[number]> {
const headers: { Authorization?: string } = {};
if (token) {
headers.Authorization = `Bearer ${token}`;
}
try {
const res = await fetch(
`https://huggingface.co/api/spaces/${space_id}/runtime`,
{ headers }
);
if (res.status !== 200)
throw new Error("Space hardware could not be obtained.");
const { hardware } = await res.json();
return hardware;
} catch (e) {
throw new Error(e.message);
}
}
export async function set_space_hardware(
space_id: string,
new_hardware: (typeof hardware_types)[number],
token: `hf_${string}`
): Promise<(typeof hardware_types)[number]> {
const headers: { Authorization?: string } = {};
if (token) {
headers.Authorization = `Bearer ${token}`;
}
try {
const res = await fetch(
`https://huggingface.co/api/spaces/${space_id}/hardware`,
{ headers, body: JSON.stringify(new_hardware) }
);
if (res.status !== 200)
throw new Error(
"Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
);
const { hardware } = await res.json();
return hardware;
} catch (e) {
throw new Error(e.message);
}
}
export async function set_space_timeout(
space_id: string,
timeout: number,
token: `hf_${string}`
): Promise<number> {
const headers: { Authorization?: string } = {};
if (token) {
headers.Authorization = `Bearer ${token}`;
}
try {
const res = await fetch(
`https://huggingface.co/api/spaces/${space_id}/hardware`,
{ headers, body: JSON.stringify({ seconds: timeout }) }
);
if (res.status !== 200)
throw new Error(
"Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
);
const { hardware } = await res.json();
return hardware;
} catch (e) {
throw new Error(e.message);
}
}
export const hardware_types = [
"cpu-basic",
"cpu-upgrade",
"t4-small",
"t4-medium",
"a10g-small",
"a10g-large",
"a100-large"
] as const;
|