Images.svelte 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. getAUTOMATIC1111Url,
  7. getImageGenerationModels,
  8. getDefaultImageGenerationModel,
  9. updateDefaultImageGenerationModel,
  10. getImageSize,
  11. getImageGenerationConfig,
  12. updateImageGenerationConfig,
  13. updateAUTOMATIC1111Url,
  14. updateImageSize,
  15. getImageSteps,
  16. updateImageSteps,
  17. getOpenAIKey,
  18. updateOpenAIKey
  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 OPENAI_API_KEY = '';
  29. let selectedModel = '';
  30. let models = null;
  31. let imageSize = '';
  32. let steps = 50;
  33. const getModels = async () => {
  34. models = await getImageGenerationModels(localStorage.token).catch((error) => {
  35. toast.error(error);
  36. return null;
  37. });
  38. selectedModel = await getDefaultImageGenerationModel(localStorage.token).catch((error) => {
  39. return '';
  40. });
  41. };
  42. const updateAUTOMATIC1111UrlHandler = async () => {
  43. const res = await updateAUTOMATIC1111Url(localStorage.token, AUTOMATIC1111_BASE_URL).catch(
  44. (error) => {
  45. toast.error(error);
  46. return null;
  47. }
  48. );
  49. if (res) {
  50. AUTOMATIC1111_BASE_URL = res;
  51. await getModels();
  52. if (models) {
  53. toast.success($i18n.t('Server connection verified'));
  54. }
  55. } else {
  56. AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
  57. }
  58. };
  59. const updateImageGeneration = async () => {
  60. const res = await updateImageGenerationConfig(
  61. localStorage.token,
  62. imageGenerationEngine,
  63. enableImageGeneration
  64. ).catch((error) => {
  65. toast.error(error);
  66. return null;
  67. });
  68. if (res) {
  69. imageGenerationEngine = res.engine;
  70. enableImageGeneration = res.enabled;
  71. }
  72. if (enableImageGeneration) {
  73. config.set(await getBackendConfig(localStorage.token));
  74. getModels();
  75. }
  76. };
  77. onMount(async () => {
  78. if ($user.role === 'admin') {
  79. const res = await getImageGenerationConfig(localStorage.token).catch((error) => {
  80. toast.error(error);
  81. return null;
  82. });
  83. if (res) {
  84. imageGenerationEngine = res.engine;
  85. enableImageGeneration = res.enabled;
  86. }
  87. AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
  88. OPENAI_API_KEY = await getOpenAIKey(localStorage.token);
  89. imageSize = await getImageSize(localStorage.token);
  90. steps = await getImageSteps(localStorage.token);
  91. if (enableImageGeneration) {
  92. getModels();
  93. }
  94. }
  95. });
  96. </script>
  97. <form
  98. class="flex flex-col h-full justify-between space-y-3 text-sm"
  99. on:submit|preventDefault={async () => {
  100. loading = true;
  101. await updateOpenAIKey(localStorage.token, OPENAI_API_KEY);
  102. await updateDefaultImageGenerationModel(localStorage.token, selectedModel);
  103. await updateDefaultImageGenerationModel(localStorage.token, selectedModel);
  104. await updateImageSize(localStorage.token, imageSize).catch((error) => {
  105. toast.error(error);
  106. return null;
  107. });
  108. await updateImageSteps(localStorage.token, steps).catch((error) => {
  109. toast.error(error);
  110. return null;
  111. });
  112. dispatch('save');
  113. loading = false;
  114. }}
  115. >
  116. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[20.5rem]">
  117. <div>
  118. <div class=" mb-1 text-sm font-medium">{$i18n.t('Image Settings')}</div>
  119. <div class=" py-0.5 flex w-full justify-between">
  120. <div class=" self-center text-xs font-medium">{$i18n.t('Image Generation Engine')}</div>
  121. <div class="flex items-center relative">
  122. <select
  123. class="w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  124. bind:value={imageGenerationEngine}
  125. placeholder={$i18n.t('Select a mode')}
  126. on:change={async () => {
  127. await updateImageGeneration();
  128. }}
  129. >
  130. <option value="">{$i18n.t('Default (Automatic1111)')}</option>
  131. <option value="openai">{$i18n.t('Open AI (Dall-E)')}</option>
  132. </select>
  133. </div>
  134. </div>
  135. <div>
  136. <div class=" py-0.5 flex w-full justify-between">
  137. <div class=" self-center text-xs font-medium">
  138. {$i18n.t('Image Generation (Experimental)')}
  139. </div>
  140. <button
  141. class="p-1 px-3 text-xs flex rounded transition"
  142. on:click={() => {
  143. if (imageGenerationEngine === '' && AUTOMATIC1111_BASE_URL === '') {
  144. toast.error($i18n.t('AUTOMATIC1111 Base URL is required.'));
  145. enableImageGeneration = false;
  146. } else if (imageGenerationEngine === 'openai' && OPENAI_API_KEY === '') {
  147. toast.error($i18n.t('OpenAI API Key is required.'));
  148. enableImageGeneration = false;
  149. } else {
  150. enableImageGeneration = !enableImageGeneration;
  151. }
  152. updateImageGeneration();
  153. }}
  154. type="button"
  155. >
  156. {#if enableImageGeneration === true}
  157. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  158. {:else}
  159. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  160. {/if}
  161. </button>
  162. </div>
  163. </div>
  164. </div>
  165. <hr class=" dark:border-gray-700" />
  166. {#if imageGenerationEngine === ''}
  167. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('AUTOMATIC1111 Base URL')}</div>
  168. <div class="flex w-full">
  169. <div class="flex-1 mr-2">
  170. <input
  171. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  172. placeholder={$i18n.t('Enter URL (e.g. http://127.0.0.1:7860/)')}
  173. bind:value={AUTOMATIC1111_BASE_URL}
  174. />
  175. </div>
  176. <button
  177. class="px-3 bg-gray-200 hover:bg-gray-300 dark:bg-gray-600 dark:hover:bg-gray-700 rounded-lg transition"
  178. type="button"
  179. on:click={() => {
  180. // updateOllamaAPIUrlHandler();
  181. updateAUTOMATIC1111UrlHandler();
  182. }}
  183. >
  184. <svg
  185. xmlns="http://www.w3.org/2000/svg"
  186. viewBox="0 0 20 20"
  187. fill="currentColor"
  188. class="w-4 h-4"
  189. >
  190. <path
  191. fill-rule="evenodd"
  192. 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"
  193. clip-rule="evenodd"
  194. />
  195. </svg>
  196. </button>
  197. </div>
  198. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  199. {$i18n.t('Include `--api` flag when running stable-diffusion-webui')}
  200. <a
  201. class=" text-gray-300 font-medium"
  202. href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/3734"
  203. target="_blank"
  204. >
  205. {$i18n.t('(e.g. `sh webui.sh --api`)')}
  206. </a>
  207. </div>
  208. {:else if imageGenerationEngine === 'openai'}
  209. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('OpenAI API Key')}</div>
  210. <div class="flex w-full">
  211. <div class="flex-1 mr-2">
  212. <input
  213. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  214. placeholder={$i18n.t('Enter API Key')}
  215. bind:value={OPENAI_API_KEY}
  216. />
  217. </div>
  218. </div>
  219. {/if}
  220. {#if enableImageGeneration}
  221. <hr class=" dark:border-gray-700" />
  222. <div>
  223. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Default Model')}</div>
  224. <div class="flex w-full">
  225. <div class="flex-1 mr-2">
  226. <select
  227. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  228. bind:value={selectedModel}
  229. placeholder={$i18n.t('Select a model')}
  230. >
  231. {#if !selectedModel}
  232. <option value="" disabled selected>{$i18n.t('Select a model')}</option>
  233. {/if}
  234. {#each models ?? [] as model}
  235. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">{model.name}</option>
  236. {/each}
  237. </select>
  238. </div>
  239. </div>
  240. </div>
  241. <div>
  242. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Image Size')}</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 Image Size (e.g. 512x512)')}
  248. bind:value={imageSize}
  249. />
  250. </div>
  251. </div>
  252. </div>
  253. <div>
  254. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Steps')}</div>
  255. <div class="flex w-full">
  256. <div class="flex-1 mr-2">
  257. <input
  258. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  259. placeholder={$i18n.t('Enter Number of Steps (e.g. 50)')}
  260. bind:value={steps}
  261. />
  262. </div>
  263. </div>
  264. </div>
  265. {/if}
  266. </div>
  267. <div class="flex justify-end pt-3 text-sm font-medium">
  268. <button
  269. 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
  270. ? ' cursor-not-allowed'
  271. : ''}"
  272. type="submit"
  273. disabled={loading}
  274. >
  275. {$i18n.t('Save')}
  276. {#if loading}
  277. <div class="ml-2 self-center">
  278. <svg
  279. class=" w-4 h-4"
  280. viewBox="0 0 24 24"
  281. fill="currentColor"
  282. xmlns="http://www.w3.org/2000/svg"
  283. ><style>
  284. .spinner_ajPY {
  285. transform-origin: center;
  286. animation: spinner_AtaB 0.75s infinite linear;
  287. }
  288. @keyframes spinner_AtaB {
  289. 100% {
  290. transform: rotate(360deg);
  291. }
  292. }
  293. </style><path
  294. 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"
  295. opacity=".25"
  296. /><path
  297. 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"
  298. class="spinner_ajPY"
  299. /></svg
  300. >
  301. </div>
  302. {/if}
  303. </button>
  304. </div>
  305. </form>