Skip to content

Moving models download settings to user settings. #3216

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

Open
wants to merge 6 commits 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
22 changes: 22 additions & 0 deletions browser_tests/tests/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ test.describe('Missing models warning', () => {
test('Should display a warning when missing models are found', async ({
comfyPage
}) => {
await comfyPage.page.evaluate(() => {
const app = window['app']
app.api.getModelsDownloadSettings = async () => {
return {
allowedSources: ['https://civitai.com/', 'https://huggingface.co/'],
allowedSuffixes: ['.safetensors', '.sft'],
whiteListedUrls: new Set([])
}
}
})

await comfyPage.loadWorkflow('missing_models')

const missingModelsWarning = comfyPage.page.locator('.comfy-missing-models')
Expand All @@ -81,6 +92,17 @@ test.describe('Missing models warning', () => {
test('Should display a warning when missing models are found in node properties', async ({
comfyPage
}) => {
await comfyPage.page.evaluate(() => {
const app = window['app']
app.api.getModelsDownloadSettings = async () => {
return {
allowedSources: ['https://civitai.com/', 'https://huggingface.co/'],
allowedSuffixes: ['.safetensors', '.sft'],
whiteListedUrls: new Set([])
}
}
})

// Load workflow that has a node with models metadata at the node level
await comfyPage.loadWorkflow('missing_models_from_node_properties')

Expand Down
37 changes: 19 additions & 18 deletions src/components/dialog/content/MissingModelsWarning.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,19 @@
</template>

<script setup lang="ts">
import { computedAsync } from '@vueuse/core'
import Checkbox from 'primevue/checkbox'
import ListBox from 'primevue/listbox'
import { computed, onBeforeUnmount, ref } from 'vue'
import { onBeforeUnmount, ref } from 'vue'
import { useI18n } from 'vue-i18n'

import FileDownload from '@/components/common/FileDownload.vue'
import NoResultsPlaceholder from '@/components/common/NoResultsPlaceholder.vue'
import { api } from '@/scripts/api'
import { useSettingStore } from '@/stores/settingStore'
import { isElectron } from '@/utils/envUtil'

// TODO: Read this from server internal API rather than hardcoding here
// as some installations may wish to use custom sources
const allowedSources = [
'https://civitai.com/',
'https://huggingface.co/',
'http://localhost:' // Included for testing usage only
]
const allowedSuffixes = ['.safetensors', '.sft']
// Models that fail above conditions but are still allowed
const whiteListedUrls = new Set([
'https://huggingface.co/stabilityai/stable-zero123/resolve/main/stable_zero123.ckpt',
'https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_depth_sd14v1.pth?download=true',
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
])
let modelsDownloadConfig: any

interface ModelInfo {
name: string
Expand All @@ -78,7 +67,15 @@ const { t } = useI18n()
const doNotAskAgain = ref(false)

const modelDownloads = ref<Record<string, ModelInfo>>({})
const missingModels = computed(() => {
const missingModels = computedAsync(async () => {
if (!modelsDownloadConfig) {
modelsDownloadConfig = await api.getModelsDownloadSettings()
}
// Custom models sources, extension and whitelist can be customized in user settings.
const allowedSources = modelsDownloadConfig.allowedSources || []
const allowedSuffixes = modelsDownloadConfig.allowedSuffixes || []
const whiteListedUrls = new Set(modelsDownloadConfig.whitelistedUrls || [])

return props.missingModels.map((model) => {
const paths = props.paths[model.directory]
if (model.directory_invalid || !paths) {
Expand All @@ -100,14 +97,18 @@ const missingModels = computed(() => {
}
modelDownloads.value[model.name] = downloadInfo
if (!whiteListedUrls.has(model.url)) {
if (!allowedSources.some((source) => model.url.startsWith(source))) {
if (
!allowedSources.some((source: string) => model.url.startsWith(source))
) {
return {
label: `${model.directory} / ${model.name}`,
url: model.url,
error: `Download not allowed from source '${model.url}', only allowed from '${allowedSources.join("', '")}'`
}
}
if (!allowedSuffixes.some((suffix) => model.name.endsWith(suffix))) {
if (
!allowedSuffixes.some((suffix: string) => model.name.endsWith(suffix))
) {
return {
label: `${model.directory} / ${model.name}`,
url: model.url,
Expand Down
28 changes: 28 additions & 0 deletions src/constants/coreSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,34 @@ export const CORE_SETTINGS: SettingParams[] = [
},
versionAdded: '1.15.7'
},
{
id: 'Comfy.ModelLibrary.AllowedSources',
category: ['Comfy', 'Model', 'Download'],
name: 'Model Download Allowed Sources',
type: 'hidden',
defaultValue: ['https://civitai.com/', 'https://huggingface.co/'],
versionAdded: '1.16.0'
},
{
id: 'Comfy.ModelLibrary.AllowedSuffixes',
category: ['Comfy', 'Model', 'Download'],
name: 'Model Download Allowed Suffixes',
type: 'hidden',
defaultValue: ['.safetensors', '.sft'],
versionAdded: '1.16.0'
},
{
id: 'Comfy.ModelLibrary.WhitelistedUrls',
category: ['Comfy', 'Model', 'Download'],
name: 'Model Download Whitelisted Urls',
type: 'hidden',
defaultValue: [
'https://huggingface.co/stabilityai/stable-zero123/resolve/main/stable_zero123.ckpt',
'https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_depth_sd14v1.pth?download=true',
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
],
versionAdded: '1.16.0'
},
{
id: 'Comfy.Toast.DisableReconnectingToast',
name: 'Disable toasts when reconnecting or reconnected',
Expand Down
14 changes: 14 additions & 0 deletions src/schemas/apiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ const zSettings = z.object({
'Topbar',
'Topbar (2nd-row)'
]),
'Comfy.ModelLibrary.AllowedSources': z.array(z.string()),
'Comfy.ModelLibrary.AllowedSuffixes': z.array(z.string()),
'Comfy.ModelLibrary.WhitelistedUrls': z.array(z.string()),
'Comfy.Node.DoubleClickTitleToEdit': z.boolean(),
'Comfy.WidgetControlMode': z.enum(['before', 'after']),
'Comfy.Window.UnloadConfirmation': z.boolean(),
Expand Down Expand Up @@ -459,6 +462,16 @@ const zSettings = z.object({
'LiteGraph.Node.DefaultPadding': z.boolean()
})

const zModelsDownloadSettings = z.record(z.any()).and(
z
.object({
allowedSources: z.array(z.string()),
allowedSuffixes: z.array(z.string()),
whiteListedUrls: z.array(z.string())
})
.optional()
)

export type EmbeddingsResponse = z.infer<typeof zEmbeddingsResponse>
export type ExtensionsResponse = z.infer<typeof zExtensionsResponse>
export type PromptResponse = z.infer<typeof zPromptResponse>
Expand All @@ -472,3 +485,4 @@ export type UserDataFullInfo = z.infer<typeof zUserDataFullInfo>
export type TerminalSize = z.infer<typeof zTerminalSize>
export type LogEntry = z.infer<typeof zLogEntry>
export type LogsRawResponse = z.infer<typeof zLogRawResponse>
export type ModelsDownloadSettings = z.infer<typeof zModelsDownloadSettings>
17 changes: 17 additions & 0 deletions src/scripts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
HistoryTaskItem,
LogsRawResponse,
LogsWsMessage,
ModelsDownloadSettings,
PendingTaskItem,
ProgressWsMessage,
PromptResponse,
Expand Down Expand Up @@ -922,6 +923,22 @@ export class ComfyApi extends EventTarget {
async getCustomNodesI18n(): Promise<Record<string, any>> {
return (await axios.get(this.apiURL('/i18n'))).data
}

async getModelsDownloadSettings(): Promise<ModelsDownloadSettings> {
const config = (
await axios.get(this.internalURL('/models_download/config'))
).data
if (
location.hostname === 'localhost' ||
location.hostname === '127.0.0.1'
) {
if (!config.allowedSources) {
config.allowedSources = []
}
config.allowedSources.push('http://localhost:')
}
return config
}
}

export const api = new ComfyApi()