feat: editable openai url for images

This commit is contained in:
Timothy J. Baek 2024-04-23 07:14:31 -04:00
parent aa489be53b
commit 25d09363df
4 changed files with 48 additions and 27 deletions

View file

@ -35,8 +35,8 @@ from config import (
ENABLE_IMAGE_GENERATION, ENABLE_IMAGE_GENERATION,
AUTOMATIC1111_BASE_URL, AUTOMATIC1111_BASE_URL,
COMFYUI_BASE_URL, COMFYUI_BASE_URL,
IMAGE_OPENAI_API_BASE_URL, IMAGES_OPENAI_API_BASE_URL,
IMAGE_OPENAI_API_KEY, IMAGES_OPENAI_API_KEY,
) )
@ -58,8 +58,8 @@ app.add_middleware(
app.state.ENGINE = "" app.state.ENGINE = ""
app.state.ENABLED = ENABLE_IMAGE_GENERATION app.state.ENABLED = ENABLE_IMAGE_GENERATION
app.state.OPENAI_API_BASE_URL = IMAGE_OPENAI_API_BASE_URL app.state.OPENAI_API_BASE_URL = IMAGES_OPENAI_API_BASE_URL
app.state.OPENAI_API_KEY = IMAGE_OPENAI_API_KEY app.state.OPENAI_API_KEY = IMAGES_OPENAI_API_KEY
app.state.MODEL = "" app.state.MODEL = ""
@ -135,27 +135,33 @@ async def update_engine_url(
} }
class OpenAIKeyUpdateForm(BaseModel): class OpenAIConfigUpdateForm(BaseModel):
url: str
key: str key: str
@app.get("/key") @app.get("/openai/config")
async def get_openai_key(user=Depends(get_admin_user)): async def get_openai_config(user=Depends(get_admin_user)):
return {"OPENAI_API_KEY": app.state.OPENAI_API_KEY} return {
"OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL,
"OPENAI_API_KEY": app.state.OPENAI_API_KEY,
}
@app.post("/key/update") @app.post("/openai/config/update")
async def update_openai_key( async def update_openai_config(
form_data: OpenAIKeyUpdateForm, user=Depends(get_admin_user) form_data: OpenAIConfigUpdateForm, user=Depends(get_admin_user)
): ):
if form_data.key == "": if form_data.key == "":
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.API_KEY_NOT_FOUND) raise HTTPException(status_code=400, detail=ERROR_MESSAGES.API_KEY_NOT_FOUND)
app.state.OPENAI_API_BASE_URL = form_data.url
app.state.OPENAI_API_KEY = form_data.key app.state.OPENAI_API_KEY = form_data.key
return { return {
"OPENAI_API_KEY": app.state.OPENAI_API_KEY,
"status": True, "status": True,
"OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL,
"OPENAI_API_KEY": app.state.OPENAI_API_KEY,
} }

View file

