Images.svelte 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. let loading = false;
  24. let imageGenerationEngine = '';
  25. let enableImageGeneration = false;
  26. let AUTOMATIC1111_BASE_URL = '';
  27. let AUTOMATIC1111_API_AUTH = '';
  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. AUTOMATIC1111_API_AUTH: AUTOMATIC1111_API_AUTH
  66. }).catch((error) => {
  67. toast.error(error);
  68. return null;
  69. });
  70. if (res) {
  71. AUTOMATIC1111_BASE_URL = res.AUTOMATIC1111_BASE_URL;
  72. AUTOMATIC1111_API_AUTH = res.AUTOMATIC1111_API_AUTH;
  73. await getModels();
  74. if (models) {
  75. toast.success($i18n.t('Server connection verified'));
  76. }
  77. } else {
  78. ({ AUTOMATIC1111_BASE_URL, AUTOMATIC1111_API_AUTH } = await getImageGenerationEngineUrls(
  79. localStorage.token
  80. ));
  81. }
  82. }
  83. };
  84. const updateImageGeneration = async () => {
  85. const res = await updateImageGenerationConfig(
  86. localStorage.token,
  87. imageGenerationEngine,
  88. enableImageGeneration
  89. ).catch((error) => {
  90. toast.error(error);
  91. return null;
  92. });
  93. if (res) {
  94. imageGenerationEngine = res.engine;
  95. enableImageGeneration = res.enabled;
  96. }
  97. if (enableImageGeneration) {
  98. config.set(await getBackendConfig(localStorage.token));
  99. getModels();
  100. }
  101. };
  102. onMount(async () => {
  103. if ($user.role === 'admin') {
  104. const res = await getImageGenerationConfig(localStorage.token).catch((error) => {
  105. toast.error(error);
  106. return null;
  107. });
  108. if (res) {
  109. imageGenerationEngine = res.engine;
  110. enableImageGeneration = res.enabled;
  111. }
  112. const URLS = await getImageGenerationEngineUrls(localStorage.token);
  113. AUTOMATIC1111_BASE_URL = URLS.AUTOMATIC1111_BASE_URL;
  114. AUTOMATIC1111_API_AUTH = URLS.AUTOMATIC1111_API_AUTH;
  115. COMFYUI_BASE_URL = URLS.COMFYUI_BASE_URL;
  116. const config = await getOpenAIConfig(localStorage.token);
  117. OPENAI_API_KEY = config.OPENAI_API_KEY;
  118. OPENAI_API_BASE_URL = config.OPENAI_API_BASE_URL;
  119. imageSize = await getImageSize(localStorage.token);
  120. steps = await getImageSteps(localStorage.token);
  121. if (enableImageGeneration) {
  122. getModels();
  123. }
  124. }
  125. });
  126. </script>
  127. <form
  128. class="flex flex-col h-full justify-between space-y-3 text-sm"
  129. on:submit|preventDefault={async () => {
  130. loading = true;
  131. if (imageGenerationEngine === 'openai') {
  132. await updateOpenAIConfig(localStorage.token, OPENAI_API_BASE_URL, OPENAI_API_KEY);
  133. }
  134. await updateDefaultImageGenerationModel(localStorage.token, selectedModel);
  135. await updateImageSize(localStorage.token, imageSize).catch((error) => {
  136. toast.error(error);
  137. return null;
  138. });
  139. await updateImageSteps(localStorage.token, steps).catch((error) => {
  140. toast.error(error);
  141. return null;
  142. });
  143. dispatch('save');
  144. loading = false;
  145. }}
  146. >
  147. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden">
  148. <div>
  149. <div class=" mb-1 text-sm font-medium">{$i18n.t('Image Settings')}</div>
  150. <div class=" py-0.5 flex w-full justify-between">
  151. <div class=" self-center text-xs font-medium">{$i18n.t('Image Generation Engine')}</div>
  152. <div class="flex items-center relative">
  153. <select
  154. class="w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  155. bind:value={imageGenerationEngine}
  156. placeholder={$i18n.t('Select a mode')}
  157. on:change={async () => {
  158. await updateImageGeneration();
  159. }}
  160. >
  161. <option value="">{$i18n.t('Default (Automatic1111)')}</option>
  162. <option value="comfyui">{$i18n.t('ComfyUI')}</option>
  163. <option value="openai">{$i18n.t('Open AI (Dall-E)')}</option>
  164. </select>
  165. </div>
  166. </div>
  167. <div>
  168. <div class=" py-0.5 flex w-full justify-between">
  169. <div class=" self-center text-xs font-medium">
  170. {$i18n.t('Image Generation (Experimental)')}
  171. </div>
  172. <button
  173. class="p-1 px-3 text-xs flex rounded transition"
  174. on:click={() => {
  175. if (imageGenerationEngine === '' && AUTOMATIC1111_BASE_URL === '') {
  176. toast.error($i18n.t('AUTOMATIC1111 Base URL is required.'));
  177. enableImageGeneration = false;
  178. } else if (imageGenerationEngine === 'comfyui' && COMFYUI_BASE_URL === '') {
  179. toast.error($i18n.t('ComfyUI Base URL is required.'));
  180. enableImageGeneration = false;
  181. } else if (imageGenerationEngine === 'openai' && OPENAI_API_KEY === '') {
  182. toast.error($i18n.t('OpenAI API Key is required.'));
  183. enableImageGeneration = false;
  184. } else {
  185. enableImageGeneration = !enableImageGeneration;
  186. }
  187. updateImageGeneration();
  188. }}
  189. type="button"
  190. >
  191. {#if enableImageGeneration === true}
  192. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  193. {:else}
  194. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  195. {/if}
  196. </button>
  197. </div>
  198. </div>
  199. </div>
  200. <hr class=" dark:border-gray-850" />
  201. {#if imageGenerationEngine === ''}
  202. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('AUTOMATIC1111 Base URL')}</div>
  203. <div class="flex w-full">
  204. <div class="flex-1 mr-2">
  205. <input
  206. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  207. placeholder={$i18n.t('Enter URL (e.g. http://127.0.0.1:7860/)')}
  208. bind:value={AUTOMATIC1111_BASE_URL}
  209. />
  210. </div>
  211. <button
  212. 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"
  213. type="button"
  214. on:click={() => {
  215. updateUrlHandler();
  216. }}
  217. >
  218. <svg
  219. xmlns="http://www.w3.org/2000/svg"
  220. viewBox="0 0 20 20"
  221. fill="currentColor"
  222. class="w-4 h-4"
  223. >
  224. <path
  225. fill-rule="evenodd"
  226. 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"
  227. clip-rule="evenodd"
  228. />
  229. </svg>
  230. </button>
  231. </div>
  232. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  233. {$i18n.t('Include `--api` flag when running stable-diffusion-webui')}
  234. <a
  235. class=" text-gray-300 font-medium"
  236. href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/3734"
  237. target="_blank"
  238. >
  239. {$i18n.t('(e.g. `sh webui.sh --api`)')}
  240. </a>
  241. </div>
  242. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('AUTOMATIC1111 Api Auth String')}</div>
  243. <div class="flex w-full">
  244. <div class="flex-1 mr-2">
  245. <input
  246. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  247. placeholder={$i18n.t('Enter api auth string (e.g. username:password)')}
  248. bind:value={AUTOMATIC1111_API_AUTH}
  249. />
  250. </div>
  251. </div>
  252. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  253. {$i18n.t('Include `--api-auth` flag when running stable-diffusion-webui')}
  254. <a
  255. class=" text-gray-300 font-medium"
  256. href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/13993"
  257. target="_blank"
  258. >
  259. {$i18n.t('(e.g. `sh webui.sh --api --api-auth username_password`)').replace('_', ':')}
  260. </a>
  261. </div>
  262. {:else if imageGenerationEngine === 'comfyui'}
  263. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('ComfyUI Base URL')}</div>
  264. <div class="flex w-full">
  265. <div class="flex-1 mr-2">
  266. <input
  267. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  268. placeholder={$i18n.t('Enter URL (e.g. http://127.0.0.1:7860/)')}
  269. bind:value={COMFYUI_BASE_URL}
  270. />
  271. </div>
  272. <button
  273. 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"
  274. type="button"
  275. on:click={() => {
  276. updateUrlHandler();
  277. }}
  278. >
  279. <svg
  280. xmlns="http://www.w3.org/2000/svg"
  281. viewBox="0 0 20 20"
  282. fill="currentColor"
  283. class="w-4 h-4"
  284. >
  285. <path
  286. fill-rule="evenodd"
  287. 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"
  288. clip-rule="evenodd"
  289. />
  290. </svg>
  291. </button>
  292. </div>
  293. {:else if imageGenerationEngine === 'openai'}
  294. <div>
  295. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('OpenAI API Config')}</div>
  296. <div class="flex gap-2 mb-1">
  297. <input
  298. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  299. placeholder={$i18n.t('API Base URL')}
  300. bind:value={OPENAI_API_BASE_URL}
  301. required
  302. />
  303. <input
  304. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  305. placeholder={$i18n.t('API Key')}
  306. bind:value={OPENAI_API_KEY}
  307. required
  308. />
  309. </div>
  310. </div>
  311. {/if}
  312. {#if enableImageGeneration}
  313. <hr class=" dark:border-gray-850" />
  314. <div>
  315. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Default Model')}</div>
  316. <div class="flex w-full">
  317. <div class="flex-1 mr-2">
  318. {#if imageGenerationEngine === 'openai' && !OPENAI_API_BASE_URL.includes('https://api.openai.com')}
  319. <div class="flex w-full">
  320. <div class="flex-1">
  321. <input
  322. list="model-list"
  323. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  324. bind:value={selectedModel}
  325. placeholder="Select a model"
  326. />
  327. <datalist id="model-list">
  328. {#each models ?? [] as model}
  329. <option value={model.id}>{model.name}</option>
  330. {/each}
  331. </datalist>
  332. </div>
  333. </div>
  334. {:else}
  335. <select
  336. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  337. bind:value={selectedModel}
  338. placeholder={$i18n.t('Select a model')}
  339. required
  340. >
  341. {#if !selectedModel}
  342. <option value="" disabled selected>{$i18n.t('Select a model')}</option>
  343. {/if}
  344. {#each models ?? [] as model}
  345. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">{model.name}</option
  346. >
  347. {/each}
  348. </select>
  349. {/if}
  350. </div>
  351. </div>
  352. </div>
  353. <div>
  354. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Image Size')}</div>
  355. <div class="flex w-full">
  356. <div class="flex-1 mr-2">
  357. <input
  358. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  359. placeholder={$i18n.t('Enter Image Size (e.g. 512x512)')}
  360. bind:value={imageSize}
  361. />
  362. </div>
  363. </div>
  364. </div>
  365. <div>
  366. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Steps')}</div>
  367. <div class="flex w-full">
  368. <div class="flex-1 mr-2">
  369. <input
  370. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  371. placeholder={$i18n.t('Enter Number of Steps (e.g. 50)')}
  372. bind:value={steps}
  373. />
  374. </div>
  375. </div>
  376. </div>
  377. {/if}
  378. </div>
  379. <div class="flex justify-end pt-3 text-sm font-medium">
  380. <button
  381. 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
  382. ? ' cursor-not-allowed'
  383. : ''}"
  384. type="submit"
  385. disabled={loading}
  386. >
  387. {$i18n.t('Save')}
  388. {#if loading}
  389. <div class="ml-2 self-center">
  390. <svg
  391. class=" w-4 h-4"
  392. viewBox="0 0 24 24"
  393. fill="currentColor"
  394. xmlns="http://www.w3.org/2000/svg"
  395. ><style>
  396. .spinner_ajPY {
  397. transform-origin: center;
  398. animation: spinner_AtaB 0.75s infinite linear;
  399. }
  400. @keyframes spinner_AtaB {
  401. 100% {
  402. transform: rotate(360deg);
  403. }
  404. }
  405. </style><path
  406. 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"
  407. opacity=".25"
  408. /><path
  409. 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"
  410. class="spinner_ajPY"
  411. /></svg
  412. >
  413. </div>
  414. {/if}
  415. </button>
  416. </div>
  417. </form>