From 3fa79e59bf4849549ab305e41afa7c8135e33041 Mon Sep 17 00:00:00 2001 From: Justin Firth Date: Sat, 24 Feb 2024 10:44:08 -0500 Subject: [PATCH 1/5] Allow configuration of steps, default to a1111 default --- backend/apps/images/main.py | 29 ++++++++++++++++ src/lib/apis/images/index.ts | 33 +++++++++++++++++++ .../components/chat/Settings/Images.svelte | 21 +++++++++++- 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/backend/apps/images/main.py b/backend/apps/images/main.py index 39d3f96a..08c9a083 100644 --- a/backend/apps/images/main.py +++ b/backend/apps/images/main.py @@ -100,6 +100,32 @@ async def update_image_size( status_code=400, detail=ERROR_MESSAGES.INCORRECT_FORMAT(" (e.g., 512x512)."), ) + + +class ImageStepsUpdateForm(BaseModel): + steps: int + + +@app.get("/steps") +async def get_image_size(user=Depends(get_admin_user)): + return {"IMAGE_STEPS": app.state.IMAGE_STEPS} + + +@app.post("/steps/update") +async def update_image_size( + form_data: ImageStepsUpdateForm, user=Depends(get_admin_user) +): + if form_data.steps >= 0: + app.state.IMAGE_STEPS = form_data.steps + return { + "IMAGE_STEPS": app.state.IMAGE_STEPS, + "status": True, + } + else: + raise HTTPException( + status_code=400, + detail=ERROR_MESSAGES.INCORRECT_FORMAT(" (e.g., 50)."), + ) @app.get("/models") @@ -177,6 +203,9 @@ def generate_image( "height": height, } + if app.state.IMAGE_STEPS != None: + data["steps"] = app.state.IMAGE_STEPS + if form_data.negative_prompt != None: data["negative_prompt"] = form_data.negative_prompt diff --git a/src/lib/apis/images/index.ts b/src/lib/apis/images/index.ts index 205ee90a..c64bf2ba 100644 --- a/src/lib/apis/images/index.ts +++ b/src/lib/apis/images/index.ts @@ -198,6 +198,39 @@ export const updateImageSize = async (token: string = '', size: string) => { return res.IMAGE_SIZE; }; +export const updateImageSteps = async (token: string = '', steps: number) => { + let error = null; + + const res = await fetch(`${IMAGES_API_BASE_URL}/steps/update`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + }, + body: JSON.stringify({ steps }) + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + if ('detail' in err) { + error = err.detail; + } else { + error = 'Server connection failed'; + } + return null; + }); + + if (error) { + throw error; + } + + return res.IMAGE_STEPS; +}; + export const getDiffusionModels = async (token: string = '') => { let error = null; diff --git a/src/lib/components/chat/Settings/Images.svelte b/src/lib/components/chat/Settings/Images.svelte index 94cb3213..2f6daa10 100644 --- a/src/lib/components/chat/Settings/Images.svelte +++ b/src/lib/components/chat/Settings/Images.svelte @@ -12,7 +12,8 @@ toggleImageGenerationEnabledStatus, updateAUTOMATIC1111Url, updateDefaultDiffusionModel, - updateImageSize + updateImageSize, + updateImageSteps } from '$lib/apis/images'; import { getBackendConfig } from '$lib/apis'; const dispatch = createEventDispatcher(); @@ -28,6 +29,7 @@ let models = []; let imageSize = ''; + let steps = 50; const getModels = async () => { models = await getDiffusionModels(localStorage.token).catch((error) => { @@ -98,6 +100,10 @@ toast.error(error); return null; }); + await updateImageSteps(localStorage.token, steps).catch((error) => { + toast.error(error); + return null; + }); dispatch('save'); loading = false; @@ -210,6 +216,19 @@ + +
+
Set Steps
+
+
+ +
+
+
{/if} From af1675813f099e94f8dfb3d3c95f5d1fa8eb243b Mon Sep 17 00:00:00 2001 From: Justin Firth Date: Sat, 24 Feb 2024 11:34:11 -0500 Subject: [PATCH 2/5] Restore image steps setting when Settings modal is presented --- src/lib/apis/images/index.ts | 32 +++++++++++++++++++ .../components/chat/Settings/Images.svelte | 2 ++ 2 files changed, 34 insertions(+) diff --git a/src/lib/apis/images/index.ts b/src/lib/apis/images/index.ts index c64bf2ba..f05ce0b7 100644 --- a/src/lib/apis/images/index.ts +++ b/src/lib/apis/images/index.ts @@ -198,6 +198,38 @@ export const updateImageSize = async (token: string = '', size: string) => { return res.IMAGE_SIZE; }; +export const getImageSteps = async (token: string = '') => { + let error = null; + + const res = await fetch(`${IMAGES_API_BASE_URL}/steps`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + if ('detail' in err) { + error = err.detail; + } else { + error = 'Server connection failed'; + } + return null; + }); + + if (error) { + throw error; + } + + return res.IMAGE_STEPS; +}; + export const updateImageSteps = async (token: string = '', steps: number) => { let error = null; diff --git a/src/lib/components/chat/Settings/Images.svelte b/src/lib/components/chat/Settings/Images.svelte index 2f6daa10..6e349861 100644 --- a/src/lib/components/chat/Settings/Images.svelte +++ b/src/lib/components/chat/Settings/Images.svelte @@ -13,6 +13,7 @@ updateAUTOMATIC1111Url, updateDefaultDiffusionModel, updateImageSize, + getImageSteps, updateImageSteps } from '$lib/apis/images'; import { getBackendConfig } from '$lib/apis'; @@ -85,6 +86,7 @@ if (enableImageGeneration && AUTOMATIC1111_BASE_URL) { imageSize = await getImageSize(localStorage.token); + steps = await getImageSteps(localStorage.token); getModels(); } } From 393531c9b036a24221c15805756df4c06636d1a2 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 24 Feb 2024 18:08:35 -0800 Subject: [PATCH 3/5] fix --- backend/apps/images/main.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/backend/apps/images/main.py b/backend/apps/images/main.py index 08c9a083..fa7f2c7a 100644 --- a/backend/apps/images/main.py +++ b/backend/apps/images/main.py @@ -35,6 +35,7 @@ app.add_middleware( app.state.AUTOMATIC1111_BASE_URL = AUTOMATIC1111_BASE_URL app.state.ENABLED = app.state.AUTOMATIC1111_BASE_URL != "" app.state.IMAGE_SIZE = "512x512" +app.state.IMAGE_STEPS = 50 @app.get("/enabled", response_model=bool) @@ -49,7 +50,7 @@ async def toggle_enabled(request: Request, user=Depends(get_admin_user)): app.state.ENABLED = not app.state.ENABLED return app.state.ENABLED except Exception as e: - raise HTTPException(status_code=r.status_code, detail=ERROR_MESSAGES.DEFAULT(e)) + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e)) class UrlUpdateForm(BaseModel): @@ -100,7 +101,7 @@ async def update_image_size( status_code=400, detail=ERROR_MESSAGES.INCORRECT_FORMAT(" (e.g., 512x512)."), ) - + class ImageStepsUpdateForm(BaseModel): steps: int @@ -135,7 +136,7 @@ def get_models(user=Depends(get_current_user)): models = r.json() return models except Exception as e: - raise HTTPException(status_code=r.status_code, detail=ERROR_MESSAGES.DEFAULT(e)) + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e)) @app.get("/models/default") @@ -146,7 +147,7 @@ async def get_default_model(user=Depends(get_admin_user)): return {"model": options["sd_model_checkpoint"]} except Exception as e: - raise HTTPException(status_code=r.status_code, detail=ERROR_MESSAGES.DEFAULT(e)) + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e)) class UpdateModelForm(BaseModel): @@ -219,4 +220,4 @@ def generate_image( return r.json() except Exception as e: print(e) - raise HTTPException(status_code=r.status_code, detail=ERROR_MESSAGES.DEFAULT(e)) + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e)) From 0f8a74d52dc662e25c122cc0581e20e87adad32f Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 24 Feb 2024 18:10:13 -0800 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a35d723..eaf77aca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Built-in LiteLLM Proxy**: Open WebUI now ships with LiteLLM Proxy. - **Image Generation Enhancements**: Advanced Settings + Image Preview Feature. + - Allows setting number of steps for image generation; defaults to a1111 default value. ### Fixed From 41a6bf113a0407c3192612003e9898203a98adce Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 24 Feb 2024 18:11:03 -0800 Subject: [PATCH 5/5] fix: default image generation value --- src/lib/components/chat/Settings/Images.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/chat/Settings/Images.svelte b/src/lib/components/chat/Settings/Images.svelte index 66d7d9e2..56b2d6d2 100644 --- a/src/lib/components/chat/Settings/Images.svelte +++ b/src/lib/components/chat/Settings/Images.svelte @@ -23,7 +23,7 @@ let loading = false; - let enableImageGeneration = true; + let enableImageGeneration = false; let AUTOMATIC1111_BASE_URL = ''; let selectedModel = '';