Images.svelte 6.9 KB

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