-
Notifications
You must be signed in to change notification settings - Fork 512
Integrate Nscale-cloud into HuggingFace inference #1260
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
86d56bc
feat: Adding nscale as provider
nbarr07 490d231
chore: added tests
nbarr07 3e32bc4
update the tapes.ts, update textToImage.ts nscale response format
nbarr07 d6bf892
remove nscale test from package.json
nbarr07 b93d276
remove skip comment on tests
nbarr07 4c36014
Merge remote-tracking branch 'upstream/main'
nbarr07 83f8e2d
save custom changes for nscale
nbarr07 f9e360f
Update nscale-cloud provider
nbarr07 8cf1a86
update Nscale provider
nbarr07 42edde2
Delete package-lock.json
nbarr07 efca482
Update consts.ts
nbarr07 6a6d0bd
Update nscale-cloud.ts
nbarr07 5e6cb04
Update url in nscale-cloud.ts
nbarr07 815250c
Merge remote-tracking branch 'upstream/main'
nbarr07 bcc01c0
rename to nscale
nbarr07 bce9ec0
format code
SBrandeis 95cc1f8
Merge remote-tracking branch 'origin/main' into pr/nbarr07/1260
SBrandeis b606d69
somehow some tapes were removed
SBrandeis 7e499fc
Update namespace in the readme
nbarr07 30f161c
Update packages/inference/src/providers/nscale.ts
hanouticelina 0e5a6c8
remove casting
hanouticelina 827093f
Merge remote-tracking branch 'upstream/main'
hanouticelina ed1af1f
Merge branch 'main' into main
hanouticelina 8826fc5
Merge remote-tracking branch 'upstream/main'
hanouticelina 5f2e36c
Merge branch 'main' of github.com:nbarr07/huggingface.js
hanouticelina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* See the registered mapping of HF model ID => Nscale model ID here: | ||
* | ||
* https://huggingface.co/api/partners/nscale-cloud/models | ||
* | ||
* This is a publicly available mapping. | ||
* | ||
* If you want to try to run inference for a new model locally before it's registered on huggingface.co, | ||
* you can add it to the dictionary "HARDCODED_MODEL_ID_MAPPING" in consts.ts, for dev purposes. | ||
* | ||
* - If you work at Nscale and want to update this mapping, please use the model mapping API we provide on huggingface.co | ||
* - 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 | ||
* and we will tag Nscale team members. | ||
* | ||
* Thanks! | ||
*/ | ||
import { InferenceOutputError } from "../lib/InferenceOutputError"; | ||
import type { BodyParams, UrlParams } from "../types"; | ||
import { omit } from "../utils/omit"; | ||
import { | ||
BaseConversationalTask, | ||
TaskProviderHelper, | ||
type TextToImageTaskHelper, | ||
} from "./providerHelper"; | ||
|
||
const NSCALE_API_BASE_URL = "https://inference.api.nscale.com"; | ||
|
||
interface NscaleCloudBase64ImageGeneration { | ||
data: Array<{ | ||
b64_json: string; | ||
}>; | ||
} | ||
|
||
export class NscaleConversationalTask extends BaseConversationalTask { | ||
constructor() { | ||
super("nscale", NSCALE_API_BASE_URL); | ||
} | ||
} | ||
|
||
export class NscaleTextToImageTask extends TaskProviderHelper implements TextToImageTaskHelper { | ||
constructor() { | ||
super("nscale", NSCALE_API_BASE_URL); | ||
} | ||
|
||
preparePayload(params: BodyParams): Record<string, unknown> { | ||
return { | ||
...omit(params.args, ["inputs", "parameters"]), | ||
...(params.args.parameters as Record<string, unknown>), | ||
response_format: "b64_json", | ||
prompt: params.args.inputs, | ||
model: params.model, | ||
}; | ||
} | ||
|
||
makeRoute(params: UrlParams): string { | ||
void params; | ||
return "v1/images/generations"; | ||
} | ||
|
||
hanouticelina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
async getResponse( | ||
response: NscaleCloudBase64ImageGeneration, | ||
url?: string, | ||
headers?: HeadersInit, | ||
outputType?: "url" | "blob" | ||
): Promise<string | Blob> { | ||
if ( | ||
typeof response === "object" && | ||
"data" in response && | ||
Array.isArray(response.data) && | ||
response.data.length > 0 && | ||
"b64_json" in response.data[0] && | ||
typeof response.data[0].b64_json === "string" | ||
) { | ||
const base64Data = response.data[0].b64_json; | ||
if (outputType === "url") { | ||
return `data:image/jpeg;base64,${base64Data}`; | ||
} | ||
return fetch(`data:image/jpeg;base64,${base64Data}`).then((res) => res.blob()); | ||
} | ||
|
||
throw new InferenceOutputError("Expected Nscale text-to-image response format"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.