File size: 1,843 Bytes
0ad74ed |
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 |
import type { ApiInfo, ApiData } from "../types";
import semiver from "semiver";
import { API_INFO_URL, BROKEN_CONNECTION_MSG } from "../constants";
import { Client } from "../client";
import { SPACE_FETCHER_URL } from "../constants";
import { join_urls, transform_api_info } from "../helpers/api_info";
export async function view_api(this: Client): Promise<any> {
if (this.api_info) return this.api_info;
const { hf_token } = this.options;
const { config } = this;
const headers: {
Authorization?: string;
"Content-Type": "application/json";
} = { "Content-Type": "application/json" };
if (hf_token) {
headers.Authorization = `Bearer ${hf_token}`;
}
if (!config) {
return;
}
try {
let response: Response;
let api_info: ApiInfo<ApiData> | { api: ApiInfo<ApiData> };
if (typeof window !== "undefined" && window.gradio_api_info) {
api_info = window.gradio_api_info;
} else {
if (semiver(config?.version || "2.0.0", "3.30") < 0) {
response = await this.fetch(SPACE_FETCHER_URL, {
method: "POST",
body: JSON.stringify({
serialize: false,
config: JSON.stringify(config)
}),
headers,
credentials: "include"
});
} else {
const url = join_urls(config.root, this.api_prefix, API_INFO_URL);
response = await this.fetch(url, {
headers,
credentials: "include"
});
}
if (!response.ok) {
throw new Error(BROKEN_CONNECTION_MSG);
}
api_info = await response.json();
}
if ("api" in api_info) {
api_info = api_info.api;
}
if (
api_info.named_endpoints["/predict"] &&
!api_info.unnamed_endpoints["0"]
) {
api_info.unnamed_endpoints[0] = api_info.named_endpoints["/predict"];
}
return transform_api_info(api_info, config, this.api_map);
} catch (e) {
"Could not get API info. " + (e as Error).message;
}
}
|