Images.svelte 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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('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">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">Image Generation (Experimental)</div>
  107. <button
  108. class="p-1 px-3 text-xs flex rounded transition"
  109. on:click={() => {
  110. toggleImageGeneration();
  111. }}
  112. type="button"
  113. >
  114. {#if enableImageGeneration === true}
  115. <span class="ml-2 self-center">On</span>
  116. {:else}
  117. <span class="ml-2 self-center">Off</span>
  118. {/if}
  119. </button>
  120. </div>
  121. </div>
  122. </div>
  123. <hr class=" dark:border-gray-700" />
  124. <div class=" mb-2.5 text-sm font-medium">AUTOMATIC1111 Base URL</div>
  125. <div class="flex w-full">
  126. <div class="flex-1 mr-2">
  127. <input
  128. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  129. placeholder="Enter URL (e.g. http://127.0.0.1:7860/)"
  130. bind:value={AUTOMATIC1111_BASE_URL}
  131. />
  132. </div>
  133. <button
  134. class="px-3 bg-gray-200 hover:bg-gray-300 dark:bg-gray-600 dark:hover:bg-gray-700 rounded transition"
  135. type="button"
  136. on:click={() => {
  137. // updateOllamaAPIUrlHandler();
  138. updateAUTOMATIC1111UrlHandler();
  139. }}
  140. >
  141. <svg
  142. xmlns="http://www.w3.org/2000/svg"
  143. viewBox="0 0 20 20"
  144. fill="currentColor"
  145. class="w-4 h-4"
  146. >
  147. <path
  148. fill-rule="evenodd"
  149. 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"
  150. clip-rule="evenodd"
  151. />
  152. </svg>
  153. </button>
  154. </div>
  155. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  156. Include `--api` flag when running stable-diffusion-webui
  157. <a
  158. class=" text-gray-300 font-medium"
  159. href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/3734"
  160. target="_blank"
  161. >
  162. (e.g. `sh webui.sh --api`)
  163. </a>
  164. </div>
  165. {#if enableImageGeneration}
  166. <hr class=" dark:border-gray-700" />
  167. <div>
  168. <div class=" mb-2.5 text-sm font-medium">Set Default Model</div>
  169. <div class="flex w-full">
  170. <div class="flex-1 mr-2">
  171. <select
  172. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  173. bind:value={selectedModel}
  174. placeholder={$i18n.t('ModelSelectorPlaceholder')}
  175. >
  176. {#if !selectedModel}
  177. <option value="" disabled selected>{$i18n.t('ModelSelectorPlaceholder')}</option>
  178. {/if}
  179. {#each models ?? [] as model}
  180. <option value={model.title} class="bg-gray-100 dark:bg-gray-700"
  181. >{model.model_name}</option
  182. >
  183. {/each}
  184. </select>
  185. </div>
  186. </div>
  187. </div>
  188. <div>
  189. <div class=" mb-2.5 text-sm font-medium">Set Image Size</div>
  190. <div class="flex w-full">
  191. <div class="flex-1 mr-2">
  192. <input
  193. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  194. placeholder="Enter Image Size (e.g. 512x512)"
  195. bind:value={imageSize}
  196. />
  197. </div>
  198. </div>
  199. </div>
  200. <div>
  201. <div class=" mb-2.5 text-sm font-medium">Set Steps</div>
  202. <div class="flex w-full">
  203. <div class="flex-1 mr-2">
  204. <input
  205. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  206. placeholder="Enter Number of Steps (e.g. 50)"
  207. bind:value={steps}
  208. />
  209. </div>
  210. </div>
  211. </div>
  212. {/if}
  213. </div>
  214. <div class="flex justify-end pt-3 text-sm font-medium">
  215. <button
  216. 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
  217. ? ' cursor-not-allowed'
  218. : ''}"
  219. type="submit"
  220. disabled={loading}
  221. >
  222. Save
  223. {#if loading}
  224. <div class="ml-2 self-center">
  225. <svg
  226. class=" w-4 h-4"
  227. viewBox="0 0 24 24"
  228. fill="currentColor"
  229. xmlns="http://www.w3.org/2000/svg"
  230. ><style>
  231. .spinner_ajPY {
  232. transform-origin: center;
  233. animation: spinner_AtaB 0.75s infinite linear;
  234. }
  235. @keyframes spinner_AtaB {
  236. 100% {
  237. transform: rotate(360deg);
  238. }
  239. }
  240. </style><path
  241. 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"
  242. opacity=".25"
  243. /><path
  244. 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"
  245. class="spinner_ajPY"
  246. /></svg
  247. >
  248. </div>
  249. {/if}
  250. </button>
  251. </div>
  252. </form>