Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): specify either space namespace or huggingface.co URL #1435

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/lib/server/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ const toolInputSchema = toolInputBaseSchema.and(
export const editableToolSchema = z
.object({
name: z.string().min(1).max(40),
baseUrl: z.string().min(1).max(100),
// only allow huggingface spaces either through namespace or direct URLs
baseUrl: z.union([
z.string().regex(/^[a-zA-Z0-9-]+\/[a-zA-Z0-9-]+$/),
z
.string()
.regex(/^https:\/\/huggingface\.co\/spaces\/[a-zA-Z0-9-]+\/[a-zA-Z0-9-]+$/)
.transform((url) => url.split("/").slice(-2).join("/")),
]),
endpoint: z.string().min(1).max(100),
inputs: z.array(toolInputSchema),
outputComponent: z.string().min(1).max(100),
Expand All @@ -85,7 +92,6 @@ export const editableToolSchema = z
outputComponentIdx: parseInt(tool.outputComponent.split(";")[0]),
outputComponent: ToolOutputComponents.parse(tool.outputComponent.split(";")[1]),
}));

export const configTools = z
.array(
z
Expand Down
9 changes: 7 additions & 2 deletions src/lib/utils/getGradioApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import type { Client } from "@gradio/client";
export type ApiReturnType = Awaited<ReturnType<typeof Client.prototype.view_api>>;

export async function getGradioApi(space: string) {
const api: ApiReturnType = await fetch(`${base}/api/spaces-config?space=${space}`).then((res) =>
res.json()
const api: ApiReturnType = await fetch(`${base}/api/spaces-config?space=${space}`).then(
async (res) => {
if (!res.ok) {
throw new Error(await res.text());
}
return res.json();
}
);
return api;
}
18 changes: 16 additions & 2 deletions src/routes/api/spaces-config/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,31 @@ export async function GET({ url, locals }) {
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(/^[a-zA-Z0-9-]+\/[a-zA-Z0-9-]+$/)) {
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(space)).view_api();
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(JSON.stringify({ error: true, message: "Failed to get space API" }), {
return new Response("Error fetching space API. Is the name correct?", {
status: 400,
headers: {
"Content-Type": "application/json",
Expand Down
Loading