File size: 2,364 Bytes
1cea837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

/** Actually `hf_${string}`, but for convenience, using the string type */
type AccessToken = string;

interface Credentials {
	accessToken: AccessToken;
}

type SpaceHardwareFlavor =
	| "cpu-basic"
	| "cpu-upgrade"
	| "t4-small"
	| "t4-medium"
	| "a10g-small"
	| "a10g-large"
	| "a100-large";

type SpaceSdk = "streamlit" | "gradio" | "docker" | "static";

type SpaceStage =
	| "NO_APP_FILE"
	| "CONFIG_ERROR"
	| "BUILDING"
	| "BUILD_ERROR"
	| "RUNNING"
	| "RUNNING_BUILDING"
	| "RUNTIME_ERROR"
	| "DELETING"
	| "PAUSED"
	| "SLEEPING";

type AccessTokenRole = "admin" | "write" | "contributor" | "read";

type AuthType = "access_token" | "app_token" | "app_token_as_user";


interface SpaceRuntime {
	stage: SpaceStage;
	sdk?: SpaceSdk;
	sdkVersion?: string;
	errorMessage?: string;
	hardware?: {
		current: SpaceHardwareFlavor | null;
		currentPrettyName?: string;
		requested: SpaceHardwareFlavor | null;
		requestedPrettyName?: string;
	};
	/** when calling /spaces, those props are only fetched if ?full=true */
	resources?: SpaceResourceConfig;
	/** in seconds */
	gcTimeout?: number | null;
}

interface SpaceResourceRequirement {
	cpu?: string;
	memory?: string;
	gpu?: string;
	gpuModel?: string;
	ephemeral?: string;
}

interface SpaceResourceConfig {
	requests: SpaceResourceRequirement;
	limits: SpaceResourceRequirement;
	replicas?: number;
	throttled?: boolean;
	is_custom?: boolean;
}

export interface HFSpaceStatus {
  _id: string
  id: string
  author: string
  sha: string
  lastModified: string
  private: boolean
  gated: boolean
  disabled: boolean
  host: string
  subdomain: string
  tags: string[]
  likes: number
  sdk: string
  runtime: SpaceRuntime
  createdAt: string
}

export async function getHuggingFaceSpaceStatus({
  space,
  // userName,
  // spaceName,
}: {
  space: string // a joined "user_name/space_name"
  // userName: string
  // spaceName: string
}): Promise<HFSpaceStatus> {
  const res = await fetch(`https://huggingface.co/api/spaces/${space}`, {
    method: "GET",
    headers: {
      Authorization: `Bearer ${process.env.ADMIN_HUGGING_FACE_API_TOKEN || ""}`
    }
  })

  if (res.status !== 200)  {
    throw new Error("failed to get the space data")
  }

  try {
    const data = await res.json() as HFSpaceStatus
    return data
  } catch (err) {
    throw new Error(`failed to parse space data: ${err}`)
  }
}