From 21c7f507908fe0bd49baf9575f35f16a95aa3f2f Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 26 Dec 2023 23:58:40 -0800 Subject: [PATCH] feat: modelfiles migration support --- backend/apps/web/models/modelfiles.py | 13 ++- backend/apps/web/routers/modelfiles.py | 19 +++- src/lib/apis/modelfiles/index.ts | 2 +- src/routes/(app)/modelfiles/+page.svelte | 105 +++++++++++++++++- .../(app)/modelfiles/create/+page.svelte | 6 - 5 files changed, 124 insertions(+), 21 deletions(-) diff --git a/backend/apps/web/models/modelfiles.py b/backend/apps/web/models/modelfiles.py index a0f9f1aa..4d8202db 100644 --- a/backend/apps/web/models/modelfiles.py +++ b/backend/apps/web/models/modelfiles.py @@ -74,11 +74,16 @@ class ModelfilesTable: "timestamp": int(time.time()), } ) - result = Modelfile.create(**modelfile.model_dump()) - if result: - return modelfile - else: + + try: + result = Modelfile.create(**modelfile.model_dump()) + if result: + return modelfile + else: + return None + except: return None + else: return None diff --git a/backend/apps/web/routers/modelfiles.py b/backend/apps/web/routers/modelfiles.py index 4563c4df..dd1f6cc5 100644 --- a/backend/apps/web/routers/modelfiles.py +++ b/backend/apps/web/routers/modelfiles.py @@ -56,12 +56,19 @@ async def create_new_modelfile(form_data: ModelfileForm, cred=Depends(bearer_sch # Admin Only if user.role == "admin": modelfile = Modelfiles.insert_new_modelfile(user.id, form_data) - return ModelfileResponse( - **{ - **modelfile.model_dump(), - "modelfile": json.loads(modelfile.modelfile), - } - ) + + if modelfile: + return ModelfileResponse( + **{ + **modelfile.model_dump(), + "modelfile": json.loads(modelfile.modelfile), + } + ) + else: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.DEFAULT(), + ) else: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, diff --git a/src/lib/apis/modelfiles/index.ts b/src/lib/apis/modelfiles/index.ts index ad72eec9..91af5e38 100644 --- a/src/lib/apis/modelfiles/index.ts +++ b/src/lib/apis/modelfiles/index.ts @@ -19,7 +19,7 @@ export const createNewModelfile = async (token: string, modelfile: object) => { return res.json(); }) .catch((err) => { - error = err; + error = err.detail; console.log(err); return null; }); diff --git a/src/routes/(app)/modelfiles/+page.svelte b/src/routes/(app)/modelfiles/+page.svelte index 2a8f12ab..41691524 100644 --- a/src/routes/(app)/modelfiles/+page.svelte +++ b/src/routes/(app)/modelfiles/+page.svelte @@ -1,11 +1,20 @@
@@ -175,6 +199,79 @@
{/each} + {#if localModelfiles.length > 0} +
+ +
+
+ {localModelfiles.length} Local Modelfiles Detected +
+ +
+ + + +
+
+ {/if} +
Made by OllamaHub Community
diff --git a/src/routes/(app)/modelfiles/create/+page.svelte b/src/routes/(app)/modelfiles/create/+page.svelte index 96d742b0..506edb9f 100644 --- a/src/routes/(app)/modelfiles/create/+page.svelte +++ b/src/routes/(app)/modelfiles/create/+page.svelte @@ -95,12 +95,6 @@ SYSTEM """${system}"""`.replace(/^\s*\n/gm, ''); }; const saveModelfile = async (modelfile) => { - // await modelfiles.set([ - // ...$modelfiles.filter((m) => m.tagName !== modelfile.tagName), - // modelfile - // ]); - // localStorage.setItem('modelfiles', JSON.stringify($modelfiles)); - await createNewModelfile(localStorage.token, modelfile); await modelfiles.set(await getModelfiles(localStorage.token)); };