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

Improve prompting for diffusers default snippets #909

Merged
merged 14 commits into from
Sep 19, 2024
Merged
1 change: 1 addition & 0 deletions packages/tasks/src/model-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface ModelData {
parameters?: Record<string, unknown>;
};
base_model?: string | string[];
instance_prompt?: string;
};
/**
* Library name
Expand Down
16 changes: 14 additions & 2 deletions packages/tasks/src/model-libraries-snippets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ModelData } from "./model-data";
import type { WidgetExampleTextInput } from "./widget-example";
import { LIBRARY_TASK_MAPPING } from "./library-to-tasks";

const TAG_CUSTOM_CODE = "custom_code";
Expand All @@ -8,6 +9,8 @@ function nameWithoutNamespace(modelId: string): string {
return splitted.length === 1 ? splitted[0] : splitted[1];
}

const escapeStringForJson = (str: string): string => JSON.stringify(str);

//#region snippets

export const adapters = (model: ModelData): string[] => [
Expand Down Expand Up @@ -70,6 +73,13 @@ function get_base_diffusers_model(model: ModelData): string {
return model.cardData?.base_model?.toString() ?? "fill-in-base-model";
}

function get_prompt_from_diffusers_model(model: ModelData): string | undefined {
const prompt = (model.widgetData?.[0] as WidgetExampleTextInput).text ?? model.cardData?.instance_prompt;
if (prompt) {
return escapeStringForJson(prompt);
}
}

export const bertopic = (model: ModelData): string[] => [
`from bertopic import BERTopic

Expand Down Expand Up @@ -129,12 +139,14 @@ depth = model.infer_image(raw_img) # HxW raw depth map in numpy
];
};

const diffusersDefaultPrompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k";

const diffusers_default = (model: ModelData) => [
`from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("${model.id}")

prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
prompt = "${get_prompt_from_diffusers_model(model) ?? diffusersDefaultPrompt}"
image = pipe(prompt).images[0]`,
];

Expand All @@ -153,7 +165,7 @@ const diffusers_lora = (model: ModelData) => [
pipe = DiffusionPipeline.from_pretrained("${get_base_diffusers_model(model)}")
pipe.load_lora_weights("${model.id}")

prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
prompt = "${get_prompt_from_diffusers_model(model) ?? diffusersDefaultPrompt}"
image = pipe(prompt).images[0]`,
];

Expand Down
Loading