Models.svelte 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { onMount, getContext } from 'svelte';
  6. import { WEBUI_NAME, modelfiles, models, settings, user } from '$lib/stores';
  7. import { addNewModel, deleteModelById, getModelInfos } from '$lib/apis/models';
  8. import { deleteModel } from '$lib/apis/ollama';
  9. import { goto } from '$app/navigation';
  10. import { getModels } from '$lib/apis';
  11. const i18n = getContext('i18n');
  12. let localModelfiles = [];
  13. let importFiles;
  14. let modelsImportInputElement: HTMLInputElement;
  15. const deleteModelHandler = async (model) => {
  16. console.log(model.info);
  17. if (!model?.info) {
  18. toast.error(
  19. $i18n.t('{{ owner }}: You cannot delete a base model', {
  20. owner: model.owned_by.toUpperCase()
  21. })
  22. );
  23. return null;
  24. }
  25. const res = await deleteModelById(localStorage.token, model.id);
  26. if (res) {
  27. toast.success($i18n.t(`Deleted {{name}}`, { name: model.id }));
  28. }
  29. await models.set(await getModels(localStorage.token));
  30. };
  31. const cloneModelHandler = async (model) => {
  32. if ((model?.info?.base_model_id ?? null) === null) {
  33. toast.error($i18n.t('You cannot clone a base model'));
  34. return;
  35. } else {
  36. sessionStorage.model = JSON.stringify({
  37. ...model,
  38. id: `${model.id}-clone`,
  39. name: `${model.name} (Clone)`
  40. });
  41. goto('/workspace/models/create');
  42. }
  43. };
  44. const shareModelHandler = async (model) => {
  45. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  46. const url = 'https://openwebui.com';
  47. const tab = await window.open(`${url}/models/create`, '_blank');
  48. window.addEventListener(
  49. 'message',
  50. (event) => {
  51. if (event.origin !== url) return;
  52. if (event.data === 'loaded') {
  53. tab.postMessage(JSON.stringify(model), '*');
  54. }
  55. },
  56. false
  57. );
  58. };
  59. const downloadModels = async (models) => {
  60. let blob = new Blob([JSON.stringify(models)], {
  61. type: 'application/json'
  62. });
  63. saveAs(blob, `models-export-${Date.now()}.json`);
  64. };
  65. onMount(() => {
  66. // Legacy code to sync localModelfiles with models
  67. localModelfiles = JSON.parse(localStorage.getItem('modelfiles') ?? '[]');
  68. if (localModelfiles) {
  69. console.log(localModelfiles);
  70. }
  71. });
  72. </script>
  73. <svelte:head>
  74. <title>
  75. {$i18n.t('Models')} | {$WEBUI_NAME}
  76. </title>
  77. </svelte:head>
  78. <div class=" text-lg font-semibold mb-3">{$i18n.t('Models')}</div>
  79. <a class=" flex space-x-4 cursor-pointer w-full mb-2 px-3 py-2" href="/workspace/models/create">
  80. <div class=" self-center w-10">
  81. <div
  82. class="w-full h-10 flex justify-center rounded-full bg-transparent dark:bg-gray-700 border border-dashed border-gray-200"
  83. >
  84. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6">
  85. <path
  86. fill-rule="evenodd"
  87. d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z"
  88. clip-rule="evenodd"
  89. />
  90. </svg>
  91. </div>
  92. </div>
  93. <div class=" self-center">
  94. <div class=" font-bold">{$i18n.t('Create a model')}</div>
  95. <div class=" text-sm">{$i18n.t('Customize models for a specific purpose')}</div>
  96. </div>
  97. </a>
  98. <hr class=" dark:border-gray-850" />
  99. <div class=" my-2 mb-5">
  100. {#each $models as model}
  101. <div
  102. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  103. >
  104. <a
  105. class=" flex flex-1 space-x-4 cursor-pointer w-full"
  106. href={`/?models=${encodeURIComponent(model.id)}`}
  107. >
  108. <div class=" self-center w-10">
  109. <div class=" rounded-full bg-stone-700">
  110. <img
  111. src={model?.info?.meta?.profile_image_url ?? '/favicon.png'}
  112. alt="modelfile profile"
  113. class=" rounded-full w-full h-auto object-cover"
  114. />
  115. </div>
  116. </div>
  117. <div class=" flex-1 self-center">
  118. <div class=" font-bold line-clamp-1">{model.name}</div>
  119. <div class=" text-sm overflow-hidden text-ellipsis line-clamp-1">
  120. {model?.info?.meta?.description ?? model.id}
  121. </div>
  122. </div>
  123. </a>
  124. <div class="flex flex-row space-x-1 self-center">
  125. <a
  126. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  127. type="button"
  128. href={`/workspace/models/edit?id=${encodeURIComponent(model.id)}`}
  129. >
  130. <svg
  131. xmlns="http://www.w3.org/2000/svg"
  132. fill="none"
  133. viewBox="0 0 24 24"
  134. stroke-width="1.5"
  135. stroke="currentColor"
  136. class="w-4 h-4"
  137. >
  138. <path
  139. stroke-linecap="round"
  140. stroke-linejoin="round"
  141. d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125"
  142. />
  143. </svg>
  144. </a>
  145. <button
  146. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  147. type="button"
  148. on:click={() => {
  149. cloneModelHandler(model);
  150. }}
  151. >
  152. <svg
  153. xmlns="http://www.w3.org/2000/svg"
  154. fill="none"
  155. viewBox="0 0 24 24"
  156. stroke-width="1.5"
  157. stroke="currentColor"
  158. class="w-4 h-4"
  159. >
  160. <path
  161. stroke-linecap="round"
  162. stroke-linejoin="round"
  163. d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"
  164. />
  165. </svg>
  166. </button>
  167. <button
  168. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  169. type="button"
  170. on:click={() => {
  171. shareModelHandler(model);
  172. }}
  173. >
  174. <svg
  175. xmlns="http://www.w3.org/2000/svg"
  176. fill="none"
  177. viewBox="0 0 24 24"
  178. stroke-width="1.5"
  179. stroke="currentColor"
  180. class="w-4 h-4"
  181. >
  182. <path
  183. stroke-linecap="round"
  184. stroke-linejoin="round"
  185. d="M7.217 10.907a2.25 2.25 0 1 0 0 2.186m0-2.186c.18.324.283.696.283 1.093s-.103.77-.283 1.093m0-2.186 9.566-5.314m-9.566 7.5 9.566 5.314m0 0a2.25 2.25 0 1 0 3.935 2.186 2.25 2.25 0 0 0-3.935-2.186Zm0-12.814a2.25 2.25 0 1 0 3.933-2.185 2.25 2.25 0 0 0-3.933 2.185Z"
  186. />
  187. </svg>
  188. </button>
  189. <button
  190. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  191. type="button"
  192. on:click={() => {
  193. deleteModelHandler(model);
  194. }}
  195. >
  196. <svg
  197. xmlns="http://www.w3.org/2000/svg"
  198. fill="none"
  199. viewBox="0 0 24 24"
  200. stroke-width="1.5"
  201. stroke="currentColor"
  202. class="w-4 h-4"
  203. >
  204. <path
  205. stroke-linecap="round"
  206. stroke-linejoin="round"
  207. d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"
  208. />
  209. </svg>
  210. </button>
  211. </div>
  212. </div>
  213. {/each}
  214. </div>
  215. <div class=" flex justify-end w-full mb-3">
  216. <div class="flex space-x-1">
  217. <input
  218. id="models-import-input"
  219. bind:this={modelsImportInputElement}
  220. bind:files={importFiles}
  221. type="file"
  222. accept=".json"
  223. hidden
  224. on:change={() => {
  225. console.log(importFiles);
  226. let reader = new FileReader();
  227. reader.onload = async (event) => {
  228. let savedModels = JSON.parse(event.target.result);
  229. console.log(savedModels);
  230. for (const model of savedModels) {
  231. await addNewModel(localStorage.token, model).catch((error) => {
  232. return null;
  233. });
  234. }
  235. await models.set(await getModels(localStorage.token));
  236. };
  237. reader.readAsText(importFiles[0]);
  238. }}
  239. />
  240. <button
  241. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  242. on:click={() => {
  243. modelsImportInputElement.click();
  244. }}
  245. >
  246. <div class=" self-center mr-2 font-medium">{$i18n.t('Import Models')}</div>
  247. <div class=" self-center">
  248. <svg
  249. xmlns="http://www.w3.org/2000/svg"
  250. viewBox="0 0 16 16"
  251. fill="currentColor"
  252. class="w-3.5 h-3.5"
  253. >
  254. <path
  255. fill-rule="evenodd"
  256. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
  257. clip-rule="evenodd"
  258. />
  259. </svg>
  260. </div>
  261. </button>
  262. <button
  263. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  264. on:click={async () => {
  265. downloadModels($models);
  266. }}
  267. >
  268. <div class=" self-center mr-2 font-medium">{$i18n.t('Export Models')}</div>
  269. <div class=" self-center">
  270. <svg
  271. xmlns="http://www.w3.org/2000/svg"
  272. viewBox="0 0 16 16"
  273. fill="currentColor"
  274. class="w-3.5 h-3.5"
  275. >
  276. <path
  277. fill-rule="evenodd"
  278. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 3.5a.75.75 0 0 1 .75.75v2.69l.72-.72a.75.75 0 1 1 1.06 1.06l-2 2a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l.72.72V6.25A.75.75 0 0 1 8 5.5Z"
  279. clip-rule="evenodd"
  280. />
  281. </svg>
  282. </div>
  283. </button>
  284. </div>
  285. {#if localModelfiles.length > 0}
  286. <div class="flex">
  287. <div class=" self-center text-sm font-medium mr-4">
  288. {localModelfiles.length} Local Modelfiles Detected
  289. </div>
  290. <div class="flex space-x-1">
  291. <button
  292. class="self-center w-fit text-sm p-1.5 border dark:border-gray-600 rounded-xl flex"
  293. on:click={async () => {
  294. downloadModels(localModelfiles);
  295. localStorage.removeItem('modelfiles');
  296. localModelfiles = JSON.parse(localStorage.getItem('modelfiles') ?? '[]');
  297. }}
  298. >
  299. <div class=" self-center">
  300. <svg
  301. xmlns="http://www.w3.org/2000/svg"
  302. fill="none"
  303. viewBox="0 0 24 24"
  304. stroke-width="1.5"
  305. stroke="currentColor"
  306. class="w-4 h-4"
  307. >
  308. <path
  309. stroke-linecap="round"
  310. stroke-linejoin="round"
  311. d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"
  312. />
  313. </svg>
  314. </div>
  315. </button>
  316. </div>
  317. </div>
  318. {/if}
  319. </div>
  320. <div class=" my-16">
  321. <div class=" text-lg font-semibold mb-3">{$i18n.t('Made by OpenWebUI Community')}</div>
  322. <a
  323. class=" flex space-x-4 cursor-pointer w-full mb-2 px-3 py-2"
  324. href="https://openwebui.com/"
  325. target="_blank"
  326. >
  327. <div class=" self-center w-10">
  328. <div
  329. class="w-full h-10 flex justify-center rounded-full bg-transparent dark:bg-gray-700 border border-dashed border-gray-200"
  330. >
  331. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6">
  332. <path
  333. fill-rule="evenodd"
  334. d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z"
  335. clip-rule="evenodd"
  336. />
  337. </svg>
  338. </div>
  339. </div>
  340. <div class=" self-center">
  341. <div class=" font-bold">{$i18n.t('Discover a model')}</div>
  342. <div class=" text-sm">{$i18n.t('Discover, download, and explore model presets')}</div>
  343. </div>
  344. </a>
  345. </div>