Images.svelte 14 KB

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