-
Notifications
You must be signed in to change notification settings - Fork 1
SEG-52: add v2 async helpers (submit_async, run_async, AsyncJob) #1
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d19b30
SEG-52: add v2 async helpers (submit_async, run_async, AsyncJob)
2c34e83
review: drop unused _TERMINAL_STATES + skip extra GET on FAILED
0d2b8f1
tester-fix: surface InferenceFailed when heimdall returns 4xx-on-FAIL…
3dd52af
ci: bump Pages actions past deprecated upload-artifact@v3
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,11 +6,19 @@ | |||||||||||||||||||||||
| Usage: | ||||||||||||||||||||||||
| import segmind | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Run a model | ||||||||||||||||||||||||
| # Run a model (sync v1) | ||||||||||||||||||||||||
| response = segmind.run("seedream-v3-text-to-image", prompt="A sunset") | ||||||||||||||||||||||||
| with open("image.jpg", "wb") as f: | ||||||||||||||||||||||||
| f.write(response.content) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Run a model (v2 async — submit + poll until done) | ||||||||||||||||||||||||
| result = segmind.run_async("seedance-1-pro", prompt="A sunset", timeout=300) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Or split the submit / wait for finer control | ||||||||||||||||||||||||
| job = segmind.submit_async("seedance-1-pro", prompt="A sunset") | ||||||||||||||||||||||||
| print(job.request_id) | ||||||||||||||||||||||||
| result = job.wait(timeout=300) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Upload files | ||||||||||||||||||||||||
| result = segmind.files.upload("image.png") | ||||||||||||||||||||||||
| print(result["file_urls"]) | ||||||||||||||||||||||||
|
|
@@ -22,6 +30,13 @@ | |||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| from segmind.client import SegmindClient | ||||||||||||||||||||||||
| from segmind.v2 import ( | ||||||||||||||||||||||||
| DEFAULT_POLL_INTERVAL_S, | ||||||||||||||||||||||||
| DEFAULT_POLL_TIMEOUT_S, | ||||||||||||||||||||||||
| AsyncJob, | ||||||||||||||||||||||||
| InferenceFailed, | ||||||||||||||||||||||||
| InferenceTimeout, | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| __version__ = "1.0.0" | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -38,7 +53,7 @@ def _get_client() -> SegmindClient: | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def run(slug: str, **params): | ||||||||||||||||||||||||
| """Run a model inference request. | ||||||||||||||||||||||||
| """Run a sync (v1) model inference request. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||
| slug: Model slug/identifier | ||||||||||||||||||||||||
|
|
@@ -56,6 +71,55 @@ def run(slug: str, **params): | |||||||||||||||||||||||
| return _get_client().run(slug, **params) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def submit_async(slug: str, **params) -> AsyncJob: | ||||||||||||||||||||||||
| """Submit a v2 async inference request and return a job handle. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| The handle exposes `.wait()`, `.status()`, and `.result()`. Use `.wait()` | ||||||||||||||||||||||||
| to block until COMPLETED or FAILED. Useful when you want to track the | ||||||||||||||||||||||||
| request_id, run other work in parallel, or batch many submissions. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||
| slug: Model slug/identifier. | ||||||||||||||||||||||||
| **params: Parameters to pass to the model. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Example: | ||||||||||||||||||||||||
| import segmind | ||||||||||||||||||||||||
| job = segmind.submit_async("seedance-1-pro", prompt="A sunset") | ||||||||||||||||||||||||
| print(job.request_id) | ||||||||||||||||||||||||
| result = job.wait(timeout=300) | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| return _get_client().submit_async(slug, **params) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def run_async( | ||||||||||||||||||||||||
| slug: str, | ||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||
| timeout: float = DEFAULT_POLL_TIMEOUT_S, | ||||||||||||||||||||||||
| interval: float = DEFAULT_POLL_INTERVAL_S, | ||||||||||||||||||||||||
| **params, | ||||||||||||||||||||||||
| ) -> dict: | ||||||||||||||||||||||||
| """Run a v2 async inference request to completion (submit + poll). | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||
| slug: Model slug/identifier. | ||||||||||||||||||||||||
| timeout: Hard deadline in seconds (default 600s). | ||||||||||||||||||||||||
| interval: Status-poll cadence (default 1.0s). | ||||||||||||||||||||||||
| **params: Parameters to pass to the model. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||
| The final response body once the task reaches COMPLETED. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Raises: | ||||||||||||||||||||||||
| segmind.InferenceFailed: server returned FAILED. | ||||||||||||||||||||||||
| segmind.InferenceTimeout: timeout elapsed before terminal state. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Example: | ||||||||||||||||||||||||
| import segmind | ||||||||||||||||||||||||
| result = segmind.run_async("seedance-1-pro", prompt="A sunset", timeout=300) | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| return _get_client().run_async(slug, timeout=timeout, interval=interval, **params) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Namespace proxies | ||||||||||||||||||||||||
| class _Files: | ||||||||||||||||||||||||
| def upload(self, file_paths): | ||||||||||||||||||||||||
|
|
@@ -123,11 +187,18 @@ def recent(self, model_name): | |||||||||||||||||||||||
| generations = _Generations() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| __all__ = [ | ||||||||||||||||||||||||
| "DEFAULT_POLL_INTERVAL_S", | ||||||||||||||||||||||||
| "DEFAULT_POLL_TIMEOUT_S", | ||||||||||||||||||||||||
| "AsyncJob", | ||||||||||||||||||||||||
| "InferenceFailed", | ||||||||||||||||||||||||
| "InferenceTimeout", | ||||||||||||||||||||||||
|
Comment on lines
+190
to
+194
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add SegmindError to all to explicitly export it as part of the public API.
Suggested change
|
||||||||||||||||||||||||
| "SegmindClient", | ||||||||||||||||||||||||
| "files", | ||||||||||||||||||||||||
| "generations", | ||||||||||||||||||||||||
| "models", | ||||||||||||||||||||||||
| "pixelflows", | ||||||||||||||||||||||||
| "run", | ||||||||||||||||||||||||
| "run_async", | ||||||||||||||||||||||||
| "submit_async", | ||||||||||||||||||||||||
| "webhooks", | ||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exposing SegmindError at the package level (segmind.SegmindError) is highly recommended so that users of the SDK can easily import and catch the base exception class without needing to import from internal modules. This also avoids workarounds in the test suite.