|
| 1 | +/** |
| 2 | + * See the registered mapping of HF model ID => Nscale model ID here: |
| 3 | + * |
| 4 | + * https://huggingface.co/api/partners/nscale-cloud/models |
| 5 | + * |
| 6 | + * This is a publicly available mapping. |
| 7 | + * |
| 8 | + * If you want to try to run inference for a new model locally before it's registered on huggingface.co, |
| 9 | + * you can add it to the dictionary "HARDCODED_MODEL_ID_MAPPING" in consts.ts, for dev purposes. |
| 10 | + * |
| 11 | + * - If you work at Nscale and want to update this mapping, please use the model mapping API we provide on huggingface.co |
| 12 | + * - If you're a community member and want to add a new supported HF model to Nscale, please open an issue on the present repo |
| 13 | + * and we will tag Nscale team members. |
| 14 | + * |
| 15 | + * Thanks! |
| 16 | + */ |
| 17 | +import type { TextToImageInput } from "@huggingface/tasks"; |
| 18 | +import { InferenceOutputError } from "../lib/InferenceOutputError"; |
| 19 | +import type { BodyParams } from "../types"; |
| 20 | +import { omit } from "../utils/omit"; |
| 21 | +import { BaseConversationalTask, TaskProviderHelper, type TextToImageTaskHelper } from "./providerHelper"; |
| 22 | + |
| 23 | +const NSCALE_API_BASE_URL = "https://inference.api.nscale.com"; |
| 24 | + |
| 25 | +interface NscaleCloudBase64ImageGeneration { |
| 26 | + data: Array<{ |
| 27 | + b64_json: string; |
| 28 | + }>; |
| 29 | +} |
| 30 | + |
| 31 | +export class NscaleConversationalTask extends BaseConversationalTask { |
| 32 | + constructor() { |
| 33 | + super("nscale", NSCALE_API_BASE_URL); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +export class NscaleTextToImageTask extends TaskProviderHelper implements TextToImageTaskHelper { |
| 38 | + constructor() { |
| 39 | + super("nscale", NSCALE_API_BASE_URL); |
| 40 | + } |
| 41 | + |
| 42 | + preparePayload(params: BodyParams<TextToImageInput>): Record<string, unknown> { |
| 43 | + return { |
| 44 | + ...omit(params.args, ["inputs", "parameters"]), |
| 45 | + ...params.args.parameters, |
| 46 | + response_format: "b64_json", |
| 47 | + prompt: params.args.inputs, |
| 48 | + model: params.model, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + makeRoute(): string { |
| 53 | + return "v1/images/generations"; |
| 54 | + } |
| 55 | + |
| 56 | + async getResponse( |
| 57 | + response: NscaleCloudBase64ImageGeneration, |
| 58 | + url?: string, |
| 59 | + headers?: HeadersInit, |
| 60 | + outputType?: "url" | "blob" |
| 61 | + ): Promise<string | Blob> { |
| 62 | + if ( |
| 63 | + typeof response === "object" && |
| 64 | + "data" in response && |
| 65 | + Array.isArray(response.data) && |
| 66 | + response.data.length > 0 && |
| 67 | + "b64_json" in response.data[0] && |
| 68 | + typeof response.data[0].b64_json === "string" |
| 69 | + ) { |
| 70 | + const base64Data = response.data[0].b64_json; |
| 71 | + if (outputType === "url") { |
| 72 | + return `data:image/jpeg;base64,${base64Data}`; |
| 73 | + } |
| 74 | + return fetch(`data:image/jpeg;base64,${base64Data}`).then((res) => res.blob()); |
| 75 | + } |
| 76 | + |
| 77 | + throw new InferenceOutputError("Expected Nscale text-to-image response format"); |
| 78 | + } |
| 79 | +} |
0 commit comments