import{j as e,a$ as B,cR as N,Q as l,r as d,jg as G,u as D,b1 as L,an as H,b3 as A,cU as K,x as C,aU as f,bd as k,aH as $,b2 as R,jh as W,aX as Y,b as w,fU as z,fX as q,c9 as X,fV as T,fS as O,cs as J,bf as U,g as b,bx as Q,g4 as Z,dJ as ee,o as se,hp as te,g2 as P,br as ne,bs as E,E as M}from"./index-ea7817a2.js";import{C as _}from"./CardBoundary-ba893294.js";import{s as oe,M as re}from"./modelVersion.selector-d59012b3.js";import{P as ie}from"./Panel-041c5d61.js";import{D as le,s as ae}from"./DateValue-75b0734c.js";import{d as ce}from"./dumbPluralize-86a9b9d6.js";import{g as de,e as me}from"./utils-cf9ea4a1.js";import{u as he}from"./useCurrentProject-0132f110.js";import{g as pe,b as ue,a as xe}from"./getDefaultFormValues-84b0021f.js";import"./NoData-8733f606.js";import"./Deprecated-9bfe2e44.js";import"./CardActionButton-6aa39bc1.js";import"./useProject-77619fff.js";const je=s=>`import hsml

# connect with Hopsworks
conn = hsml.connection()

# get Hopsworks Model Registry
mr = conn.get_model_registry(project="${s.projectName}")

# get model object
model = mr.get_model("${s.modelName}", version=${s.modelVersion})
`,ge=s=>{if(!s.metrics)return"";const n=Object.keys(s.metrics);return`EVALUATION_METRIC="${n.shift()}"  ${n.length?`# or ${n.join(", ")}`:""}
SORT_METRICS_BY="max" # your sorting criteria

# get best model based on custom metrics
best_model = mr.get_best_model("${s.modelName}",
                               EVALUATION_METRIC,
                               SORT_METRICS_BY)
`},fe=s=>`# get the model you want to download
model = mr.get_model("${s.modelName}", version=${s.modelVersion})

# download the model artifact
model.download()
`,ye=s=>`from hsml.schema import Schema
from hsml.model_schema import ModelSchema

SAVED_MODEL_PATH = "path-to-the-new-model-files"

x_df = [] # your training data (Pandas, numpy, tensors,...)
y_df = [] # your labels

input_schema = Schema(x_df)
output_schema = Schema(y_df)

# create the model metadata for a python model (or tensorflow, sklearn, torch...)
model = mr.python.create_model("${s.modelName}", 
                               metrics={EVALUATION_METRIC: EVALUATION_METRIC_VALUE},
                               input_example=x_df,
                               model_schema=ModelSchema(input_schema=input_schema,
                                                        output_schema=output_schema))

# save model version in the Model Registry
model.save(SAVED_MODEL_PATH)
`,ve=({model:s})=>{const{isLoading:n,project:o}=he();if(n||!o)return e.jsx(B,{});const r={projectName:o==null?void 0:o.projectName,modelName:s.name,modelVersion:s.version,metrics:s.metrics},i=e.jsx(N,{copyButton:!0,title:"Download model",content:fe(r),language:"python"});return e.jsxs(l,{alignItems:"flex-start",sx:{gap:"20px"},p:"20px",children:[e.jsxs(l,{flexDirection:"column",sx:{gap:"20px"},width:"50%",children:[e.jsx(N,{copyButton:!0,title:"Get model",content:je(r),language:"python"}),s.metrics&&Object.keys(s.metrics).length>0?e.jsx(N,{copyButton:!0,title:"Get best model based on custom metrics",content:ge(r),language:"python"}):i]}),e.jsxs(l,{flexDirection:"column",sx:{gap:"20px"},width:"50%",children:[e.jsx(N,{copyButton:!0,title:"Train a new version of the model",content:ye(r),language:"python"}),s.metrics&&i]})]})},we=({tag:s,refetch:n})=>{const[o,r]=d.useState(!1),[i,{isLoading:x}]=G(),{id:y,vId:t,mId:j}=D(),a=L(),p=H(),S=A(K),v=()=>r(h=>!h),I=async()=>{const h=p.getValues()||[],m=de(h,S);try{await Promise.all(m.map(async u=>await i({modelId:`${j}_${t}`,tagName:u.schema,data:u.value,projectId:y}))),v(),n()}catch(u){console.warn(u)}};return d.useEffect(()=>{a.schematisedTags.fetch()},[a]),e.jsxs(l,{flexDirection:"column",mb:"20px",children:[e.jsxs(l,{justifyContent:"space-between",alignItems:"center",children:[e.jsx(C,{children:s.name}),e.jsx(f,{px:0,intent:"inline",onClick:v,children:"edit properties"})]}),e.jsx(k,{hoverable:!1,padded:!1,children:Object.entries((s==null?void 0:s.tags)||[]).map(([h,m])=>e.jsxs(k.Row,{height:"42px",children:[e.jsx(k.Cell,{width:"10%",children:e.jsx(C,{gray:!0,children:h})}),e.jsx(k.Cell,{width:"90%",px:0,py:"2px",children:o?e.jsx(l,{flexDirection:"column",children:Array.isArray(m)?m.map((u,g)=>e.jsx($,{defaultValue:u,placeholder:"enter the value",width:"100%",my:"2px",...p.register(`${s.name}.${h}.${g}.value`)},g)):e.jsx($,{defaultValue:m,placeholder:"enter the value",width:"100%",...p.register(`${s.name}.${h}`)})}):e.jsx(C,{children:Array.isArray(m)?m.join(", "):m.toString()})})]},h))}),o&&e.jsxs(l,{pt:"20px",justifyContent:"flex-end",children:[e.jsx(f,{intent:"secondary",mr:"20px",onClick:v,children:"Cancel"}),e.jsx(f,{onClick:I,disabled:x,children:"Save"})]})]})},Se=()=>{const{vId:s,mId:n,id:o}=D(),r=R(),i=L(),x=H(),{data:y=[],refetch:t}=W({modelId:`${n}_${s}`,projectId:o}),j=()=>r(w.models.versionEdit,{mId:n,vId:s});return d.useEffect(()=>{i.schematisedTags.fetch()},[i]),e.jsx(Y,{...x,children:e.jsxs(l,{flexDirection:"column",children:[y.map(a=>e.jsx(we,{tag:a,refetch:t},a.name)),!y.length&&e.jsxs(l,{my:"20px",justifyContent:"center",alignItems:"center",flexDirection:"column",children:[e.jsx(C,{gray:!0,fontSize:"18px",children:"There are no tags attached"}),e.jsx(f,{mt:"20px",onClick:j,children:"Add tags"})]})]})})},F={overflow:"auto",padding:0},be=({data:s})=>{const{mId:n,vId:o}=D(),r=R(),{data:i,isLoading:x}=z(),{data:y,isLoading:t}=q(),{isServiceUser:j}=X(),a=d.useCallback(()=>{r(w.project.settings.fileBrowserWithPath,{"*":`Models/${n}/${o}`})},[n,o,r]),p=d.useCallback(()=>{if(s!=null&&s.framework)switch(s==null?void 0:s.framework){case"TENSORFLOW":return T.TENSORFLOW;case"SKLEARN":return T.SKLEARN;default:return T.PYTHON}return T.PYTHON},[s==null?void 0:s.framework]),S=d.useCallback(()=>s!=null&&s.framework?s.framework===T.TENSORFLOW?O.TENSORFLOW_SERVING:O.PYTHON:O.PYTHON,[s.framework]),v=d.useCallback(()=>{const{kafkaTopic:I,inferenceLogging:h,modelInput:m,predictions:u}=ue(t,j),g=xe(x,j,i);r(w.deployments.create,void 0,{state:{modelFramework:p(),modelServer:S(),kserve:pe(x,j,i),modelName:n,modelVersion:+o,kafkaTopicDTO:I,inferenceLogging:h,modelInput:m,predictions:u,requestedInstances:g}})},[r,p,S,n,o,x,j,i,t]);return e.jsxs(l,{children:[e.jsx(J,{firstName:s.userFullName.split(" ")[0],lastName:s.userFullName.split(" ")[1]}),e.jsx(le,{ml:"25px",label:"Version created on",date:s.created}),e.jsxs(l,{flexDirection:"column",ml:"30px",flex:"1",children:[e.jsx(U,{mb:"3px",gray:!0,children:"Framework"}),e.jsx(b,{children:s!=null&&s.framework?s.framework:"No framework"})]}),e.jsxs(l,{children:[e.jsx(f,{intent:"secondary",onClick:a,mr:"8px",children:"Browse model files ->"}),e.jsx(f,{onClick:v,children:"Deploy this version ->"})]})]})},Ce=d.memo(be),Ie=({metrics:s})=>e.jsx(l,{children:s?Object.entries(s).map(([n,o])=>e.jsxs(l,{flexDirection:"column",mr:"20px",children:[e.jsx(U,{mb:"3px",gray:!0,children:n}),e.jsx(b,{color:"black",children:o})]},n)):e.jsx(Q,{children:"No metrics found for this version"})}),Te=d.memo(Ie),Fe=()=>{var u,g;const{id:s,mId:n,vId:o}=D(),r=R(),i=L(),{data:x}=A(ae(n)),{isLoading:y,data:t}=A(oe),{isLoading:j,data:a}=Z({projectId:s,model:n});d.useEffect(()=>{i.modelView.fetch({projectId:+s,modelName:n})},[i,s,n]),d.useEffect(()=>(i.modelVersion.fetch({projectId:+s,modelName:n,modelVersion:+o}),()=>{i.modelVersion.clear()}),[i,s,n,o]);const p=d.useMemo(()=>((a==null?void 0:a.all)??[]).filter(c=>c.modelVersion===(t==null?void 0:t.version)).sort(me),[t,a]),S=(c,V)=>()=>{r(w.deployments.overview,{dId:V,mId:c})},v=()=>{r(w.models.overview,{mId:n})},I=c=>{r(w.models.versionView,{vId:Number.parseInt(c,10),mId:(t==null?void 0:t.name)||""})},h=d.useMemo(()=>x.map(c=>({id:Number(c.version),version:Number(c.version),deployments:((a==null?void 0:a.all)??[]).filter(V=>V.modelVersion===c.version).length})),[a,x]),m=d.useCallback(()=>{r(w.models.versionEdit,{mId:(t==null?void 0:t.name)||"",vId:(t==null?void 0:t.version)||""})},[t,r]);return y||j||!t?e.jsx(B,{}):e.jsxs(e.Fragment,{children:[e.jsx(ie,{versionHasSearch:!0,type:ee.modelVersion,data:t,title:t.name,version:t.version,versions:h,onVersionChange:I,onClickEdit:m,idColor:"labels.blueShade1",children:e.jsx(f,{intent:"secondary",onClick:v,ml:"5px",children:e.jsx(C,{bold:!0,children:"all versions ->"})})}),e.jsx(se,{mt:"10px",width:"100%",children:e.jsx(Ce,{data:t})}),e.jsxs(l,{mt:"20px",width:"100%",flexWrap:"wrap",sx:{gap:"8px"},children:[e.jsx(b,{sx:{flex:"1 1 100%"},children:t.description?e.jsx(Q,{my:"20px",children:(t==null?void 0:t.description)||"-"}):e.jsx(C,{my:"20px",gray:!0,children:"-"})}),e.jsx(b,{sx:{flex:"1 1 100%"},children:p.length>0?e.jsxs(b,{children:["This version is used in ",e.jsx(b,{primary:!0,display:"inline",children:p.length}),ce(" deployment",p.length)]}):"This version is not used in any deployments"}),e.jsx(l,{sx:{gap:"6px"},children:p.map(c=>e.jsx(te,{height:"31px",isActive:c.status===P.RUNNING,indicator:"horizontal",mode:c.status===P.RUNNING?"highlight":"default",onClick:S(t.name,c.id),children:`${c.name} ->`},c.id))})]}),e.jsx(l,{flexDirection:"column",sx:{gap:"20px"},py:"20px",children:e.jsxs(ne,{children:[e.jsx(E,{name:"modelMetrics",children:e.jsx(_,{children:e.jsx(M,{title:"Model metrics",maxHeight:"400px",children:e.jsx(Te,{metrics:t.metrics})})})}),e.jsx(E,{name:"modelSchema",children:e.jsx(_,{children:e.jsx(M,{title:"Model schema",contentProps:F,children:e.jsx(re,{schema:(g=(u=t.modelSchema)==null?void 0:u.preview)==null?void 0:g.content})})})}),e.jsx(E,{name:"modelTags",children:e.jsx(_,{children:e.jsx(M,{title:"Tags",actions:e.jsx(f,{p:0,intent:"inline",onClick:m,children:"edit"}),children:e.jsx(Se,{})})})}),e.jsx(E,{name:"codeSnippets",children:e.jsx(_,{children:e.jsx(M,{title:"Code snippets",contentProps:F,children:e.jsx(ve,{model:t})})})})]})})]})};export{Fe as default};