@ -72,10 +72,10 @@ export const updateImageGenerationConfig = async (
return res; return res;
}; };
export const getOpenAIKey = async (token: string = '') => { export const getOpenAIConfig = async (token: string = '') => {
let error = null; let error = null;
const res = await fetch(`${IMAGES_API_BASE_URL}/key`, { const res = await fetch(`${IMAGES_API_BASE_URL}/openai/config`, {
method: 'GET', method: 'GET',
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
@ -101,13 +101,13 @@ export const getOpenAIKey = async (token: string = '') => {
throw error; throw error;
} }
return res.OPENAI_API_KEY; return res;
}; };
export const updateOpenAIKey = async (token: string = '', key: string) => { export const updateOpenAIConfig = async (token: string = '', url: string, key: string) => {
let error = null; let error = null;
const res = await fetch(`${IMAGES_API_BASE_URL}/key/update`, { const res = await fetch(`${IMAGES_API_BASE_URL}/openai/config/update`, {
method: 'POST', method: 'POST',
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
@ -115,6 +115,7 @@ export const updateOpenAIKey = async (token: string = '', key: string) => {
...(token && { authorization: `Bearer ${token}` }) ...(token && { authorization: `Bearer ${token}` })
}, },
body: JSON.stringify({ body: JSON.stringify({
url: url,
key: key key: key
}) })
}) })
@ -136,7 +137,7 @@ export const updateOpenAIKey = async (token: string = '', key: string) => {
throw error; throw error;
} }
return res.OPENAI_API_KEY; return res;
}; };
export const getImageGenerationEngineUrls = async (token: string = '') => { export const getImageGenerationEngineUrls = async (token: string = '') => {

View file

@ -15,8 +15,8 @@
updateImageSize, updateImageSize,
getImageSteps, getImageSteps,
updateImageSteps, updateImageSteps,
getOpenAIKey, getOpenAIConfig,
updateOpenAIKey updateOpenAIConfig
} from '$lib/apis/images'; } from '$lib/apis/images';
import { getBackendConfig } from '$lib/apis'; import { getBackendConfig } from '$lib/apis';
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
@ -33,6 +33,7 @@
let AUTOMATIC1111_BASE_URL = ''; let AUTOMATIC1111_BASE_URL = '';
let COMFYUI_BASE_URL = ''; let COMFYUI_BASE_URL = '';
let OPENAI_API_BASE_URL = '';
let OPENAI_API_KEY = ''; let OPENAI_API_KEY = '';
let selectedModel = ''; let selectedModel = '';
@ -131,7 +132,10 @@
AUTOMATIC1111_BASE_URL = URLS.AUTOMATIC1111_BASE_URL; AUTOMATIC1111_BASE_URL = URLS.AUTOMATIC1111_BASE_URL;
COMFYUI_BASE_URL = URLS.COMFYUI_BASE_URL; COMFYUI_BASE_URL = URLS.COMFYUI_BASE_URL;
OPENAI_API_KEY = await getOpenAIKey(localStorage.token); const config = await getOpenAIConfig(localStorage.token);
OPENAI_API_KEY = config.OPENAI_API_KEY;
OPENAI_API_BASE_URL = config.OPENAI_API_BASE_URL;
imageSize = await getImageSize(localStorage.token); imageSize = await getImageSize(localStorage.token);
steps = await getImageSteps(localStorage.token); steps = await getImageSteps(localStorage.token);
@ -149,7 +153,7 @@
loading = true; loading = true;
if (imageGenerationEngine === 'openai') { if (imageGenerationEngine === 'openai') {
await updateOpenAIKey(localStorage.token, OPENAI_API_KEY); await updateOpenAIConfig(localStorage.token, OPENAI_API_BASE_URL, OPENAI_API_KEY);
} }
await updateDefaultImageGenerationModel(localStorage.token, selectedModel); await updateDefaultImageGenerationModel(localStorage.token, selectedModel);
@ -300,13 +304,22 @@
</button> </button>
</div> </div>
{:else if imageGenerationEngine === 'openai'} {:else if imageGenerationEngine === 'openai'}
<div class=" mb-2.5 text-sm font-medium">{$i18n.t('OpenAI API Key')}</div> <div>
<div class="flex w-full"> <div class=" mb-1.5 text-sm font-medium">{$i18n.t('OpenAI API Config')}</div>
<div class="flex-1 mr-2">
<div class="flex gap-2 mb-1">
<input <input
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none" class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
placeholder={$i18n.t('Enter API Key')} placeholder={$i18n.t('API Base URL')}
bind:value={OPENAI_API_BASE_URL}
required
/>
<input
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
placeholder={$i18n.t('API Key')}
bind:value={OPENAI_API_KEY} bind:value={OPENAI_API_KEY}
required
/> />
</div> </div>
</div> </div>

1
static/manifest.json Normal file
View file

@ -0,0 +1 @@
{}