Skip to content

Commit 8cf6462

Browse files
authored
feat: add link to API playground for compatible models (#1488)
* feat: add link to API playground for compatible models * fix: move buttons to correct spot * fix: inference API placement button
1 parent c42d5f7 commit 8cf6462

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

src/lib/components/ModelCardMetadata.svelte

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
import CarbonEarth from "~icons/carbon/earth";
33
import CarbonArrowUpRight from "~icons/carbon/arrow-up-right";
44
import BIMeta from "~icons/bi/meta";
5+
import CarbonCode from "~icons/carbon/code";
56
import type { Model } from "$lib/types/Model";
67
7-
export let model: Pick<Model, "name" | "datasetName" | "websiteUrl" | "modelUrl" | "datasetUrl">;
8+
export let model: Pick<
9+
Model,
10+
"name" | "datasetName" | "websiteUrl" | "modelUrl" | "datasetUrl" | "hasInferenceAPI"
11+
>;
812
913
export let variant: "light" | "dark" = "light";
1014
</script>
@@ -35,6 +39,16 @@
3539
<div class="max-sm:hidden">&nbsp;page</div></a
3640
>
3741
{/if}
42+
{#if model.hasInferenceAPI}
43+
<a
44+
href={"https://huggingface.co/playground?modelId=" + model.name}
45+
target="_blank"
46+
rel="noreferrer"
47+
class="flex items-center hover:underline"
48+
><CarbonCode class="mr-1.5 shrink-0 text-xs text-gray-400" />
49+
API
50+
</a>
51+
{/if}
3852
{#if model.websiteUrl}
3953
<a
4054
href={model.websiteUrl}

src/lib/server/models.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import JSON5 from "json5";
1313
import { getTokenizer } from "$lib/utils/getTokenizer";
1414
import { logger } from "$lib/server/logger";
1515
import { ToolResultStatus, type ToolInput } from "$lib/types/Tool";
16+
import { isHuggingChat } from "$lib/utils/isHuggingChat";
1617

1718
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
1819

@@ -253,10 +254,6 @@ const processModel = async (m: z.infer<typeof modelConfig>) => ({
253254
parameters: { ...m.parameters, stop_sequences: m.parameters?.stop },
254255
});
255256

256-
export type ProcessedModel = Awaited<ReturnType<typeof processModel>> & {
257-
getEndpoint: () => Promise<Endpoint>;
258-
};
259-
260257
const addEndpoint = (m: Awaited<ReturnType<typeof processModel>>) => ({
261258
...m,
262259
getEndpoint: async (): Promise<Endpoint> => {
@@ -316,10 +313,40 @@ const addEndpoint = (m: Awaited<ReturnType<typeof processModel>>) => ({
316313
},
317314
});
318315

319-
export const models: ProcessedModel[] = await Promise.all(
320-
modelsRaw.map((e) => processModel(e).then(addEndpoint))
316+
const hasInferenceAPI = async (m: Awaited<ReturnType<typeof processModel>>) => {
317+
if (!isHuggingChat) {
318+
return false;
319+
}
320+
321+
const r = await fetch(`https://huggingface.co/api/models/${m.id}`);
322+
323+
if (!r.ok) {
324+
logger.warn(`Failed to check if ${m.id} has inference API: ${r.statusText}`);
325+
return false;
326+
}
327+
328+
const json = await r.json();
329+
330+
if (json.cardData.inference === false) {
331+
return false;
332+
}
333+
334+
return true;
335+
};
336+
337+
export const models = await Promise.all(
338+
modelsRaw.map((e) =>
339+
processModel(e)
340+
.then(addEndpoint)
341+
.then(async (m) => ({
342+
...m,
343+
hasInferenceAPI: await hasInferenceAPI(m),
344+
}))
345+
)
321346
);
322347

348+
export type ProcessedModel = (typeof models)[number];
349+
323350
// super ugly but not sure how to make typescript happier
324351
export const validModelIdSchema = z.enum(models.map((m) => m.id) as [string, ...string[]]);
325352

@@ -357,5 +384,5 @@ export const smallModel = env.TASK_MODEL
357384

358385
export type BackendModel = Optional<
359386
typeof defaultModel,
360-
"preprompt" | "parameters" | "multimodal" | "unlisted" | "tools"
387+
"preprompt" | "parameters" | "multimodal" | "unlisted" | "tools" | "hasInferenceAPI"
361388
>;

src/lib/types/Model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export type Model = Pick<
1818
| "multimodal"
1919
| "unlisted"
2020
| "tools"
21+
| "hasInferenceAPI"
2122
>;

src/routes/+layout.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export const load: LayoutServerLoad = async ({ locals, depends, request }) => {
194194
// disable tools on huggingchat android app
195195
!request.headers.get("user-agent")?.includes("co.huggingface.chat_ui_android"),
196196
unlisted: model.unlisted,
197+
hasInferenceAPI: model.hasInferenceAPI,
197198
})),
198199
oldModels,
199200
tools: [...toolFromConfigs, ...communityTools]

src/routes/api/models/+server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export async function GET() {
1919
multimodal: model.multimodal ?? false,
2020
unlisted: model.unlisted ?? false,
2121
tools: model.tools ?? false,
22+
hasInferenceAPI: model.hasInferenceAPI ?? false,
2223
}));
2324
return Response.json(res);
2425
}

src/routes/settings/(nav)/[...model]/+page.svelte

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import CarbonArrowUpRight from "~icons/carbon/arrow-up-right";
1010
import CarbonLink from "~icons/carbon/link";
1111
import CarbonChat from "~icons/carbon/chat";
12+
import CarbonCode from "~icons/carbon/code";
1213
1314
import { goto } from "$app/navigation";
1415
@@ -78,6 +79,19 @@
7879
Model website
7980
</a>
8081
{/if}
82+
83+
{#if model.hasInferenceAPI}
84+
<a
85+
href={"https://huggingface.co/playground?modelId=" + model.name}
86+
target="_blank"
87+
rel="noreferrer"
88+
class="flex items-center truncate underline underline-offset-2"
89+
>
90+
<CarbonCode class="mr-1.5 shrink-0 text-xs " />
91+
API Playground
92+
</a>
93+
{/if}
94+
8195
<CopyToClipBoardBtn
8296
value="{envPublic.PUBLIC_ORIGIN || $page.url.origin}{base}/models/{model.id}"
8397
classNames="!border-none !shadow-none !py-0 !px-1 !rounded-md"

0 commit comments

Comments
 (0)