Skip to content

Commit

Permalink
feat: add more options for cortex popup (#3236)
Browse files Browse the repository at this point in the history
update
  • Loading branch information
namchuai authored Aug 5, 2024
1 parent 37a3c4f commit 5e06ed8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 38 deletions.
28 changes: 22 additions & 6 deletions web/screens/HubScreen2/components/DownloadLocalModelModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, useEffect, useState } from 'react'
import { Fragment, useEffect, useState, useCallback } from 'react'

import { Modal } from '@janhq/joi'
import { useAtom } from 'jotai'
Expand All @@ -13,6 +13,7 @@ import Tab, { ModelTab } from './Tab'
import { localModelModalStageAtom } from '@/helpers/atoms/DownloadLocalModel.atom'

const DownloadLocalModelModal: React.FC = () => {
const [availableModels, setAvailableModels] = useState<string[]>([])
const [{ stage, modelHandle }, setLocalModelModalStage] = useAtom(
localModelModalStageAtom
)
Expand All @@ -31,9 +32,21 @@ const DownloadLocalModelModal: React.FC = () => {
}, [])

const modelName = modelHandle?.split('/')[1] ?? ''
if (!modelHandle) return null
const isFromCortexHub = modelHandle?.includes('cortexso') ?? false

const isFromCortexHub = modelHandle.includes('cortexso')
const onModelBranchChanged = useCallback(
(models: string[]) => {
const isFromCortexHub = modelHandle?.includes('cortexso') ?? false
if (isFromCortexHub) {
setAvailableModels(models)
} else {
setAvailableModels(modelHandle != null ? [modelHandle] : [])
}
},
[modelHandle]
)

if (!modelHandle) return null

