Images.svelte 6.9 KB

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