Images.svelte 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { createEventDispatcher, onMount, getContext } from 'svelte';
  4. import { config, user } from '$lib/stores';
  5. import {
  6. getImageGenerationModels,
  7. getDefaultImageGenerationModel,
  8. updateDefaultImageGenerationModel,
  9. getImageSize,
  10. getImageGenerationConfig,
  11. updateImageGenerationConfig,
  12. getImageGenerationEngineUrls,
  13. updateImageGenerationEngineUrls,
  14. updateImageSize,
  15. getImageSteps,
  16. updateImageSteps,
  17. getOpenAIConfig,
  18. updateOpenAIConfig
  19. } from '$lib/apis/images';
  20. import { getBackendConfig } from '$lib/apis';
  21. const dispatch = createEventDispatcher();
  22. const i18n = getContext('i18n');
  23. export let saveSettings: Function;
  24. let loading = false;
  25. let imageGenerationEngine = '';
  26. let enableImageGeneration = false;
  27. let AUTOMATIC1111_BASE_URL = '';
  28. let COMFYUI_BASE_URL = '';
  29. let OPENAI_API_BASE_URL = '';
  30. let OPENAI_API_KEY = '';
  31. let selectedModel = '';
  32. let models = null;
  33. let imageSize = '';
  34. let steps = 50;
  35. const getModels = async () => {
  36. models = await getImageGenerationModels(localStorage.token).catch((error) => {
  37. toast.error(error);
  38. return null;
  39. });
  40. selectedModel = await getDefaultImageGenerationModel(localStorage.token).catch((error) => {
  41. return '';
  42. });
  43. };
  44. const updateUrlHandler = async () => {
  45. if (imageGenerationEngine === 'comfyui') {
  46. const res = await updateImageGenerationEngineUrls(localStorage.token, {
  47. COMFYUI_BASE_URL: COMFYUI_BASE_URL
  48. }).catch((error) => {
  49. toast.error(error);
  50. console.log(error);
  51. return null;
  52. });
  53. if (res) {
  54. COMFYUI_BASE_URL = res.COMFYUI_BASE_URL;
  55. await getModels();
  56. if (models) {
  57. toast.success($i18n.t('Server connection verified'));
  58. }
  59. } else {
  60. ({ COMFYUI_BASE_URL } = await getImageGenerationEngineUrls(localStorage.token));
  61. }
  62. } else {
  63. const res = await updateImageGenerationEngineUrls(localStorage.token, {
  64. AUTOMATIC1111_BASE_URL: AUTOMATIC1111_BASE_URL
  65. }).catch((error) => {
  66. toast.error(error);
  67. return null;
  68. });
  69. if (res) {
  70. AUTOMATIC1111_BASE_URL = res.AUTOMATIC1111_BASE_URL;
  71. await getModels();
  72. if (models) {
  73. toast.success($i18n.t('Server connection verified'));
  74. }
  75. } else {
  76. ({ AUTOMATIC1111_BASE_URL } = await getImageGenerationEngineUrls(localStorage.token));
  77. }
  78. }
  79. };
  80. const updateImageGeneration = async () => {
  81. const res = await updateImageGenerationConfig(
  82. localStorage.token,
  83. imageGenerationEngine,
  84. enableImageGeneration
  85. ).catch((error) => {
  86. toast.error(error);
  87. return null;
  88. });
  89. if (res) {
  90. imageGenerationEngine = res.engine;
  91. enableImageGeneration = res.enabled;
  92. }
  93. if (enableImageGeneration) {
  94. config.set(await getBackendConfig(localStorage.token));
  95. getModels();
  96. }
  97. };
  98. onMount(async () => {
  99. if ($user.role === 'admin') {
  100. const res = await getImageGenerationConfig(localStorage.token).catch((error) => {
  101. toast.error(error);
  102. return null;
  103. });
  104. if (res) {
  105. imageGenerationEngine = res.engine;
  106. enableImageGeneration = res.enabled;
  107. }
  108. const URLS = await getImageGenerationEngineUrls(localStorage.token);
  109. AUTOMATIC1111_BASE_URL = URLS.AUTOMATIC1111_BASE_URL;
  110. COMFYUI_BASE_URL = URLS.COMFYUI_BASE_URL;
  111. const config = await getOpenAIConfig(localStorage.token);
  112. OPENAI_API_KEY = config.OPENAI_API_KEY;
  113. OPENAI_API_BASE_URL = config.OPENAI_API_BASE_URL;
  114. imageSize = await getImageSize(localStorage.token);
  115. steps = await getImageSteps(localStorage.token);
  116. if (enableImageGeneration) {
  117. getModels();
  118. }
  119. }
  120. });
  121. </script>
  122. <form
  123. class="flex flex-col h-full justify-between space-y-3 text-sm"
  124. on:submit|preventDefault={async () => {
  125. loading = true;
  126. if (imageGenerationEngine === 'openai') {
  127. await updateOpenAIConfig(localStorage.token, OPENAI_API_BASE_URL, OPENAI_API_KEY);
  128. }
  129. await updateDefaultImageGenerationModel(localStorage.token, selectedModel);
  130. await updateImageSize(localStorage.token, imageSize).catch((error) => {
  131. toast.error(error);
  132. return null;
  133. });
  134. await updateImageSteps(localStorage.token, steps).catch((error) => {
  135. toast.error(error);
  136. return null;
  137. });
  138. dispatch('save');
  139. loading = false;
  140. }}
  141. >
  142. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[24rem]">
  143. <div>
  144. <div class=" mb-1 text-sm font-medium">{$i18n.t('Image Settings')}</div>
  145. <div class=" py-0.5 flex w-full justify-between">
  146. <div class=" self-center text-xs font-medium">{$i18n.t('Image Generation Engine')}</div>
  147. <div class="flex items-center relative">
  148. <select
  149. class="w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  150. bind:value={imageGenerationEngine}
  151. placeholder={$i18n.t('Select a mode')}
  152. on:change={async () => {
  153. await updateImageGeneration();
  154. }}
  155. >
  156. <option value="">{$i18n.t('Default (Automatic1111)')}</option>
  157. <option value="comfyui">{$i18n.t('ComfyUI')}</option>
  158. <option value="openai">{$i18n.t('Open AI (Dall-E)')}</option>
  159. </select>
  160. </div>
  161. </div>
  162. <div>
  163. <div class=" py-0.5 flex w-full justify-between">
  164. <div class=" self-center text-xs font-medium">
  165. {$i18n.t('Image Generation (Experimental)')}
  166. </div>
  167. <button
  168. class="p-1 px-3 text-xs flex rounded transition"
  169. on:click={() => {
  170. if (imageGenerationEngine === '' && AUTOMATIC1111_BASE_URL === '') {
  171. toast.error($i18n.t('AUTOMATIC1111 Base URL is required.'));
  172. enableImageGeneration = false;
  173. } else if (imageGenerationEngine === 'comfyui' && COMFYUI_BASE_URL === '') {
  174. toast.error($i18n.t('ComfyUI Base URL is required.'));
  175. enableImageGeneration = false;
  176. } else if (imageGenerationEngine === 'openai' && OPENAI_API_KEY === '') {
  177. toast.error($i18n.t('OpenAI API Key is required.'));
  178. enableImageGeneration = false;
  179. } else {
  180. enableImageGeneration = !enableImageGeneration;
  181. }
  182. updateImageGeneration();
  183. }}
  184. type="button"
  185. >
  186. {#if enableImageGeneration === true}
  187. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  188. {:else}
  189. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  190. {/if}
  191. </button>
  192. </div>
  193. </div>
  194. </div>
  195. <hr class=" dark:border-gray-700" />
  196. {#if imageGenerationEngine === ''}
  197. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('AUTOMATIC1111 Base URL')}</div>
  198. <div class="flex w-full">
  199. <div class="flex-1 mr-2">
  200. <input
  201. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  202. placeholder={$i18n.t('Enter URL (e.g. http://127.0.0.1:7860/)')}
  203. bind:value={AUTOMATIC1111_BASE_URL}
  204. />
  205. </div>
  206. <button
  207. class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
  208. type="button"
  209. on:click={() => {
  210. updateUrlHandler();
  211. }}
  212. >
  213. <svg
  214. xmlns="http://www.w3.org/2000/svg"
  215. viewBox="0 0 20 20"
  216. fill="currentColor"
  217. class="w-4 h-4"
  218. >
  219. <path
  220. fill-rule="evenodd"
  221. d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
  222. clip-rule="evenodd"
  223. />
  224. </svg>
  225. </button>
  226. </div>
  227. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  228. {$i18n.t('Include `--api` flag when running stable-diffusion-webui')}
  229. <a
  230. class=" text-gray-300 font-medium"
  231. href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/3734"
  232. target="_blank"
  233. >
  234. {$i18n.t('(e.g. `sh webui.sh --api`)')}
  235. </a>
  236. </div>
  237. {:else if imageGenerationEngine === 'comfyui'}
  238. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('ComfyUI Base URL')}</div>
  239. <div class="flex w-full">
  240. <div class="flex-1 mr-2">
  241. <input
  242. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  243. placeholder={$i18n.t('Enter URL (e.g. http://127.0.0.1:7860/)')}
  244. bind:value={COMFYUI_BASE_URL}
  245. />
  246. </div>
  247. <button
  248. class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
  249. type="button"
  250. on:click={() => {
  251. updateUrlHandler();
  252. }}
  253. >
  254. <svg
  255. xmlns="http://www.w3.org/2000/svg"
  256. viewBox="0 0 20 20"
  257. fill="currentColor"
  258. class="w-4 h-4"
  259. >
  260. <path
  261. fill-rule="evenodd"
  262. d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
  263. clip-rule="evenodd"
  264. />
  265. </svg>
  266. </button>
  267. </div>
  268. {:else if imageGenerationEngine === 'openai'}
  269. <div>
  270. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('OpenAI API Config')}</div>
  271. <div class="flex gap-2 mb-1">
  272. <input
  273. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  274. placeholder={$i18n.t('API Base URL')}
  275. bind:value={OPENAI_API_BASE_URL}
  276. required
  277. />
  278. <input
  279. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  280. placeholder={$i18n.t('API Key')}
  281. bind:value={OPENAI_API_KEY}
  282. required
  283. />
  284. </div>
  285. </div>
  286. {/if}
  287. {#if enableImageGeneration}
  288. <hr class=" dark:border-gray-700" />
  289. <div>
  290. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Default Model')}</div>
  291. <div class="flex w-full">
  292. <div class="flex-1 mr-2">
  293. {#if imageGenerationEngine === 'openai' && !OPENAI_API_BASE_URL.includes('https://api.openai.com')}
  294. <div class="flex w-full">
  295. <div class="flex-1">
  296. <input
  297. list="model-list"
  298. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  299. bind:value={selectedModel}
  300. placeholder="Select a model"
  301. />
  302. <datalist id="model-list">
  303. {#each models ?? [] as model}
  304. <option value={model.id}>{model.name}</option>
  305. {/each}
  306. </datalist>
  307. </div>
  308. </div>
  309. {:else}
  310. <select
  311. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  312. bind:value={selectedModel}
  313. placeholder={$i18n.t('Select a model')}
  314. required
  315. >
  316. {#if !selectedModel}
  317. <option value="" disabled selected>{$i18n.t('Select a model')}</option>
  318. {/if}
  319. {#each models ?? [] as model}
  320. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">{model.name}</option
  321. >
  322. {/each}
  323. </select>
  324. {/if}
  325. </div>
  326. </div>
  327. </div>
  328. <div>
  329. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Image Size')}</div>
  330. <div class="flex w-full">
  331. <div class="flex-1 mr-2">
  332. <input
  333. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  334. placeholder={$i18n.t('Enter Image Size (e.g. 512x512)')}
  335. bind:value={imageSize}
  336. />
  337. </div>
  338. </div>
  339. </div>
  340. <div>
  341. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Steps')}</div>
  342. <div class="flex w-full">
  343. <div class="flex-1 mr-2">
  344. <input
  345. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  346. placeholder={$i18n.t('Enter Number of Steps (e.g. 50)')}
  347. bind:value={steps}
  348. />
  349. </div>
  350. </div>
  351. </div>
  352. {/if}
  353. </div>
  354. <div class="flex justify-end pt-3 text-sm font-medium">
  355. <button
  356. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg flex flex-row space-x-1 items-center {loading
  357. ? ' cursor-not-allowed'
  358. : ''}"
  359. type="submit"
  360. disabled={loading}
  361. >
  362. {$i18n.t('Save')}
  363. {#if loading}
  364. <div class="ml-2 self-center">
  365. <svg
  366. class=" w-4 h-4"
  367. viewBox="0 0 24 24"
  368. fill="currentColor"
  369. xmlns="http://www.w3.org/2000/svg"
  370. ><style>
  371. .spinner_ajPY {
  372. transform-origin: center;
  373. animation: spinner_AtaB 0.75s infinite linear;
  374. }
  375. @keyframes spinner_AtaB {
  376. 100% {
  377. transform: rotate(360deg);
  378. }
  379. }
  380. </style><path
  381. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  382. opacity=".25"
  383. /><path
  384. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  385. class="spinner_ajPY"
  386. /></svg
  387. >
  388. </div>
  389. {/if}
  390. </button>
  391. </div>
  392. </form>