Skip to content

Commit 0ad412a

Browse files
committed
allow environment variables to be changed
1 parent f4f8685 commit 0ad412a

File tree

8 files changed

+49
-9
lines changed

8 files changed

+49
-9
lines changed

i18n/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ export const en: LangConfig = {
7979
'webui/config/embeddings-dir': 'Embeddings directory',
8080
'webui/config/xformers': 'Enable xformers',
8181
'webui/config/custom': 'Custom arguments',
82+
'webui/config/env': 'Environment variables',
8283
}

i18n/ja.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ export const ja: LangConfig = {
7878
'webui/config/embeddings-dir': 'Embeddingsディレクトリ',
7979
'webui/config/xformers': 'xformersを有効にする',
8080
'webui/config/custom': 'コマンドライン引数',
81+
'webui/config/env': "環境変数"
8182
}

i18n/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const LangKeys = [
7272
'webui/config/hypernetwork-dir',
7373
'webui/config/xformers',
7474
'webui/config/custom',
75+
'webui/config/env',
7576
] as const
7677
export type LangKeys = (typeof LangKeys)[number]
7778
export type LangConfig = Record<LangKeys, string>

src/features/stable-diffusion-webui/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class StableDiffusionWebUI {
5151
)
5252

5353
this.ipc.handle('webui/running', () => this.ps !== null)
54-
this.ipc.handle('webui/launch', (_, args, commit) => this.launch(args, commit))
54+
this.ipc.handle('webui/launch', (_, args, env, commit) => this.launch(args, env, commit))
5555
this.ipc.handle('webui/stop', this.stop.bind(this))
5656
this.ipc.handle('webui/logs', () => this.logs)
5757
this.ipc.handle('webui/port', () => this.port)
@@ -125,7 +125,7 @@ class StableDiffusionWebUI {
125125
this.ipc.emit('log', 'Installation finished🎉')
126126
}
127127

128-
public async launch(args: string, commit = 'master') {
128+
public async launch(args: string, env: Record<string, any>, commit = 'master') {
129129
if (this.ps) throw new Error('already running.')
130130
const git = this.git()
131131
try {
@@ -139,6 +139,7 @@ class StableDiffusionWebUI {
139139
cwd: path.join(this.dir, 'repository'),
140140
env: {
141141
COMMANDLINE_ARGS: args,
142+
...env,
142143
},
143144
})
144145
this.ps.stdout?.on('data', (data: Buffer) => {

src/features/stable-diffusion-webui/ipc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface ClientToServerEvents {
2121
'folder/open': () => void
2222
'webui/running': () => boolean
2323
'webui/logs': () => string[]
24-
'webui/launch': (args: string, commit: string) => number
24+
'webui/launch': (args: string, env: Record<string, any>, commit: string) => number
2525
'webui/stop': () => void
2626
'webui/port': () => number
2727
'webui/data-dir': () => string

src/web/lib/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type Config = {
1515
'webui/args/hypernetwork-dir': string
1616
'webui/args/xformers': boolean
1717
'webui/args/custom': string
18+
'webui/settings/env': string
1819
}
1920

2021
const defaultConfig: Config = {
@@ -42,6 +43,7 @@ const defaultConfig: Config = {
4243
),
4344
'webui/args/xformers': true,
4445
'webui/args/custom': '',
46+
'webui/settings/env': '',
4547
}
4648

4749
const merge = (obj: any) => {

src/web/pages/webui/launcher.tsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ const StyledIconButton = styled(IconButton)`
3333
font-size: 1.5rem;
3434
`
3535

36+
const extractString = (str: string) => {
37+
const regex = /(['"])((?:\\\1|.)*?)\1/
38+
const match = str.match(regex)
39+
if (match) {
40+
return match[2]
41+
}
42+
return str
43+
}
44+
3645
export const createArgs = () => {
3746
let result = ''
3847
for (const [key, val] of Object.entries(config)) {
@@ -47,11 +56,22 @@ export const createArgs = () => {
4756
return result
4857
}
4958

59+
export const createEnv = () => {
60+
const result: Record<string, any> = {}
61+
const envs = config['webui/settings/env'].split(',')
62+
for (const v of envs) {
63+
if (!v.includes('=') || v.split('=').length < 2) continue
64+
const [key, value] = v.split('=')
65+
result[key!] = extractString(value!)
66+
}
67+
return result
68+
}
69+
5070
export const Launcher: Component = () => {
5171
const theme = useTheme()
5272
const toast = useToast()
5373
const [t] = useI18n()
54-
const { setUrl, onLaunch } = useContext(WebUIContext)
74+
const { url, setUrl, onLaunch } = useContext(WebUIContext)
5575
const [gitLog, setGitLog] = createSignal<LogResult<DefaultLogFields>['all']>([])
5676
const [logs, setLogs] = createSignal<string[]>([])
5777
const [installed, setInstalled] = createSignal(true)
@@ -63,16 +83,18 @@ export const Launcher: Component = () => {
6383
setLogs((prev) => [...prev, ...logs])
6484

6585
const re = /Running on local URL: (.*)/
66-
const url = logs.map((v) => v.match(re)).filter(Boolean)
67-
if (url.length > 0) {
86+
const urls = logs.map((v) => v.match(re)).filter(Boolean)
87+
if (urls.length > 0) {
6888
toast({
6989
title: t('webui/launcher/launched/title'),
7090
status: 'success',
7191
duration: 3000,
7292
isClosable: true,
7393
})
74-
const matches = url[0] as RegExpMatchArray
75-
setTimeout(() => onLaunch(matches[1] as string), 500)
94+
const matches = urls[0] as RegExpMatchArray
95+
const newUrl = matches[1] as string
96+
if (newUrl == url()) return
97+
setTimeout(() => onLaunch(newUrl), 500)
7698
}
7799
}
78100

@@ -211,7 +233,12 @@ export const Launcher: Component = () => {
211233
onClick={() => {
212234
setRunning(true)
213235
setLogs([])
214-
ipc.webui.invoke('webui/launch', createArgs(), config['webui/git/commit'].slice(0, 6))
236+
ipc.webui.invoke(
237+
'webui/launch',
238+
createArgs(),
239+
createEnv(),
240+
config['webui/git/commit'].slice(0, 6),
241+
)
215242
ipc.webui.once('webui/close', () => {
216243
setUrl('')
217244
setRunning(false)

src/web/pages/webui/settings.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ export const Settings: Component = () => {
118118
/>
119119
<br />
120120

121+
<Label>{t('webui/config/env')}</Label>
122+
<Input
123+
value={config['webui/settings/env']}
124+
onInput={(e) => setConfig('webui/settings/env', e.currentTarget.value)}
125+
/>
126+
<br />
127+
121128
<HStack>
122129
<Button
123130
onClick={() => {

0 commit comments

Comments
 (0)