-
Notifications
You must be signed in to change notification settings - Fork 55
feat: report kernel bot upload results #1033
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
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env python3 | ||
| # Input: | ||
| # [--base <comment-body>] <kernel> <repo-prefix> <upload-json-or-path>... | ||
| # | ||
| # Output: | ||
| # A Hub upload section, or <comment-body> with that section updated. | ||
| # | ||
| # Example: | ||
| # python3 .github/scripts/format_hub_upload_comment.py msa kernels-community "$RUNNER_TEMP"/upload*.json | ||
| # python3 .github/scripts/format_hub_upload_comment.py --base "$BODY" msa kernels-community "$RUNNER_TEMP"/upload*.json | ||
| # python3 .github/scripts/format_hub_upload_comment.py msa kernels-community '{"pull_requests":[{"url":"https://hf.co/kernels/MiniMaxAI/msa/discussions/1"}]}' | ||
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from hub_pr_upload_args import repo_id | ||
|
|
||
| SECTION = "Hub uploads:" | ||
|
|
||
|
|
||
| def load_json(value): | ||
| if not value.lstrip().startswith(("{", "[")): | ||
| path = Path(value) | ||
| if path.is_file(): | ||
| with open(path) as f: | ||
| return json.load(f) | ||
| return json.loads(value) | ||
|
|
||
|
|
||
| def dedupe(lines): | ||
| return list(dict.fromkeys(lines)) | ||
|
|
||
|
|
||
| def upload_lines(kernel, repo_prefix, uploads): | ||
| urls = [ | ||
| pr["url"] | ||
| for upload in uploads | ||
| for pr in load_json(upload).get("pull_requests", []) | ||
| ] | ||
| lines = [ | ||
| f"- Hub repo: https://huggingface.co/kernels/{repo_id(kernel, repo_prefix)}" | ||
| ] | ||
| lines.extend(f"- Hub pull request: {url}" for url in urls) | ||
| return dedupe(lines) | ||
|
|
||
|
|
||
| def update_comment(base, lines): | ||
| body_lines = base.rstrip().splitlines() | ||
| if SECTION in body_lines: | ||
| section = body_lines.index(SECTION) | ||
| prefix = body_lines[:section] | ||
| existing = [line for line in body_lines[section + 1 :] if line.startswith("- ")] | ||
| else: | ||
| prefix = body_lines | ||
| existing = [] | ||
| body = "\n".join(prefix).rstrip() | ||
| section = "\n".join([SECTION, *dedupe(existing + lines)]) | ||
| return f"{body}\n\n{section}".strip() | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--base") | ||
| parser.add_argument("kernel") | ||
| parser.add_argument("repo_prefix") | ||
| parser.add_argument("uploads", nargs="+") | ||
| args = parser.parse_args() | ||
|
|
||
| lines = upload_lines(args.kernel, args.repo_prefix, args.uploads) | ||
| if args.base is not None: | ||
| print(update_comment(args.base, lines)) | ||
| else: | ||
| print("\n".join([SECTION, *lines])) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
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,35 @@ | ||
| #!/usr/bin/env python3 | ||
| # Input: | ||
| # <repo-id|upload-args> <kernel> <repo-prefix> | ||
| # | ||
| # Output: | ||
| # The effective Hub repo-id, or upload args using --create-pr for external repos. | ||
| # | ||
| # Example: | ||
| # python3 .github/scripts/hub_pr_upload_args.py upload-args msa kernels-community | ||
| import sys | ||
| import tomllib | ||
| from pathlib import Path | ||
|
|
||
| COMMUNITY = "kernels-community" | ||
| ROOT = Path(__file__).resolve().parents[2] | ||
|
|
||
|
|
||
| def external_repo_id(kernel): | ||
| with open(ROOT / kernel / "build.toml", "rb") as f: | ||
| repo_id = tomllib.load(f).get("general", {}).get("hub", {}).get("repo-id") | ||
| return repo_id if isinstance(repo_id, str) and repo_id and not repo_id.startswith(f"{COMMUNITY}/") else "" | ||
|
|
||
|
|
||
| def repo_id(kernel, repo_prefix): | ||
| return external_repo_id(kernel) or f"{repo_prefix}/{kernel}" | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| mode, kernel, repo_prefix = sys.argv[1:] | ||
| if mode == "repo-id": | ||
| print(repo_id(kernel, repo_prefix)) | ||
| elif mode == "upload-args": | ||
| print("--create-pr" if external_repo_id(kernel) else f"--repo-id {repo_prefix}/{kernel}") | ||
| else: | ||
| sys.exit(f"unknown mode: {mode}") |
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
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.
(nit): Possible to include an actual representative example?
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.
just below this section are some copy pastable commands to test the script and see the output. for example in the PR description above there is an example running the script and its output
I avoided including the string output in the comment in the case we tweak the formatting - however it should be very easy to run the command to see what the output would look like using on of the examples in the header comments