import{j as e,a_ as B,cO as N,a as l,r as d,ji as Q,u as D,b0 as A,am as K,b2 as V,cR as U,c as C,aT as g,bc as k,aG as $,b1 as R,jj as W,aW as Y,e as w,fO as q,fR as z,c8 as Z,fP as I,fM as L,cr as J,be as G,f as b,bw as H,f$ as X,dF as ee,O as se,hp as te,fZ as P,bq as ne,br as M,K as E}from"./index-d6499a22.js";import{C as _}from"./CardBoundary-898d91f9.js";import{s as oe,M as re}from"./modelVersion.selector-975bf538.js";import{P as ie}from"./Panel-b19e8cc0.js";import{D as le,s as ae}from"./DateValue-11d0b0dc.js";import{d as ce}from"./dumbPluralize-86a9b9d6.js";import{g as de,e as me}from"./utils-14352779.js";import{u as he}from"./useCurrentProject-b23444be.js";import{g as pe,b as ue,a as xe}from"./getDefaultFormValues-84b0021f.js";import"./NoData-789df290.js";import"./Deprecated-ec8916ed.js";import"./CardActionButton-ccf1bc35.js";import"./useProject-0d0fdeb3.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})
`,fe=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)
`},ge=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:ge(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:fe(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}]=Q(),{id:y,vId:t,mId:j}=D(),a=A(),p=K(),S=V(U),v=()=>r(h=>!h),T=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(g,{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,f)=>e.jsx($,{defaultValue:u,placeholder:"enter the value",width:"100%",my:"2px",...p.register(`${s.name}.${h}.${f}.value`)},f)):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(g,{intent:"secondary",mr:"20px",onClick:v,children:"Cancel"}),e.jsx(g,{onClick:T,disabled:x,children:"Save"})]})]})},Se=()=>{const{vId:s,mId:n,id:o}=D(),r=R(),i=A(),x=K(),{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(g,{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}=q(),{data:y,isLoading:t}=z(),{isServiceUser:j}=Z(),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 I.TENSORFLOW;case"SKLEARN":return I.SKLEARN;default:return I.PYTHON}return I.PYTHON},[s==null?void 0:s.framework]),S=d.useCallback(()=>s!=null&&s.framework?s.framework===I.TENSORFLOW?L.TENSORFLOW_SERVING:L.PYTHON:L.PYTHON,[s.framework]),v=d.useCallback(()=>{const{kafkaTopic:T,inferenceLogging:h,modelInput:m,predictions:u}=ue(t,j),f=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:T,inferenceLogging:h,modelInput:m,predictions:u,requestedInstances:f}})},[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(G,{mb:"3px",gray:!0,children:"Framework"}),e.jsx(b,{children:s!=null&&s.framework?s.framework:"No framework"})]}),e.jsxs(l,{children:[e.jsx(g,{intent:"secondary",onClick:a,mr:"8px",children:"Browse model files ->"}),e.jsx(g,{onClick:v,children:"Deploy this version ->"})]})]})},Ce=d.memo(be),Te=({metrics:s})=>e.jsx(l,{children:s?Object.entries(s).map(([n,o])=>e.jsxs(l,{flexDirection:"column",mr:"20px",children:[e.jsx(G,{mb:"3px",gray:!0,children:n}),e.jsx(b,{color:"black",children:o})]},n)):e.jsx(H,{children:"No metrics found for this version"})}),Ie=d.memo(Te),Fe=()=>{var u,f;const{id:s,mId:n,vId:o}=D(),r=R(),i=A(),{data:x}=V(ae(n)),{isLoading:y,data:t}=V(oe),{isLoading:j,data:a}=X({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,O)=>()=>{r(w.deployments.overview,{dId:O,mId:c})},v=()=>{r(w.models.overview,{mId:n})},T=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(O=>O.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:T,onClickEdit:m,idColor:"labels.blueShade1",children:e.jsx(g,{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(H,{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(M,{name:"modelMetrics",children:e.jsx(_,{children:e.jsx(E,{title:"Model metrics",maxHeight:"400px",children:e.jsx(Ie,{metrics:t.metrics})})})}),e.jsx(M,{name:"modelSchema",children:e.jsx(_,{children:e.jsx(E,{title:"Model schema",contentProps:F,children:e.jsx(re,{schema:(f=(u=t.modelSchema)==null?void 0:u.preview)==null?void 0:f.content})})})}),e.jsx(M,{name:"modelTags",children:e.jsx(_,{children:e.jsx(E,{title:"Tags",actions:e.jsx(g,{p:0,intent:"inline",onClick:m,children:"edit"}),children:e.jsx(Se,{})})})}),e.jsx(M,{name:"codeSnippets",children:e.jsx(_,{children:e.jsx(E,{title:"Code snippets",contentProps:F,children:e.jsx(ve,{model:t})})})})]})})]})};export{Fe as default};
