Skip to content

Commit

Permalink
Make ultralytics code snippet generic (#1101)
Browse files Browse the repository at this point in the history
Follow-up PR after
#1093 and especially
this thread
#1093 (comment).

I agree with @NielsRogge's point that `ultralytics` _is_ the library
name. Yolov8 or Yolov10 are "just" model names.

---

_originally from @merveenoyan
(#1093 (comment)
> there's no way of knowing which yolo version would it be because
there's no config file.

The solution I suggest in this PR is to use the repo tags instead. I
have checked and all 258 [ultralytics
models](https://huggingface.co/models?other=ultralytics) have a "yolovX"
tag.
~In parallel I suggest to remove the `yolov10` library since code
snippet is now correctly generated for them as well. I have checked and
[29 out of 63 yolov10
models](https://huggingface.co/models?other=yolov10) don't have the
`ultranalytics` tag so they'll loose the code snippet. If we agree on
this solution I'll open PRs on these repos and everything should be
good.~ See below: in the end, I kept `yolov10` but unified the code
snippet generation.

@merveenoyan @NielsRogge @pcuenca let me know what you think. Will this
definitely close the yolo/ultranalytics tags matter ?

**Note:** some models have multiple yolo tags (e.g.
https://huggingface.co/Ultralytics/YOLOv8 has v10, v8, v3, v5, v9). In
that case I suggest to only take the first one (v10 in this case). But
to be honest, this looks more like something to fix in the model's
metadata
[here](https://huggingface.co/Ultralytics/YOLOv8/blob/main/README.md?code=true#L13).
  • Loading branch information
Wauplin authored Jan 13, 2025
1 parent 51413a4 commit ba42e0b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
27 changes: 14 additions & 13 deletions packages/tasks/src/model-libraries-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,23 +1028,24 @@ wavs = chat.infer(texts, )
torchaudio.save("output1.wav", torch.from_numpy(wavs[0]), 24000)`,
];

export const ultralytics = (model: ModelData): string[] => [
`from ultralytics import YOLOv8 # modify the yolo version here
export const ultralytics = (model: ModelData): string[] => {
// ultralytics models must have a version tag (e.g. `yolov8`)
const versionTag = model.tags.find((tag) => tag.match(/^yolov\d+$/));

model = YOLOv8.from_pretrained("${model.id}")
source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
model.predict(source=source, save=True)
`,
];
const className = versionTag ? `YOLOv${versionTag.slice(4)}` : "YOLOvXX";
const prefix = versionTag
? ""
: `# Couldn't find a valid YOLO version tag.\n# Replace XX with the correct version.\n`;

export const yolov10 = (model: ModelData): string[] => [
`from ultralytics import YOLOv10
return [
prefix +
`from ultralytics import ${className}
model = YOLOv10.from_pretrained("${model.id}")
model = ${className}.from_pretrained("${model.id}")
source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
model.predict(source=source, save=True)
`,
];
model.predict(source=source, save=True)`,
];
};

export const birefnet = (model: ModelData): string[] => [
`# Option 1: use with transformers
Expand Down
35 changes: 18 additions & 17 deletions packages/tasks/src/model-libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,15 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
repoUrl: "https://github.com/microsoft/TRELLIS",
countDownloads: `path_extension:"safetensors"`,
},
ultralytics: {
prettyLabel: "ultralytics",
repoName: "ultralytics",
repoUrl: "https://github.com/ultralytics/ultralytics",
docsUrl: "https://github.com/ultralytics/ultralytics",
filter: false,
countDownloads: `path_extension:"pt"`,
snippets: snippets.ultralytics,
},
"unity-sentis": {
prettyLabel: "unity-sentis",
repoName: "unity-sentis",
Expand Down Expand Up @@ -848,23 +857,6 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
docsUrl: "https://github.com/jasonppy/VoiceCraft",
snippets: snippets.voicecraft,
},
ultralytics: {
prettyLabel: "ultralytics",
repoName: "ultralytics",
repoUrl: "https://github.com/ultralytics/ultralytics",
docsUrl: "https://github.com/ultralytics/ultralytics",
filter: false,
countDownloads: `path_extension:"pt"`,
snippets: snippets.ultralytics,
},
yolov10: {
prettyLabel: "YOLOv10",
repoName: "yolov10",
repoUrl: "https://github.com/THU-MIG/yolov10",
docsUrl: "https://github.com/THU-MIG/yolov10",
countDownloads: `path_extension:"pt"`,
snippets: snippets.yolov10,
},
whisperkit: {
prettyLabel: "WhisperKit",
repoName: "WhisperKit",
Expand All @@ -873,6 +865,15 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
snippets: snippets.whisperkit,
countDownloads: `path_filename:"model" AND path_extension:"mil" AND _exists_:"path_prefix"`,
},
yolov10: {
// YOLOv10 is a fork of ultraLytics. Code snippets and download count are the same but the repo is different.
prettyLabel: "YOLOv10",
repoName: "YOLOv10",
repoUrl: "https://github.com/THU-MIG/yolov10",
docsUrl: "https://github.com/THU-MIG/yolov10",
countDownloads: `path_extension:"pt"`,
snippets: snippets.ultralytics,
},
"3dtopia-xl": {
prettyLabel: "3DTopia-XL",
repoName: "3DTopia-XL",
Expand Down

0 comments on commit ba42e0b

Please sign in to comment.