Skip to content
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

Document python text to image snippets #1016

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions packages/tasks/src/snippets/python.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ModelDataMinimal } from "./types";
import type { InferenceSnippet, ModelDataMinimal } from "./types";
import { describe, expect, it } from "vitest";
import { snippetConversational } from "./python";
import { snippetConversational, getPythonInferenceSnippet } from "./python";

describe("inference API snippets", () => {
it("conversational llm", async () => {
Expand Down Expand Up @@ -75,4 +75,39 @@ stream = client.chat.completions.create(
for chunk in stream:
print(chunk.choices[0].delta.content, end="")`);
});

it("text-to-image", async () => {
const model: ModelDataMinimal = {
id: "black-forest-labs/FLUX.1-schnell",
pipeline_tag: "text-to-image",
tags: [],
inference: "",
};
const snippets = getPythonInferenceSnippet(model, "api_token") as InferenceSnippet[];

expect(snippets.length).toEqual(2);

expect(snippets[0].client).toEqual("huggingface_hub");
expect(snippets[0].content).toEqual(`from huggingface_hub import InferenceClient
client = InferenceClient("black-forest-labs/FLUX.1-schnell", token="api_token")
# output is a PIL.Image object
image = client.text_to_image("Astronaut riding a horse")`);

expect(snippets[1].client).toEqual("requests");
expect(snippets[1].content).toEqual(`import requests

API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
image_bytes = query({
"inputs": "Astronaut riding a horse",
})
# You can access the image with PIL.Image for example
import io
from PIL import Image
image = Image.open(io.BytesIO(image_bytes))`);
});
});
25 changes: 20 additions & 5 deletions packages/tasks/src/snippets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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,
Expand Down Expand Up @@ -161,8 +165,16 @@ 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[] => [
{
client: "huggingface_hub",
content: `${snippetImportInferenceClient(model, accessToken)}
# output is a PIL.Image object
image = client.text_to_image(${getModelInputSnippet(model)})`,
},
{
client: "requests",
content: `def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
image_bytes = query({
Expand All @@ -172,7 +184,8 @@ image_bytes = query({
import io
from PIL import Image
image = Image.open(io.BytesIO(image_bytes))`,
});
},
];

export const snippetTabular = (model: ModelDataMinimal): InferenceSnippet => ({
content: `def query(payload):
Expand Down Expand Up @@ -288,12 +301,14 @@ export function getPythonInferenceSnippet(
return snippets.map((snippet) => {
return {
...snippet,
content: `import requests
content: snippet.content.includes("requests")
? `import requests

API_URL = "https://api-inference.huggingface.co/models/${model.id}"
headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}}

${snippet.content}`,
${snippet.content}`
: snippet.content,
};
});
}
Expand Down
Loading