forked from open-webui/open-webui
refac: image generation
This commit is contained in:
parent
f1c8e7f5d1
commit
dd3a4b3889
2 changed files with 125 additions and 81 deletions
|
@ -1,9 +1,9 @@
|
||||||
import { IMAGES_API_BASE_URL } from '$lib/constants';
|
import { IMAGES_API_BASE_URL } from '$lib/constants';
|
||||||
|
|
||||||
export const getImageGenerationEnabledStatus = async (token: string = '') => {
|
export const getImageGenerationConfig = async (token: string = '') => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const res = await fetch(`${IMAGES_API_BASE_URL}/enabled`, {
|
const res = await fetch(`${IMAGES_API_BASE_URL}/config`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
|
@ -32,16 +32,24 @@ export const getImageGenerationEnabledStatus = async (token: string = '') => {
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const toggleImageGenerationEnabledStatus = async (token: string = '') => {
|
export const updateImageGenerationConfig = async (
|
||||||
|
token: string = '',
|
||||||
|
engine: string,
|
||||||
|
enabled: boolean
|
||||||
|
) => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const res = await fetch(`${IMAGES_API_BASE_URL}/enabled/toggle`, {
|
const res = await fetch(`${IMAGES_API_BASE_URL}/config/update`, {
|
||||||
method: 'GET',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
...(token && { authorization: `Bearer ${token}` })
|
...(token && { authorization: `Bearer ${token}` })
|
||||||
}
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
engine,
|
||||||
|
enabled
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.then(async (res) => {
|
.then(async (res) => {
|
||||||
if (!res.ok) throw await res.json();
|
if (!res.ok) throw await res.json();
|
||||||
|
@ -263,7 +271,7 @@ export const updateImageSteps = async (token: string = '', steps: number) => {
|
||||||
return res.IMAGE_STEPS;
|
return res.IMAGE_STEPS;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDiffusionModels = async (token: string = '') => {
|
export const getImageGenerationModels = async (token: string = '') => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const res = await fetch(`${IMAGES_API_BASE_URL}/models`, {
|
const res = await fetch(`${IMAGES_API_BASE_URL}/models`, {
|
||||||
|
@ -295,7 +303,7 @@ export const getDiffusionModels = async (token: string = '') => {
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDefaultDiffusionModel = async (token: string = '') => {
|
export const getDefaultImageGenerationModel = async (token: string = '') => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const res = await fetch(`${IMAGES_API_BASE_URL}/models/default`, {
|
const res = await fetch(`${IMAGES_API_BASE_URL}/models/default`, {
|
||||||
|
@ -327,7 +335,7 @@ export const getDefaultDiffusionModel = async (token: string = '') => {
|
||||||
return res.model;
|
return res.model;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateDefaultDiffusionModel = async (token: string = '', model: string) => {
|
export const updateDefaultImageGenerationModel = async (token: string = '', model: string) => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const res = await fetch(`${IMAGES_API_BASE_URL}/models/default/update`, {
|
const res = await fetch(`${IMAGES_API_BASE_URL}/models/default/update`, {
|
||||||
|
|
|
@ -5,13 +5,13 @@
|
||||||
import { config, user } from '$lib/stores';
|
import { config, user } from '$lib/stores';
|
||||||
import {
|
import {
|
||||||
getAUTOMATIC1111Url,
|
getAUTOMATIC1111Url,
|
||||||
getDefaultDiffusionModel,
|
getImageGenerationModels,
|
||||||
getDiffusionModels,
|
getDefaultImageGenerationModel,
|
||||||
getImageGenerationEnabledStatus,
|
updateDefaultImageGenerationModel,
|
||||||
getImageSize,
|
getImageSize,
|
||||||
toggleImageGenerationEnabledStatus,
|
getImageGenerationConfig,
|
||||||
|
updateImageGenerationConfig,
|
||||||
updateAUTOMATIC1111Url,
|
updateAUTOMATIC1111Url,
|
||||||
updateDefaultDiffusionModel,
|
|
||||||
updateImageSize,
|
updateImageSize,
|
||||||
getImageSteps,
|
getImageSteps,
|
||||||
updateImageSteps
|
updateImageSteps
|
||||||
|
@ -23,7 +23,9 @@
|
||||||
|
|
||||||
let loading = false;
|
let loading = false;
|
||||||
|
|
||||||
|
let imageGenerationEngine = '';
|
||||||
let enableImageGeneration = false;
|
let enableImageGeneration = false;
|
||||||
|
|
||||||
let AUTOMATIC1111_BASE_URL = '';
|
let AUTOMATIC1111_BASE_URL = '';
|
||||||
|
|
||||||
let selectedModel = '';
|
let selectedModel = '';
|
||||||
|
@ -33,11 +35,11 @@
|
||||||
let steps = 50;
|
let steps = 50;
|
||||||
|
|
||||||
const getModels = async () => {
|
const getModels = async () => {
|
||||||
models = await getDiffusionModels(localStorage.token).catch((error) => {
|
models = await getImageGenerationModels(localStorage.token).catch((error) => {
|
||||||
toast.error(error);
|
toast.error(error);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
selectedModel = await getDefaultDiffusionModel(localStorage.token).catch((error) => {
|
selectedModel = await getDefaultImageGenerationModel(localStorage.token).catch((error) => {
|
||||||
return '';
|
return '';
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -62,33 +64,44 @@
|
||||||
AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
|
AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const toggleImageGeneration = async () => {
|
const updateImageGeneration = async () => {
|
||||||
if (AUTOMATIC1111_BASE_URL) {
|
const res = await updateImageGenerationConfig(
|
||||||
enableImageGeneration = await toggleImageGenerationEnabledStatus(localStorage.token).catch(
|
localStorage.token,
|
||||||
(error) => {
|
imageGenerationEngine,
|
||||||
|
enableImageGeneration
|
||||||
|
).catch((error) => {
|
||||||
toast.error(error);
|
toast.error(error);
|
||||||
return false;
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
imageGenerationEngine = res.engine;
|
||||||
|
enableImageGeneration = res.enabled;
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
if (enableImageGeneration) {
|
if (enableImageGeneration) {
|
||||||
config.set(await getBackendConfig(localStorage.token));
|
config.set(await getBackendConfig(localStorage.token));
|
||||||
getModels();
|
getModels();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
enableImageGeneration = false;
|
|
||||||
toast.error('AUTOMATIC1111_BASE_URL not provided');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
if ($user.role === 'admin') {
|
if ($user.role === 'admin') {
|
||||||
enableImageGeneration = await getImageGenerationEnabledStatus(localStorage.token);
|
const res = await getImageGenerationConfig(localStorage.token).catch((error) => {
|
||||||
|
toast.error(error);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
imageGenerationEngine = res.engine;
|
||||||
|
enableImageGeneration = res.enabled;
|
||||||
|
}
|
||||||
AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
|
AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
|
||||||
|
|
||||||
if (enableImageGeneration && AUTOMATIC1111_BASE_URL) {
|
|
||||||
imageSize = await getImageSize(localStorage.token);
|
imageSize = await getImageSize(localStorage.token);
|
||||||
steps = await getImageSteps(localStorage.token);
|
steps = await getImageSteps(localStorage.token);
|
||||||
|
|
||||||
|
if (enableImageGeneration) {
|
||||||
getModels();
|
getModels();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +112,7 @@
|
||||||
class="flex flex-col h-full justify-between space-y-3 text-sm"
|
class="flex flex-col h-full justify-between space-y-3 text-sm"
|
||||||
on:submit|preventDefault={async () => {
|
on:submit|preventDefault={async () => {
|
||||||
loading = true;
|
loading = true;
|
||||||
await updateDefaultDiffusionModel(localStorage.token, selectedModel);
|
await updateDefaultImageGenerationModel(localStorage.token, selectedModel);
|
||||||
await updateImageSize(localStorage.token, imageSize).catch((error) => {
|
await updateImageSize(localStorage.token, imageSize).catch((error) => {
|
||||||
toast.error(error);
|
toast.error(error);
|
||||||
return null;
|
return null;
|
||||||
|
@ -117,6 +130,23 @@
|
||||||
<div>
|
<div>
|
||||||
<div class=" mb-1 text-sm font-medium">Image Settings</div>
|
<div class=" mb-1 text-sm font-medium">Image Settings</div>
|
||||||
|
|
||||||
|
<div class=" py-0.5 flex w-full justify-between">
|
||||||
|
<div class=" self-center text-xs font-medium">Image Generation Engine</div>
|
||||||
|
<div class="flex items-center relative">
|
||||||
|
<select
|
||||||
|
class="w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
|
||||||
|
bind:value={imageGenerationEngine}
|
||||||
|
placeholder="Select a mode"
|
||||||
|
on:change={async () => {
|
||||||
|
await updateImageGeneration();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<option value="">Default (Automatic1111)</option>
|
||||||
|
<option value="openai">Open AI (Dall-E)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<div class=" py-0.5 flex w-full justify-between">
|
<div class=" py-0.5 flex w-full justify-between">
|
||||||
<div class=" self-center text-xs font-medium">Image Generation (Experimental)</div>
|
<div class=" self-center text-xs font-medium">Image Generation (Experimental)</div>
|
||||||
|
@ -124,7 +154,12 @@
|
||||||
<button
|
<button
|
||||||
class="p-1 px-3 text-xs flex rounded transition"
|
class="p-1 px-3 text-xs flex rounded transition"
|
||||||
on:click={() => {
|
on:click={() => {
|
||||||
toggleImageGeneration();
|
if (imageGenerationEngine === '' && AUTOMATIC1111_BASE_URL === '') {
|
||||||
|
toast.error('AUTOMATIC1111 Base URL is required.');
|
||||||
|
} else {
|
||||||
|
enableImageGeneration = !enableImageGeneration;
|
||||||
|
updateImageGeneration();
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
|
@ -137,6 +172,8 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if imageGenerationEngine === ''}
|
||||||
<hr class=" dark:border-gray-700" />
|
<hr class=" dark:border-gray-700" />
|
||||||
|
|
||||||
<div class=" mb-2.5 text-sm font-medium">AUTOMATIC1111 Base URL</div>
|
<div class=" mb-2.5 text-sm font-medium">AUTOMATIC1111 Base URL</div>
|
||||||
|
@ -182,6 +219,7 @@
|
||||||
(e.g. `sh webui.sh --api`)
|
(e.g. `sh webui.sh --api`)
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
{#if enableImageGeneration}
|
{#if enableImageGeneration}
|
||||||
<hr class=" dark:border-gray-700" />
|
<hr class=" dark:border-gray-700" />
|
||||||
|
@ -199,9 +237,7 @@
|
||||||
<option value="" disabled selected>Select a model</option>
|
<option value="" disabled selected>Select a model</option>
|
||||||
{/if}
|
{/if}
|
||||||
{#each models ?? [] as model}
|
{#each models ?? [] as model}
|
||||||
<option value={model.title} class="bg-gray-100 dark:bg-gray-700"
|
<option value={model.id} class="bg-gray-100 dark:bg-gray-700">{model.name}</option>
|
||||||
>{model.model_name}</option
|
|
||||||
>
|
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue