Images.svelte 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. getDefaultDiffusionModel,
  8. getDiffusionModels,
  9. getImageGenerationEnabledStatus,
  10. getImageSize,
  11. toggleImageGenerationEnabledStatus,
  12. updateAUTOMATIC1111Url,
  13. updateDefaultDiffusionModel,
  14. updateImageSize,
  15. getImageSteps,
  16. updateImageSteps
  17. } from '$lib/apis/images';
  18. import { getBackendConfig } from '$lib/apis';
  19. const dispatch = createEventDispatcher();
  20. const i18n = getContext('i18n');
  21. export let saveSettings: Function;
  22. let loading = false;
  23. let enableImageGeneration = false;
  24. let AUTOMATIC1111_BASE_URL = '';
  25. let selectedModel = '';
  26. let models = null;
  27. let imageSize = '';
  28. let steps = 50;
  29. const getModels = async () => {
  30. models = await getDiffusionModels(localStorage.token).catch((error) => {
  31. toast.error(error);
  32. return null;
  33. });
  34. selectedModel = await getDefaultDiffusionModel(localStorage.token).catch((error) => {
  35. return '';
  36. });
  37. };
  38. const updateAUTOMATIC1111UrlHandler = async () => {
  39. const res = await updateAUTOMATIC1111Url(localStorage.token, AUTOMATIC1111_BASE_URL).catch(
  40. (error) => {
  41. toast.error(error);
  42. return null;
  43. }
  44. );
  45. if (res) {
  46. AUTOMATIC1111_BASE_URL = res;
  47. await getModels();
  48. if (models) {
  49. toast.success($i18n.t('Server connection verified'));
  50. }
  51. } else {
  52. AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
  53. }
  54. };
  55. const toggleImageGeneration = async () => {
  56. if (AUTOMATIC1111_BASE_URL) {
  57. enableImageGeneration = await toggleImageGenerationEnabledStatus(localStorage.token).catch(
  58. (error) => {
  59. toast.error(error);
  60. return false;
  61. }
  62. );
  63. if (enableImageGeneration) {
  64. config.set(await getBackendConfig(localStorage.token));
  65. getModels();
  66. }
  67. } else {
  68. enableImageGeneration = false;
  69. toast.error('AUTOMATIC1111_BASE_URL not provided');
  70. }
  71. };
  72. onMount(async () => {
  73. if ($user.role === 'admin') {
  74. enableImageGeneration = await getImageGenerationEnabledStatus(localStorage.token);
  75. AUTOMATIC1111_BASE_URL = await getAUTOMATIC1111Url(localStorage.token);
  76. if (enableImageGeneration && AUTOMATIC1111_BASE_URL) {
  77. imageSize = await getImageSize(localStorage.token);
  78. steps = await getImageSteps(localStorage.token);
  79. getModels();
  80. }
  81. }
  82. });
  83. </script>
  84. <form
  85. class="flex flex-col h-full justify-between space-y-3 text-sm"
  86. on:submit|preventDefault={async () => {
  87. loading = true;
  88. await updateDefaultDiffusionModel(localStorage.token, selectedModel);
  89. await updateImageSize(localStorage.token, imageSize).catch((error) => {
  90. toast.error(error);
  91. return null;
  92. });
  93. await updateImageSteps(localStorage.token, steps).catch((error) => {
  94. toast.error(error);
  95. return null;
  96. });
  97. dispatch('save');
  98. loading = false;
  99. }}
  100. >
  101. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[20.5rem]">
  102. <div>
  103. <div class=" mb-1 text-sm font-medium">{$i18n.t('Image Settings')}</div>
  104. <div>
  105. <div class=" py-0.5 flex w-full justify-between">
  106. <div class=" self-center text-xs font-medium">
  107. {$i18n.t('Image Generation (Experimental)')}
  108. </div>
  109. <button
  110. class="p-1 px-3 text-xs flex rounded transition"
  111. on:click={() => {
  112. toggleImageGeneration();
  113. }}
  114. type="button"
  115. >
  116. {#if enableImageGeneration === true}
  117. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  118. {:else}
  119. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  120. {/if}
  121. </button>
  122. </div>
  123. </div>
  124. </div>
  125. <hr class=" dark:border-gray-700" />
  126. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('AUTOMATIC1111 Base URL')}</div>
  127. <div class="flex w-full">
  128. <div class="flex-1 mr-2">
  129. <input
  130. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  131. placeholder="Enter URL (e.g. http://127.0.0.1:7860/)"
  132. bind:value={AUTOMATIC1111_BASE_URL}
  133. />
  134. </div>
  135. <button
  136. class="px-3 bg-gray-200 hover:bg-gray-300 dark:bg-gray-600 dark:hover:bg-gray-700 rounded transition"
  137. type="button"
  138. on:click={() => {
  139. // updateOllamaAPIUrlHandler();
  140. updateAUTOMATIC1111UrlHandler();
  141. }}
  142. >
  143. <svg
  144. xmlns="http://www.w3.org/2000/svg"
  145. viewBox="0 0 20 20"
  146. fill="currentColor"
  147. class="w-4 h-4"
  148. >
  149. <path
  150. fill-rule="evenodd"
  151. 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"
  152. clip-rule="evenodd"
  153. />
  154. </svg>
  155. </button>
  156. </div>
  157. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  158. Include `--api` flag when running stable-diffusion-webui
  159. <a
  160. class=" text-gray-300 font-medium"
  161. href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/3734"
  162. target="_blank"
  163. >
  164. (e.g. `sh webui.sh --api`)
  165. </a>
  166. </div>
  167. {#if enableImageGeneration}
  168. <hr class=" dark:border-gray-700" />
  169. <div>
  170. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Default Model')}</div>
  171. <div class="flex w-full">
  172. <div class="flex-1 mr-2">
  173. <select
  174. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  175. bind:value={selectedModel}
  176. placeholder={$i18n.t('Select a Model')}
  177. >
  178. {#if !selectedModel}
  179. <option value="" disabled selected>{$i18n.t('Select a Model')}</option>
  180. {/if}
  181. {#each models ?? [] as model}
  182. <option value={model.title} class="bg-gray-100 dark:bg-gray-700"
  183. >{model.model_name}</option
  184. >
  185. {/each}
  186. </select>
  187. </div>
  188. </div>
  189. </div>
  190. <div>
  191. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Image Size')}</div>
  192. <div class="flex w-full">
  193. <div class="flex-1 mr-2">
  194. <input
  195. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  196. placeholder="Enter Image Size (e.g. 512x512)"
  197. bind:value={imageSize}
  198. />
  199. </div>
  200. </div>
  201. </div>
  202. <div>
  203. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Steps')}</div>
  204. <div class="flex w-full">
  205. <div class="flex-1 mr-2">
  206. <input
  207. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  208. placeholder="Enter Number of Steps (e.g. 50)"
  209. bind:value={steps}
  210. />
  211. </div>
  212. </div>
  213. </div>
  214. {/if}
  215. </div>
  216. <div class="flex justify-end pt-3 text-sm font-medium">
  217. <button
  218. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded flex flex-row space-x-1 items-center {loading
  219. ? ' cursor-not-allowed'
  220. : ''}"
  221. type="submit"
  222. disabled={loading}
  223. >
  224. Save
  225. {#if loading}
  226. <div class="ml-2 self-center">
  227. <svg
  228. class=" w-4 h-4"
  229. viewBox="0 0 24 24"
  230. fill="currentColor"
  231. xmlns="http://www.w3.org/2000/svg"
  232. ><style>
  233. .spinner_ajPY {
  234. transform-origin: center;
  235. animation: spinner_AtaB 0.75s infinite linear;
  236. }
  237. @keyframes spinner_AtaB {
  238. 100% {
  239. transform: rotate(360deg);
  240. }
  241. }
  242. </style><path
  243. 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"
  244. opacity=".25"
  245. /><path
  246. 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"
  247. class="spinner_ajPY"
  248. /></svg
  249. >
  250. </div>
  251. {/if}
  252. </button>
  253. </div>
  254. </form>