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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
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
11 changes: 11 additions & 0 deletions src/schemas/apiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,16 @@ const zSettings = z.record(z.any()).and(
.optional()
)

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 @@ -423,3 +433,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 @@ -898,6 +899,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()
Loading