-
Notifications
You must be signed in to change notification settings - Fork 397
[Inference] Implement a "1 class = 1 provider<>task pair" logic to isolate provider-specific code #1315
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
[Inference] Implement a "1 class = 1 provider<>task pair" logic to isolate provider-specific code #1315
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
fc0926c
refactor providers
hanouticelina 4e6cf94
nit
hanouticelina 5514cde
nit
hanouticelina 84745e3
remove unnecessary check
hanouticelina 9466000
fix linting
hanouticelina fa3f8f0
implement individual classes for sambanova, cohere and cerebras
hanouticelina a4b4682
add hf-inference helpers
hanouticelina 4dd7e02
fix text-to-image
hanouticelina e3cb303
Merge branch 'main' of github.com:huggingface/huggingface.js into ref…
hanouticelina 213e658
use conversational task
hanouticelina 6c3823e
backward compatibility hf-inference tasks
hanouticelina 8ccab73
fix tests
hanouticelina 59d1457
Merge branch 'main' of github.com:huggingface/huggingface.js into ref…
hanouticelina c6252ae
fixes
hanouticelina e34f2a2
add text-to-audio
hanouticelina 677afc0
improvements and lint
hanouticelina cc4b255
remove code and add missing tasks for replicate
hanouticelina b374452
nit
hanouticelina d0d0f73
Merge branch 'main' of github.com:huggingface/huggingface.js into ref…
hanouticelina d138032
regenerate fal-ai snippet
hanouticelina 20a8864
nit
hanouticelina a9ddea1
no need to 'override'
hanouticelina 5a8c576
fix
hanouticelina 6da3a75
apply suggestions
hanouticelina f87081b
Merge branch 'main' into refactor-providers
hanouticelina 2276e94
some fixes
hanouticelina 6835182
Merge branch 'refactor-providers' of github.com:huggingface/huggingfa…
hanouticelina 67afa27
fix code style
hanouticelina 9d16e7c
group abstract methods
hanouticelina 7782f2b
Better typing + make task functions generic (#1338)
hanouticelina 7753620
Merge branch 'main' of github.com:huggingface/huggingface.js into ref…
hanouticelina 21f9d34
nit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { BlackForestLabsTextToImageTask } from "../providers/black-forest-labs"; | ||
import { FalAITask, FalAITextToImageTask, FalAITextToVideoTask } from "../providers/fal-ai"; | ||
import { FireworksConversationalTask } from "../providers/fireworks-ai"; | ||
import { | ||
HyperbolicConversationalTask, | ||
HyperbolicTextGenerationTask, | ||
HyperbolicTextToImageTask, | ||
} from "../providers/hyperbolic"; | ||
import { NebiusTextToImageTask } from "../providers/nebius"; | ||
import { NovitaConversationalTask, NovitaTextGenerationTask } from "../providers/novita"; | ||
import { OpenAIConversationalTask } from "../providers/openai"; | ||
import type { TaskProviderHelper } from "../providers/providerHelper"; | ||
import { BaseConversationalTask, BaseTextGenerationTask } from "../providers/providerHelper"; | ||
import { ReplicateTextToImageTask, ReplicateTextToSpeechTask, ReplicateTextToVideoTask } from "../providers/replicate"; | ||
import { TogetherConversationalTask, TogetherTextGenerationTask, TogetherTextToImageTask } from "../providers/together"; | ||
import type { InferenceProvider, InferenceTask } from "../types"; | ||
|
||
export const PROVIDERS: Record<InferenceProvider, Partial<Record<InferenceTask, TaskProviderHelper>>> = { | ||
"black-forest-labs": { | ||
"text-to-image": new BlackForestLabsTextToImageTask(), | ||
}, | ||
cerebras: { | ||
conversational: new BaseConversationalTask("cerebras", "https://api.cerebras.ai"), | ||
}, | ||
cohere: { | ||
conversational: new BaseConversationalTask("cohere", "https://api.cohere.ai"), | ||
}, | ||
"fal-ai": { | ||
// TODO: Add automatic-speech-recognition task helper | ||
// "automatic-speech-recognition": new FalAIAutomaticSpeechRecognitionTask(), | ||
"text-to-image": new FalAITextToImageTask(), | ||
"text-to-speech": new FalAITask("text-to-speech"), | ||
"text-to-video": new FalAITextToVideoTask(), | ||
}, | ||
"fireworks-ai": { | ||
conversational: new FireworksConversationalTask(), | ||
}, | ||
"hf-inference": { | ||
//TODO: Add the correct provider helpers for hf-inference | ||
}, | ||
hyperbolic: { | ||
"text-to-image": new HyperbolicTextToImageTask(), | ||
conversational: new HyperbolicConversationalTask(), | ||
"text-generation": new HyperbolicTextGenerationTask(), | ||
}, | ||
nebius: { | ||
"text-to-image": new NebiusTextToImageTask(), | ||
conversational: new BaseConversationalTask("nebius", "https://api.nebius.ai"), | ||
"text-generation": new BaseTextGenerationTask("nebius", "https://api.nebius.ai"), | ||
}, | ||
novita: { | ||
"text-generation": new NovitaTextGenerationTask(), | ||
conversational: new NovitaConversationalTask(), | ||
}, | ||
openai: { | ||
conversational: new OpenAIConversationalTask(), | ||
}, | ||
replicate: { | ||
"text-to-image": new ReplicateTextToImageTask(), | ||
"text-to-speech": new ReplicateTextToSpeechTask(), | ||
"text-to-video": new ReplicateTextToVideoTask(), | ||
}, | ||
sambanova: { | ||
conversational: new BaseConversationalTask("sambanova", "https://api.sambanova.ai"), | ||
}, | ||
together: { | ||
"text-to-image": new TogetherTextToImageTask(), | ||
"text-generation": new TogetherTextGenerationTask(), | ||
conversational: new TogetherConversationalTask(), | ||
}, | ||
}; | ||
|
||
/** | ||
* Get provider helper instance by name and task | ||
*/ | ||
export function getProviderHelper(provider: InferenceProvider, task: InferenceTask | undefined): TaskProviderHelper { | ||
if (!task) { | ||
throw new Error("you need to provide a task name, e.g. 'text-to-image'"); | ||
} | ||
if (!(provider in PROVIDERS)) { | ||
throw new Error(`Provider '${provider}' not supported. Available providers: ${Object.keys(PROVIDERS)}`); | ||
} | ||
hanouticelina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const providerTasks = PROVIDERS[provider]; | ||
if (!providerTasks || !(task in providerTasks)) { | ||
throw new Error( | ||
`Task '${task}' not supported for provider '${provider}'. Available tasks: ${Object.keys(providerTasks ?? {})}` | ||
); | ||
} | ||
const helper = providerTasks[task]; | ||
if (!helper) { | ||
throw new Error(`Internal error: Helper for task '${task}' and provider '${provider}' resolved to undefined.`); | ||
} | ||
return helper; | ||
} |
hanouticelina marked this conversation as resolved.
Show resolved
Hide resolved
|
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
Oops, something went wrong.
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.