ManageOllama.svelte 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { getContext, onMount } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import { WEBUI_NAME, models, MODEL_DOWNLOAD_POOL, user, config } from '$lib/stores';
  6. import { splitStream } from '$lib/utils';
  7. import {
  8. createModel,
  9. deleteModel,
  10. downloadModel,
  11. getOllamaUrls,
  12. getOllamaVersion,
  13. pullModel,
  14. uploadModel,
  15. getOllamaConfig,
  16. getOllamaModels
  17. } from '$lib/apis/ollama';
  18. import { getModels } from '$lib/apis';
  19. import Modal from '$lib/components/common/Modal.svelte';
  20. import Tooltip from '$lib/components/common/Tooltip.svelte';
  21. import ModelDeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  22. import Spinner from '$lib/components/common/Spinner.svelte';
  23. let modelUploadInputElement: HTMLInputElement;
  24. let showModelDeleteConfirm = false;
  25. let loading = true;
  26. // Models
  27. export let urlIdx: number | null = null;
  28. let ollamaModels = [];
  29. let updateModelId = null;
  30. let updateProgress = null;
  31. let showExperimentalOllama = false;
  32. const MAX_PARALLEL_DOWNLOADS = 3;
  33. let modelTransferring = false;
  34. let modelTag = '';
  35. let createModelLoading = false;
  36. let createModelTag = '';
  37. let createModelContent = '';
  38. let createModelDigest = '';
  39. let createModelPullProgress = null;
  40. let digest = '';
  41. let pullProgress = null;
  42. let modelUploadMode = 'file';
  43. let modelInputFile: File[] | null = null;
  44. let modelFileUrl = '';
  45. let modelFileContent = `TEMPLATE """{{ .System }}\nUSER: {{ .Prompt }}\nASSISTANT: """\nPARAMETER num_ctx 4096\nPARAMETER stop "</s>"\nPARAMETER stop "USER:"\nPARAMETER stop "ASSISTANT:"`;
  46. let modelFileDigest = '';
  47. let uploadProgress = null;
  48. let uploadMessage = '';
  49. let deleteModelTag = '';
  50. const updateModelsHandler = async () => {
  51. for (const model of ollamaModels) {
  52. console.log(model);
  53. updateModelId = model.id;
  54. const [res, controller] = await pullModel(localStorage.token, model.id, urlIdx).catch(
  55. (error) => {
  56. toast.error(`${error}`);
  57. return null;
  58. }
  59. );
  60. if (res) {
  61. const reader = res.body
  62. .pipeThrough(new TextDecoderStream())
  63. .pipeThrough(splitStream('\n'))
  64. .getReader();
  65. while (true) {
  66. try {
  67. const { value, done } = await reader.read();
  68. if (done) break;
  69. let lines = value.split('\n');
  70. for (const line of lines) {
  71. if (line !== '') {
  72. let data = JSON.parse(line);
  73. console.log(data);
  74. if (data.error) {
  75. throw data.error;
  76. }
  77. if (data.detail) {
  78. throw data.detail;
  79. }
  80. if (data.status) {
  81. if (data.digest) {
  82. updateProgress = 0;
  83. if (data.completed) {
  84. updateProgress = Math.round((data.completed / data.total) * 1000) / 10;
  85. } else {
  86. updateProgress = 100;
  87. }
  88. } else {
  89. toast.success(data.status);
  90. }
  91. }
  92. }
  93. }
  94. } catch (error) {
  95. console.log(error);
  96. }
  97. }
  98. }
  99. }
  100. updateModelId = null;
  101. updateProgress = null;
  102. };
  103. const pullModelHandler = async () => {
  104. const sanitizedModelTag = modelTag.trim().replace(/^ollama\s+(run|pull)\s+/, '');
  105. console.log($MODEL_DOWNLOAD_POOL);
  106. if ($MODEL_DOWNLOAD_POOL[sanitizedModelTag]) {
  107. toast.error(
  108. $i18n.t(`Model '{{modelTag}}' is already in queue for downloading.`, {
  109. modelTag: sanitizedModelTag
  110. })
  111. );
  112. return;
  113. }
  114. if (Object.keys($MODEL_DOWNLOAD_POOL).length === MAX_PARALLEL_DOWNLOADS) {
  115. toast.error(
  116. $i18n.t('Maximum of 3 models can be downloaded simultaneously. Please try again later.')
  117. );
  118. return;
  119. }
  120. const [res, controller] = await pullModel(localStorage.token, sanitizedModelTag, urlIdx).catch(
  121. (error) => {
  122. toast.error(`${error}`);
  123. return null;
  124. }
  125. );
  126. if (res) {
  127. const reader = res.body
  128. .pipeThrough(new TextDecoderStream())
  129. .pipeThrough(splitStream('\n'))
  130. .getReader();
  131. MODEL_DOWNLOAD_POOL.set({
  132. ...$MODEL_DOWNLOAD_POOL,
  133. [sanitizedModelTag]: {
  134. ...$MODEL_DOWNLOAD_POOL[sanitizedModelTag],
  135. abortController: controller,
  136. reader,
  137. done: false
  138. }
  139. });
  140. while (true) {
  141. try {
  142. const { value, done } = await reader.read();
  143. if (done) break;
  144. let lines = value.split('\n');
  145. for (const line of lines) {
  146. if (line !== '') {
  147. let data = JSON.parse(line);
  148. console.log(data);
  149. if (data.error) {
  150. throw data.error;
  151. }
  152. if (data.detail) {
  153. throw data.detail;
  154. }
  155. if (data.status) {
  156. if (data.digest) {
  157. let downloadProgress = 0;
  158. if (data.completed) {
  159. downloadProgress = Math.round((data.completed / data.total) * 1000) / 10;
  160. } else {
  161. downloadProgress = 100;
  162. }
  163. MODEL_DOWNLOAD_POOL.set({
  164. ...$MODEL_DOWNLOAD_POOL,
  165. [sanitizedModelTag]: {
  166. ...$MODEL_DOWNLOAD_POOL[sanitizedModelTag],
  167. pullProgress: downloadProgress,
  168. digest: data.digest
  169. }
  170. });
  171. } else {
  172. toast.success(data.status);
  173. MODEL_DOWNLOAD_POOL.set({
  174. ...$MODEL_DOWNLOAD_POOL,
  175. [sanitizedModelTag]: {
  176. ...$MODEL_DOWNLOAD_POOL[sanitizedModelTag],
  177. done: data.status === 'success'
  178. }
  179. });
  180. }
  181. }
  182. }
  183. }
  184. } catch (error) {
  185. console.log(error);
  186. if (typeof error !== 'string') {
  187. error = error.message;
  188. }
  189. toast.error(`${error}`);
  190. // opts.callback({ success: false, error, modelName: opts.modelName });
  191. }
  192. }
  193. console.log($MODEL_DOWNLOAD_POOL[sanitizedModelTag]);
  194. if ($MODEL_DOWNLOAD_POOL[sanitizedModelTag].done) {
  195. toast.success(
  196. $i18n.t(`Model '{{modelName}}' has been successfully downloaded.`, {
  197. modelName: sanitizedModelTag
  198. })
  199. );
  200. models.set(await getModels(localStorage.token));
  201. } else {
  202. toast.error($i18n.t('Download canceled'));
  203. }
  204. delete $MODEL_DOWNLOAD_POOL[sanitizedModelTag];
  205. MODEL_DOWNLOAD_POOL.set({
  206. ...$MODEL_DOWNLOAD_POOL
  207. });
  208. }
  209. modelTag = '';
  210. modelTransferring = false;
  211. };
  212. const uploadModelHandler = async () => {
  213. modelTransferring = true;
  214. let uploaded = false;
  215. let fileResponse = null;
  216. let name = '';
  217. if (modelUploadMode === 'file') {
  218. const file = modelInputFile ? modelInputFile[0] : null;
  219. if (file) {
  220. uploadMessage = 'Uploading...';
  221. fileResponse = await uploadModel(localStorage.token, file, urlIdx).catch((error) => {
  222. toast.error(`${error}`);
  223. return null;
  224. });
  225. }
  226. } else {
  227. uploadProgress = 0;
  228. fileResponse = await downloadModel(localStorage.token, modelFileUrl, urlIdx).catch(
  229. (error) => {
  230. toast.error(`${error}`);
  231. return null;
  232. }
  233. );
  234. }
  235. if (fileResponse && fileResponse.ok) {
  236. const reader = fileResponse.body
  237. .pipeThrough(new TextDecoderStream())
  238. .pipeThrough(splitStream('\n'))
  239. .getReader();
  240. while (true) {
  241. const { value, done } = await reader.read();
  242. if (done) break;
  243. try {
  244. let lines = value.split('\n');
  245. for (const line of lines) {
  246. if (line !== '') {
  247. let data = JSON.parse(line.replace(/^data: /, ''));
  248. if (data.progress) {
  249. if (uploadMessage) {
  250. uploadMessage = '';
  251. }
  252. uploadProgress = data.progress;
  253. }
  254. if (data.error) {
  255. throw data.error;
  256. }
  257. if (data.done) {
  258. modelFileDigest = data.blob;
  259. name = data.name;
  260. uploaded = true;
  261. }
  262. }
  263. }
  264. } catch (error) {
  265. console.log(error);
  266. }
  267. }
  268. } else {
  269. const error = await fileResponse?.json();
  270. toast.error(error?.detail ?? error);
  271. }
  272. if (uploaded) {
  273. const res = await createModel(
  274. localStorage.token,
  275. `${name}:latest`,
  276. `FROM @${modelFileDigest}\n${modelFileContent}`
  277. );
  278. if (res && res.ok) {
  279. const reader = res.body
  280. .pipeThrough(new TextDecoderStream())
  281. .pipeThrough(splitStream('\n'))
  282. .getReader();
  283. while (true) {
  284. const { value, done } = await reader.read();
  285. if (done) break;
  286. try {
  287. let lines = value.split('\n');
  288. for (const line of lines) {
  289. if (line !== '') {
  290. console.log(line);
  291. let data = JSON.parse(line);
  292. console.log(data);
  293. if (data.error) {
  294. throw data.error;
  295. }
  296. if (data.detail) {
  297. throw data.detail;
  298. }
  299. if (data.status) {
  300. if (
  301. !data.digest &&
  302. !data.status.includes('writing') &&
  303. !data.status.includes('sha256')
  304. ) {
  305. toast.success(data.status);
  306. } else {
  307. if (data.digest) {
  308. digest = data.digest;
  309. if (data.completed) {
  310. pullProgress = Math.round((data.completed / data.total) * 1000) / 10;
  311. } else {
  312. pullProgress = 100;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. } catch (error) {
  320. console.log(error);
  321. toast.error(`${error}`);
  322. }
  323. }
  324. }
  325. }
  326. modelFileUrl = '';
  327. if (modelUploadInputElement) {
  328. modelUploadInputElement.value = '';
  329. }
  330. modelInputFile = null;
  331. modelTransferring = false;
  332. uploadProgress = null;
  333. models.set(await getModels(localStorage.token));
  334. };
  335. const deleteModelHandler = async () => {
  336. const res = await deleteModel(localStorage.token, deleteModelTag, urlIdx).catch((error) => {
  337. toast.error(`${error}`);
  338. });
  339. if (res) {
  340. toast.success($i18n.t(`Deleted {{deleteModelTag}}`, { deleteModelTag }));
  341. }
  342. deleteModelTag = '';
  343. models.set(await getModels(localStorage.token));
  344. };
  345. const cancelModelPullHandler = async (model: string) => {
  346. const { reader, abortController } = $MODEL_DOWNLOAD_POOL[model];
  347. if (abortController) {
  348. abortController.abort();
  349. }
  350. if (reader) {
  351. await reader.cancel();
  352. delete $MODEL_DOWNLOAD_POOL[model];
  353. MODEL_DOWNLOAD_POOL.set({
  354. ...$MODEL_DOWNLOAD_POOL
  355. });
  356. await deleteModel(localStorage.token, model);
  357. toast.success(`${model} download has been canceled`);
  358. }
  359. };
  360. const createModelHandler = async () => {
  361. createModelLoading = true;
  362. const res = await createModel(
  363. localStorage.token,
  364. createModelTag,
  365. createModelContent,
  366. urlIdx
  367. ).catch((error) => {
  368. toast.error(`${error}`);
  369. return null;
  370. });
  371. if (res && res.ok) {
  372. const reader = res.body
  373. .pipeThrough(new TextDecoderStream())
  374. .pipeThrough(splitStream('\n'))
  375. .getReader();
  376. while (true) {
  377. const { value, done } = await reader.read();
  378. if (done) break;
  379. try {
  380. let lines = value.split('\n');
  381. for (const line of lines) {
  382. if (line !== '') {
  383. console.log(line);
  384. let data = JSON.parse(line);
  385. console.log(data);
  386. if (data.error) {
  387. throw data.error;
  388. }
  389. if (data.detail) {
  390. throw data.detail;
  391. }
  392. if (data.status) {
  393. if (
  394. !data.digest &&
  395. !data.status.includes('writing') &&
  396. !data.status.includes('sha256')
  397. ) {
  398. toast.success(data.status);
  399. } else {
  400. if (data.digest) {
  401. createModelDigest = data.digest;
  402. if (data.completed) {
  403. createModelPullProgress =
  404. Math.round((data.completed / data.total) * 1000) / 10;
  405. } else {
  406. createModelPullProgress = 100;
  407. }
  408. }
  409. }
  410. }
  411. }
  412. }
  413. } catch (error) {
  414. console.log(error);
  415. toast.error(`${error}`);
  416. }
  417. }
  418. }
  419. models.set(await getModels(localStorage.token));
  420. createModelLoading = false;
  421. createModelTag = '';
  422. createModelContent = '';
  423. createModelDigest = '';
  424. createModelPullProgress = null;
  425. };
  426. const init = async () => {
  427. loading = true;
  428. ollamaModels = await getOllamaModels(localStorage.token, urlIdx);
  429. console.log(ollamaModels);
  430. loading = false;
  431. };
  432. $: if (urlIdx !== null) {
  433. init();
  434. }
  435. </script>
  436. <ModelDeleteConfirmDialog
  437. bind:show={showModelDeleteConfirm}
  438. on:confirm={() => {
  439. deleteModelHandler();
  440. }}
  441. />
  442. {#if !loading}
  443. <div class=" flex flex-col w-full">
  444. <div>
  445. <div class="space-y-2">
  446. <div>
  447. <div class=" mb-2 text-sm font-medium flex items-center gap-1.5">
  448. <div>
  449. {$i18n.t('Pull a model from Ollama.com')}
  450. </div>
  451. <div>
  452. <Tooltip content="Update All Models" placement="top">
  453. <button
  454. class="flex gap-2 items-center bg-transparent rounded-lg transition"
  455. on:click={() => {
  456. updateModelsHandler();
  457. }}
  458. >
  459. <svg
  460. xmlns="http://www.w3.org/2000/svg"
  461. viewBox="0 0 16 16"
  462. fill="currentColor"
  463. class="w-4 h-4"
  464. >
  465. <path
  466. d="M7 1a.75.75 0 0 1 .75.75V6h-1.5V1.75A.75.75 0 0 1 7 1ZM6.25 6v2.94L5.03 7.72a.75.75 0 0 0-1.06 1.06l2.5 2.5a.75.75 0 0 0 1.06 0l2.5-2.5a.75.75 0 1 0-1.06-1.06L7.75 8.94V6H10a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h2.25Z"
  467. />
  468. <path
  469. d="M4.268 14A2 2 0 0 0 6 15h6a2 2 0 0 0 2-2v-3a2 2 0 0 0-1-1.732V11a3 3 0 0 1-3 3H4.268Z"
  470. />
  471. </svg>
  472. </button>
  473. </Tooltip>
  474. </div>
  475. </div>
  476. <div class="flex w-full">
  477. <div class="flex-1 mr-2">
  478. <input
  479. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  480. placeholder={$i18n.t('Enter model tag (e.g. {{modelTag}})', {
  481. modelTag: 'mistral:7b'
  482. })}
  483. bind:value={modelTag}
  484. />
  485. </div>
  486. <button
  487. class="px-2.5 bg-gray-50 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
  488. on:click={() => {
  489. pullModelHandler();
  490. }}
  491. disabled={modelTransferring}
  492. >
  493. {#if modelTransferring}
  494. <div class="self-center">
  495. <svg
  496. class=" w-4 h-4"
  497. viewBox="0 0 24 24"
  498. fill="currentColor"
  499. xmlns="http://www.w3.org/2000/svg"
  500. >
  501. <style>
  502. .spinner_ajPY {
  503. transform-origin: center;
  504. animation: spinner_AtaB 0.75s infinite linear;
  505. }
  506. @keyframes spinner_AtaB {
  507. 100% {
  508. transform: rotate(360deg);
  509. }
  510. }
  511. </style>
  512. <path
  513. 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"
  514. opacity=".25"
  515. />
  516. <path
  517. 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"
  518. class="spinner_ajPY"
  519. />
  520. </svg>
  521. </div>
  522. {:else}
  523. <svg
  524. xmlns="http://www.w3.org/2000/svg"
  525. viewBox="0 0 16 16"
  526. fill="currentColor"
  527. class="w-4 h-4"
  528. >
  529. <path
  530. d="M8.75 2.75a.75.75 0 0 0-1.5 0v5.69L5.03 6.22a.75.75 0 0 0-1.06 1.06l3.5 3.5a.75.75 0 0 0 1.06 0l3.5-3.5a.75.75 0 0 0-1.06-1.06L8.75 8.44V2.75Z"
  531. />
  532. <path
  533. d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
  534. />
  535. </svg>
  536. {/if}
  537. </button>
  538. </div>
  539. <div class="mt-2 mb-1 text-xs text-gray-400 dark:text-gray-500">
  540. {$i18n.t('To access the available model names for downloading,')}
  541. <a
  542. class=" text-gray-500 dark:text-gray-300 font-medium underline"
  543. href="https://ollama.com/library"
  544. target="_blank">{$i18n.t('click here.')}</a
  545. >
  546. </div>
  547. {#if updateModelId}
  548. <div class="text-xs">
  549. Updating "{updateModelId}" {updateProgress ? `(${updateProgress}%)` : ''}
  550. </div>
  551. {/if}
  552. {#if Object.keys($MODEL_DOWNLOAD_POOL).length > 0}
  553. {#each Object.keys($MODEL_DOWNLOAD_POOL) as model}
  554. {#if 'pullProgress' in $MODEL_DOWNLOAD_POOL[model]}
  555. <div class="flex flex-col">
  556. <div class="font-medium mb-1">{model}</div>
  557. <div class="">
  558. <div class="flex flex-row justify-between space-x-4 pr-2">
  559. <div class=" flex-1">
  560. <div
  561. class="dark:bg-gray-600 bg-gray-500 text-xs font-medium text-gray-100 text-center p-0.5 leading-none rounded-full"
  562. style="width: {Math.max(
  563. 15,
  564. $MODEL_DOWNLOAD_POOL[model].pullProgress ?? 0
  565. )}%"
  566. >
  567. {$MODEL_DOWNLOAD_POOL[model].pullProgress ?? 0}%
  568. </div>
  569. </div>
  570. <Tooltip content={$i18n.t('Cancel')}>
  571. <button
  572. class="text-gray-800 dark:text-gray-100"
  573. on:click={() => {
  574. cancelModelPullHandler(model);
  575. }}
  576. >
  577. <svg
  578. class="w-4 h-4 text-gray-800 dark:text-white"
  579. aria-hidden="true"
  580. xmlns="http://www.w3.org/2000/svg"
  581. width="24"
  582. height="24"
  583. fill="currentColor"
  584. viewBox="0 0 24 24"
  585. >
  586. <path
  587. stroke="currentColor"
  588. stroke-linecap="round"
  589. stroke-linejoin="round"
  590. stroke-width="2"
  591. d="M6 18 17.94 6M18 18 6.06 6"
  592. />
  593. </svg>
  594. </button>
  595. </Tooltip>
  596. </div>
  597. {#if 'digest' in $MODEL_DOWNLOAD_POOL[model]}
  598. <div class="mt-1 text-xs dark:text-gray-500" style="font-size: 0.5rem;">
  599. {$MODEL_DOWNLOAD_POOL[model].digest}
  600. </div>
  601. {/if}
  602. </div>
  603. </div>
  604. {/if}
  605. {/each}
  606. {/if}
  607. </div>
  608. <div>
  609. <div class=" mb-2 text-sm font-medium">{$i18n.t('Delete a model')}</div>
  610. <div class="flex w-full">
  611. <div
  612. class="flex-1 mr-2 pr-1.5 rounded-lg bg-gray-50 dark:text-gray-300 dark:bg-gray-850"
  613. >
  614. <select
  615. class="w-full py-2 px-4 text-sm outline-none bg-transparent"
  616. bind:value={deleteModelTag}
  617. placeholder={$i18n.t('Select a model')}
  618. >
  619. {#if !deleteModelTag}
  620. <option value="" disabled selected>{$i18n.t('Select a model')}</option>
  621. {/if}
  622. {#each ollamaModels as model}
  623. <option value={model.id} class="bg-gray-50 dark:bg-gray-700"
  624. >{model.name + ' (' + (model.size / 1024 ** 3).toFixed(1) + ' GB)'}</option
  625. >
  626. {/each}
  627. </select>
  628. </div>
  629. <button
  630. class="px-2.5 bg-gray-50 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
  631. on:click={() => {
  632. showModelDeleteConfirm = true;
  633. }}
  634. >
  635. <svg
  636. xmlns="http://www.w3.org/2000/svg"
  637. viewBox="0 0 16 16"
  638. fill="currentColor"
  639. class="w-4 h-4"
  640. >
  641. <path
  642. fill-rule="evenodd"
  643. d="M5 3.25V4H2.75a.75.75 0 0 0 0 1.5h.3l.815 8.15A1.5 1.5 0 0 0 5.357 15h5.285a1.5 1.5 0 0 0 1.493-1.35l.815-8.15h.3a.75.75 0 0 0 0-1.5H11v-.75A2.25 2.25 0 0 0 8.75 1h-1.5A2.25 2.25 0 0 0 5 3.25Zm2.25-.75a.75.75 0 0 0-.75.75V4h3v-.75a.75.75 0 0 0-.75-.75h-1.5ZM6.05 6a.75.75 0 0 1 .787.713l.275 5.5a.75.75 0 0 1-1.498.075l-.275-5.5A.75.75 0 0 1 6.05 6Zm3.9 0a.75.75 0 0 1 .712.787l-.275 5.5a.75.75 0 0 1-1.498-.075l.275-5.5a.75.75 0 0 1 .786-.711Z"
  644. clip-rule="evenodd"
  645. />
  646. </svg>
  647. </button>
  648. </div>
  649. </div>
  650. <div>
  651. <div class=" mb-2 text-sm font-medium">{$i18n.t('Create a model')}</div>
  652. <div class="flex w-full">
  653. <div class="flex-1 mr-2 flex flex-col gap-2">
  654. <input
  655. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  656. placeholder={$i18n.t('Enter model tag (e.g. {{modelTag}})', {
  657. modelTag: 'my-modelfile'
  658. })}
  659. bind:value={createModelTag}
  660. disabled={createModelLoading}
  661. />
  662. <textarea
  663. bind:value={createModelContent}
  664. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-100 dark:bg-gray-850 outline-none resize-none scrollbar-hidden"
  665. rows="6"
  666. placeholder={`TEMPLATE """{{ .System }}\nUSER: {{ .Prompt }}\nASSISTANT: """\nPARAMETER num_ctx 4096\nPARAMETER stop "</s>"\nPARAMETER stop "USER:"\nPARAMETER stop "ASSISTANT:"`}
  667. disabled={createModelLoading}
  668. />
  669. </div>
  670. <div class="flex self-start">
  671. <button
  672. class="px-2.5 py-2.5 bg-gray-50 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition disabled:cursor-not-allowed"
  673. on:click={() => {
  674. createModelHandler();
  675. }}
  676. disabled={createModelLoading}
  677. >
  678. <svg
  679. xmlns="http://www.w3.org/2000/svg"
  680. viewBox="0 0 16 16"
  681. fill="currentColor"
  682. class="size-4"
  683. >
  684. <path
  685. d="M7.25 10.25a.75.75 0 0 0 1.5 0V4.56l2.22 2.22a.75.75 0 1 0 1.06-1.06l-3.5-3.5a.75.75 0 0 0-1.06 0l-3.5 3.5a.75.75 0 0 0 1.06 1.06l2.22-2.22v5.69Z"
  686. />
  687. <path
  688. d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
  689. />
  690. </svg>
  691. </button>
  692. </div>
  693. </div>
  694. {#if createModelDigest !== ''}
  695. <div class="flex flex-col mt-1">
  696. <div class="font-medium mb-1">{createModelTag}</div>
  697. <div class="">
  698. <div class="flex flex-row justify-between space-x-4 pr-2">
  699. <div class=" flex-1">
  700. <div
  701. class="dark:bg-gray-600 bg-gray-500 text-xs font-medium text-gray-100 text-center p-0.5 leading-none rounded-full"
  702. style="width: {Math.max(15, createModelPullProgress ?? 0)}%"
  703. >
  704. {createModelPullProgress ?? 0}%
  705. </div>
  706. </div>
  707. </div>
  708. {#if createModelDigest}
  709. <div class="mt-1 text-xs dark:text-gray-500" style="font-size: 0.5rem;">
  710. {createModelDigest}
  711. </div>
  712. {/if}
  713. </div>
  714. </div>
  715. {/if}
  716. </div>
  717. <div class="pt-1">
  718. <div class="flex justify-between items-center text-xs">
  719. <div class=" text-sm font-medium">{$i18n.t('Experimental')}</div>
  720. <button
  721. class=" text-xs font-medium text-gray-500"
  722. type="button"
  723. on:click={() => {
  724. showExperimentalOllama = !showExperimentalOllama;
  725. }}>{showExperimentalOllama ? $i18n.t('Hide') : $i18n.t('Show')}</button
  726. >
  727. </div>
  728. </div>
  729. {#if showExperimentalOllama}
  730. <form
  731. on:submit|preventDefault={() => {
  732. uploadModelHandler();
  733. }}
  734. >
  735. <div class=" mb-2 flex w-full justify-between">
  736. <div class=" text-sm font-medium">{$i18n.t('Upload a GGUF model')}</div>
  737. <button
  738. class="p-1 px-3 text-xs flex rounded transition"
  739. on:click={() => {
  740. if (modelUploadMode === 'file') {
  741. modelUploadMode = 'url';
  742. } else {
  743. modelUploadMode = 'file';
  744. }
  745. }}
  746. type="button"
  747. >
  748. {#if modelUploadMode === 'file'}
  749. <span class="ml-2 self-center">{$i18n.t('File Mode')}</span>
  750. {:else}
  751. <span class="ml-2 self-center">{$i18n.t('URL Mode')}</span>
  752. {/if}
  753. </button>
  754. </div>
  755. <div class="flex w-full mb-1.5">
  756. <div class="flex flex-col w-full">
  757. {#if modelUploadMode === 'file'}
  758. <div class="flex-1 {modelInputFile && modelInputFile.length > 0 ? 'mr-2' : ''}">
  759. <input
  760. id="model-upload-input"
  761. bind:this={modelUploadInputElement}
  762. type="file"
  763. bind:files={modelInputFile}
  764. on:change={() => {
  765. console.log(modelInputFile);
  766. }}
  767. accept=".gguf,.safetensors"
  768. required
  769. hidden
  770. />
  771. <button
  772. type="button"
  773. class="w-full rounded-lg text-left py-2 px-4 bg-gray-50 dark:text-gray-300 dark:bg-gray-850"
  774. on:click={() => {
  775. modelUploadInputElement.click();
  776. }}
  777. >
  778. {#if modelInputFile && modelInputFile.length > 0}
  779. {modelInputFile[0].name}
  780. {:else}
  781. {$i18n.t('Click here to select')}
  782. {/if}
  783. </button>
  784. </div>
  785. {:else}
  786. <div class="flex-1 {modelFileUrl !== '' ? 'mr-2' : ''}">
  787. <input
  788. class="w-full rounded-lg text-left py-2 px-4 bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none {modelFileUrl !==
  789. ''
  790. ? 'mr-2'
  791. : ''}"
  792. type="url"
  793. required
  794. bind:value={modelFileUrl}
  795. placeholder={$i18n.t('Type Hugging Face Resolve (Download) URL')}
  796. />
  797. </div>
  798. {/if}
  799. </div>
  800. {#if (modelUploadMode === 'file' && modelInputFile && modelInputFile.length > 0) || (modelUploadMode === 'url' && modelFileUrl !== '')}
  801. <button
  802. class="px-2.5 bg-gray-50 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg disabled:cursor-not-allowed transition"
  803. type="submit"
  804. disabled={modelTransferring}
  805. >
  806. {#if modelTransferring}
  807. <div class="self-center">
  808. <svg
  809. class=" w-4 h-4"
  810. viewBox="0 0 24 24"
  811. fill="currentColor"
  812. xmlns="http://www.w3.org/2000/svg"
  813. >
  814. <style>
  815. .spinner_ajPY {
  816. transform-origin: center;
  817. animation: spinner_AtaB 0.75s infinite linear;
  818. }
  819. @keyframes spinner_AtaB {
  820. 100% {
  821. transform: rotate(360deg);
  822. }
  823. }
  824. </style>
  825. <path
  826. 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"
  827. opacity=".25"
  828. />
  829. <path
  830. 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"
  831. class="spinner_ajPY"
  832. />
  833. </svg>
  834. </div>
  835. {:else}
  836. <svg
  837. xmlns="http://www.w3.org/2000/svg"
  838. viewBox="0 0 16 16"
  839. fill="currentColor"
  840. class="w-4 h-4"
  841. >
  842. <path
  843. d="M7.25 10.25a.75.75 0 0 0 1.5 0V4.56l2.22 2.22a.75.75 0 1 0 1.06-1.06l-3.5-3.5a.75.75 0 0 0-1.06 0l-3.5 3.5a.75.75 0 0 0 1.06 1.06l2.22-2.22v5.69Z"
  844. />
  845. <path
  846. d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
  847. />
  848. </svg>
  849. {/if}
  850. </button>
  851. {/if}
  852. </div>
  853. {#if (modelUploadMode === 'file' && modelInputFile && modelInputFile.length > 0) || (modelUploadMode === 'url' && modelFileUrl !== '')}
  854. <div>
  855. <div>
  856. <div class=" my-2.5 text-sm font-medium">
  857. {$i18n.t('Modelfile Content')}
  858. </div>
  859. <textarea
  860. bind:value={modelFileContent}
  861. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-100 dark:bg-gray-850 outline-none resize-none"
  862. rows="6"
  863. />
  864. </div>
  865. </div>
  866. {/if}
  867. <div class=" mt-1 text-xs text-gray-400 dark:text-gray-500">
  868. {$i18n.t('To access the GGUF models available for downloading,')}
  869. <a
  870. class=" text-gray-500 dark:text-gray-300 font-medium underline"
  871. href="https://huggingface.co/models?search=gguf"
  872. target="_blank">{$i18n.t('click here.')}</a
  873. >
  874. </div>
  875. {#if uploadMessage}
  876. <div class="mt-2">
  877. <div class=" mb-2 text-xs">{$i18n.t('Upload Progress')}</div>
  878. <div class="w-full rounded-full dark:bg-gray-800">
  879. <div
  880. class="dark:bg-gray-600 bg-gray-500 text-xs font-medium text-gray-100 text-center p-0.5 leading-none rounded-full"
  881. style="width: 100%"
  882. >
  883. {uploadMessage}
  884. </div>
  885. </div>
  886. <div class="mt-1 text-xs dark:text-gray-500" style="font-size: 0.5rem;">
  887. {modelFileDigest}
  888. </div>
  889. </div>
  890. {:else if uploadProgress !== null}
  891. <div class="mt-2">
  892. <div class=" mb-2 text-xs">{$i18n.t('Upload Progress')}</div>
  893. <div class="w-full rounded-full dark:bg-gray-800">
  894. <div
  895. class="dark:bg-gray-600 bg-gray-500 text-xs font-medium text-gray-100 text-center p-0.5 leading-none rounded-full"
  896. style="width: {Math.max(15, uploadProgress ?? 0)}%"
  897. >
  898. {uploadProgress ?? 0}%
  899. </div>
  900. </div>
  901. <div class="mt-1 text-xs dark:text-gray-500" style="font-size: 0.5rem;">
  902. {modelFileDigest}
  903. </div>
  904. </div>
  905. {/if}
  906. </form>
  907. {/if}
  908. </div>
  909. </div>
  910. </div>
  911. {:else}
  912. <Spinner />
  913. {/if}