-
Notifications
You must be signed in to change notification settings - Fork 434
Add debug script to check inference snippets + update Python text-to-image snippets #994
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
461d989
Add debug script to check inference snippets
Wauplin e172a27
Add script to check inference snippet changes + update Python textToI…
Wauplin 3772f29
revert
Wauplin b3deb60
no new deps
Wauplin 61d2e33
forgot this one
Wauplin 24793d8
fix install
Wauplin 06451a3
simple
Wauplin 79232a9
not a fix
Wauplin f988d7d
Merge branch 'main' into update-python-inference-snippets-2
Wauplin 30d5d4d
Update packages/tasks/scripts/check-snippets.ts
Wauplin 251856d
Update packages/tasks/scripts/check-snippets.ts
Wauplin b11f875
no need for that
Wauplin 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,42 @@ | ||
name: Inference check snippets | ||
on: | ||
pull_request: | ||
paths: | ||
- "packages/tasks/src/snippets/**" | ||
- ".github/workflows/inference-check-snippets.yml" | ||
|
||
jobs: | ||
check-snippets: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- run: corepack enable | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: "20" | ||
cache: "pnpm" | ||
cache-dependency-path: "**/pnpm-lock.yaml" | ||
- run: | | ||
cd packages/tasks | ||
pnpm install | ||
|
||
# TODO: Find a way to run on all pipeline tags | ||
# TODO: print snippet only if it has changed since the last commit on main (?) | ||
# TODO: (even better: automated message on the PR with diff) | ||
- name: Print text-to-image snippets | ||
run: | | ||
cd packages/tasks | ||
pnpm run check-snippets --pipeline-tag="text-to-image" | ||
|
||
- name: Print simple text-generation snippets | ||
run: | | ||
cd packages/tasks | ||
pnpm run check-snippets --pipeline-tag="text-generation" | ||
|
||
- name: Print conversational text-generation snippets | ||
run: | | ||
cd packages/tasks | ||
pnpm run check-snippets --pipeline-tag="text-generation" --tags="conversational" |
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,55 @@ | ||
/* | ||
* Generates inference snippets as they would be shown on the Hub for Curl, JS and Python. | ||
* Snippets will only be printed to the terminal to make it easier to debug when making changes to the snippets. | ||
* | ||
* Usage: | ||
* pnpm run check-snippets --pipeline-tag="text-generation" --tags="conversational" | ||
* pnpm run check-snippets --pipeline-tag="image-text-to-text" --tags="conversational" | ||
* pnpm run check-snippets --pipeline-tag="text-to-image" | ||
* | ||
* This script is meant only for debug purposes. | ||
*/ | ||
import { python, curl, js } from "../src/snippets/index"; | ||
import type { InferenceSnippet, ModelDataMinimal } from "../src/snippets/types"; | ||
import type { PipelineType } from "../src/pipelines"; | ||
|
||
// Parse command-line arguments | ||
const args = process.argv.slice(2).reduce( | ||
(acc, arg) => { | ||
const [key, value] = arg.split("="); | ||
acc[key.replace("--", "")] = value; | ||
return acc; | ||
}, | ||
{} as { [key: string]: string } | ||
); | ||
|
||
const accessToken = "hf_**********"; | ||
const pipelineTag = (args["pipeline-tag"] || "text-generation") as PipelineType; | ||
const tags = (args["tags"] ?? "").split(","); | ||
|
||
const modelMinimal: ModelDataMinimal = { | ||
id: "llama-6-1720B-Instruct", | ||
pipeline_tag: pipelineTag, | ||
tags: tags, | ||
inference: "****", | ||
}; | ||
|
||
const printSnippets = (snippets: InferenceSnippet | InferenceSnippet[], language: string) => { | ||
const snippetArray = Array.isArray(snippets) ? snippets : [snippets]; | ||
snippetArray.forEach((snippet) => { | ||
console.log(`\n\x1b[33m${language} ${snippet.client}\x1b[0m`); | ||
console.log(`\n\`\`\`${language}\n${snippet.content}\n\`\`\`\n`); | ||
}); | ||
}; | ||
|
||
const generateAndPrintSnippets = ( | ||
generator: (model: ModelDataMinimal, token: string) => InferenceSnippet | InferenceSnippet[], | ||
language: string | ||
) => { | ||
const snippets = generator(modelMinimal, accessToken); | ||
printSnippets(snippets, language); | ||
}; | ||
|
||
generateAndPrintSnippets(curl.getCurlInferenceSnippet, "curl"); | ||
generateAndPrintSnippets(python.getPythonInferenceSnippet, "python"); | ||
generateAndPrintSnippets(js.getJsInferenceSnippet, "js"); |
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 |
---|---|---|
|
@@ -4,6 +4,11 @@ import { stringifyGenerationConfig, stringifyMessages } from "./common.js"; | |
import { getModelInputSnippet } from "./inputs.js"; | ||
import type { InferenceSnippet, ModelDataMinimal } from "./types.js"; | ||
|
||
const snippetImportInferenceClient = (model: ModelDataMinimal, accessToken: string): string => | ||
`from huggingface_hub import InferenceClient | ||
|
||
client = InferenceClient(${model.id}, token="${accessToken || "{API_TOKEN}"}")`; | ||
|
||
export const snippetConversational = ( | ||
model: ModelDataMinimal, | ||
accessToken: string, | ||
|
@@ -168,18 +173,31 @@ export const snippetFile = (model: ModelDataMinimal): InferenceSnippet => ({ | |
output = query(${getModelInputSnippet(model)})`, | ||
}); | ||
|
||
export const snippetTextToImage = (model: ModelDataMinimal): InferenceSnippet => ({ | ||
content: `def query(payload): | ||
export const snippetTextToImage = (model: ModelDataMinimal, accessToken: string): InferenceSnippet[] => { | ||
return [ | ||
{ | ||
client: "requests", | ||
content: `def query(payload): | ||
response = requests.post(API_URL, headers=headers, json=payload) | ||
return response.content | ||
|
||
image_bytes = query({ | ||
"inputs": ${getModelInputSnippet(model)}, | ||
}) | ||
# You can access the image with PIL.Image for example | ||
import io | ||
from PIL import Image | ||
image = Image.open(io.BytesIO(image_bytes))`, | ||
}); | ||
}, | ||
{ | ||
client: "huggingface_hub", | ||
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. maybe let's agree on the convention of putting |
||
content: `${snippetImportInferenceClient(model, accessToken)} | ||
|
||
# output is a PIL.Image object | ||
image = client.text_to_image(${getModelInputSnippet(model)})`, | ||
}, | ||
]; | ||
}; | ||
|
||
export const snippetTabular = (model: ModelDataMinimal): InferenceSnippet => ({ | ||
content: `def query(payload): | ||
|
@@ -284,6 +302,9 @@ export function getPythonInferenceSnippet( | |
if (model.tags.includes("conversational")) { | ||
// Conversational model detected, so we display a code snippet that features the Messages API | ||
return snippetConversational(model, accessToken, opts); | ||
} else if (model.pipeline_tag == "text-to-image") { | ||
// TODO: factorize this logic | ||
return snippetTextToImage(model, accessToken); | ||
} else { | ||
let snippets = | ||
model.pipeline_tag && model.pipeline_tag in pythonSnippets | ||
|
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.
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.
I've implemented tests in https://github.com/huggingface/huggingface.js/pull/1003/files using the vite test convention of
xyz.spec.ts
files (which run onpnpm test
).I think we should just put more tests into
xyz.spec.ts
files rather than creating a custom mechanism ofcheck-snippet.ts
&inference-check-snippets.yml
. Or am I missing some necessary details?Wdyt?
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.
much better yes!
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.
I went with this solution because I was bored about how difficult it was to debug things locally but automated tests feel much more natural now that you mention it ^^