return (
<Modal
Expand All @@ -43,19 +56,22 @@ const DownloadLocalModelModal: React.FC = () => {
content={
<Fragment>
<HeaderModal
modelId={modelHandle}
modelHandle={modelHandle}
name={modelName}
onActionClick={() => {}}
modelIdVariants={[modelHandle]}
isLocalModel={true}
availableModels={availableModels}
/>
<Tab
tab={tab}
handleTab={(input) => setTab(input as 'Versions' | 'Information')}
/>
{tab === 'Versions' &&
(isFromCortexHub ? (
<ListModel modelHandle={modelHandle} />
<ListModel
modelHandle={modelHandle}
onBranchSelected={onModelBranchChanged}
/>
) : (
<HfListModel modelHandle={modelHandle} />
))}
Expand Down
55 changes: 45 additions & 10 deletions web/screens/HubScreen2/components/HeaderModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, useCallback, useRef, useState } from 'react'
import { Fragment, useCallback, useEffect, useRef, useState } from 'react'

import Image from 'next/image'

Expand All @@ -10,34 +10,69 @@ import { twMerge } from 'tailwind-merge'
import DropdownModal from './DropdownModal'

type Props = {
modelIdVariants: string[]
modelId: string
name: string
modelHandle?: string
availableModels: string[]
onActionClick: () => void
isLocalModel?: boolean
}

const HeaderModal: React.FC<Props> = ({
modelIdVariants,
modelId,
name,
modelHandle,
availableModels,
onActionClick,
isLocalModel = false,
}) => {
const [selectedVariant, setSelectedVariant] = useState<string>(modelId)
const [options, setOptions] = useState<{ name: string; value: string }[]>([])
const [selectedVariant, setSelectedVariant] = useState<string | undefined>()
const textRef = useRef<HTMLDivElement>(null)

const options = modelIdVariants.map((variant) => ({
name: variant,
value: variant,
}))
useEffect(() => {
const isFromCortexHub = modelHandle?.includes('cortexso') ?? false
if (!isLocalModel) {
setOptions(
availableModels.map((variant) => ({
name: variant,
value: variant,
}))
)
if (availableModels.length > 0) {
setSelectedVariant(availableModels[0])
}
return
}

if (isLocalModel && !isFromCortexHub) {
setOptions([
{
name: modelHandle ?? '',
value: modelHandle ?? '',
},
])
setSelectedVariant(modelHandle)
return
}

setOptions(
availableModels.map((variant) => ({
name: `${name}:${variant}`,
value: `${name}:${variant}`,
}))
)
if (availableModels.length > 0) {
setSelectedVariant(`${name}:${availableModels[0]}`)
}
}, [availableModels, name, modelHandle, isLocalModel])

const onCopyClicked = useCallback(() => {
navigator.clipboard.writeText(textRef.current?.innerText ?? '')
}, [])

const title = name.charAt(0).toUpperCase() + name.slice(1)

if (!selectedVariant) return null

return (
<div className="flex items-center">
<span className="text-xl font-semibold leading-8">{title}</span>
Expand Down
33 changes: 22 additions & 11 deletions web/screens/HubScreen2/components/ListModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'

type Props = {
modelHandle: string
onBranchSelected?: (availableSelections: string[]) => void
}

const ListModel: React.FC<Props> = ({ modelHandle }) => {
const ListModel: React.FC<Props> = ({ modelHandle, onBranchSelected }) => {
const { data: engineData } = useEngineQuery()
const { data, isLoading } = useHfEngineToBranchesQuery(modelHandle)

const [engineFilter, setEngineFilter] = useState<EngineType | undefined>(
undefined
)
const { data, isLoading } = useHfEngineToBranchesQuery(modelHandle)

const engineSelections: { name: string; value: string }[] = useMemo(() => {
if (!data || !engineData) return []
Expand All @@ -63,17 +65,26 @@ const ListModel: React.FC<Props> = ({ modelHandle }) => {
return result
}, [data, engineData])

const modelBranches: CortexHubModel[] = useMemo((): CortexHubModel[] => {
if (!data) return []
return (data[engineFilter as EngineType] as CortexHubModel[]) ?? []
}, [data, engineFilter])

useEffect(() => {
if (engineSelections.length === 0) return
setEngineFilter(engineSelections[0].value as EngineType)
}, [engineSelections])

const modelBranches: CortexHubModel[] = []
if (data) {
const branches = data[engineFilter as EngineType] as CortexHubModel[]
if (!branches || branches.length === 0) return
modelBranches.push(...branches)
}
const models = modelBranches.map((m) => m.name)
onBranchSelected?.(models)
}, [engineSelections, modelBranches, onBranchSelected])

const onSelectionChanged = useCallback(
(selectionValue: string) => {
setEngineFilter(selectionValue as EngineType)
const models = modelBranches.map((m) => m.name)
onBranchSelected?.(models)
},
[setEngineFilter, onBranchSelected, modelBranches]
)

if (isLoading)
return (
Expand All @@ -90,7 +101,7 @@ const ListModel: React.FC<Props> = ({ modelHandle }) => {
value={engineFilter}
className="gap-1.5 whitespace-nowrap px-4 py-2 font-semibold"
options={engineSelections}
onValueChange={(value) => setEngineFilter(value as EngineType)}
onValueChange={onSelectionChanged}
/>
</div>
<div className="mt-3 w-full overflow-hidden rounded-md border border-[hsla(var(--app-border))]">
Expand Down
3 changes: 1 addition & 2 deletions web/screens/HubScreen2/components/SetUpRemoteModelModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const SetUpRemoteModelModal: React.FC = () => {
<HeaderModal
name={modelName}
onActionClick={navigateToSetUpApiKey}
modelId={modelId}
modelIdVariants={[modelId]}
availableModels={[modelId]}
/>
<ModelTitle
className="text-[hsla(var(--text-secondary)] my-4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const ImportingModelItem = ({ model }: Props) => {
}, [])

const onDeleteModelClick = useCallback(() => {}, [])
console.log('namh model', model)
const displayStatus = useMemo(() => {
if (model.status === 'FAILED') {
return 'Failed'
Expand Down
8 changes: 0 additions & 8 deletions web/screens/Settings/ImportingModelModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ const ImportingModelModal = () => {
const importModels = async () => {
for (const model of importingModels) {
await downloadModel(model.path)
// const parsedResult = await result?.json()
// if (
// parsedResult['message'] &&
// parsedResult['message'] === 'Download model started successfully.'
// ) {
// // update importingModels
// }
// console.log(`NamH result ${JSON.stringify(parsedResult)}`)
}
}
importModels()
Expand Down

0 comments on commit 5e06ed8

Please sign in to comment.