Images.svelte 13 KB

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