File size: 1,178 Bytes
6a5e4c9
0c3e3b2
 
6a5e4c9
 
 
0c3e3b2
 
 
 
 
 
 
ba9c926
 
 
 
1f110a3
ba9c926
 
 
 
 
 
 
 
 
0c3e3b2
 
ba9c926
0c3e3b2
 
 
 
 
 
 
ba9c926
0c3e3b2
 
 
 
 
 
 
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
import { env } from "$env/dynamic/private";
import { Client } from "@gradio/client";

export async function GET({ url }) {
	if (env.COMMUNITY_TOOLS !== "true") {
		return new Response("Community tools are not enabled", { status: 403 });
	}

	const space = url.searchParams.get("space");

	if (!space) {
		return new Response("Missing space", { status: 400 });
	}
	// Extract namespace from space URL or use as-is if it's already in namespace format
	let namespace = null;
	if (space.startsWith("https://huggingface.co/spaces/")) {
		namespace = space.split("/").slice(-2).join("/");
	} else if (space.match(/^[^/]+\/[^/]+$/)) {
		namespace = space;
	}

	if (!namespace) {
		return new Response(
			"Invalid space name. Specify a namespace or a full URL on huggingface.co.",
			{ status: 400 }
		);
	}

	try {
		const api = await (await Client.connect(namespace)).view_api();
		return new Response(JSON.stringify(api), {
			status: 200,
			headers: {
				"Content-Type": "application/json",
			},
		});
	} catch (e) {
		return new Response("Error fetching space API. Is the name correct?", {
			status: 400,
			headers: {
				"Content-Type": "application/json",
			},
		});
	}
}