import { OnnxExecutionProvider, Protocol, Framework, fileSelector } from "@dannadori/voice-changer-client-js" import React from "react" import { useMemo } from "react" import { ClientState } from "./hooks/useClient" export type UseServerSettingProps = { clientState: ClientState } export type ServerSettingState = { serverSetting: JSX.Element; } export const useServerSetting = (props: UseServerSettingProps): ServerSettingState => { const mmvcServerUrlRow = useMemo(() => { const onSetServerClicked = async () => { const input = document.getElementById("mmvc-server-url") as HTMLInputElement props.clientState.setSettingState({ ...props.clientState.settingState, mmvcServerUrl: input.value }) } return (
MMVC Server
set
) }, [props.clientState.settingState]) const uploadeModelRow = useMemo(() => { const onPyTorchFileLoadClicked = async () => { const file = await fileSelector("") if (file.name.endsWith(".pth") == false) { alert("モデルファイルの拡張子はpthである必要があります。") return } props.clientState.setSettingState({ ...props.clientState.settingState, pyTorchModel: file }) } const onPyTorchFileClearClicked = () => { props.clientState.setSettingState({ ...props.clientState.settingState, pyTorchModel: null }) } const onConfigFileLoadClicked = async () => { const file = await fileSelector("") if (file.name.endsWith(".json") == false) { alert("モデルファイルの拡張子はjsonである必要があります。") return } props.clientState.setSettingState({ ...props.clientState.settingState, configFile: file }) } const onConfigFileClearClicked = () => { props.clientState.setSettingState({ ...props.clientState.settingState, configFile: null }) } const onOnnxFileLoadClicked = async () => { const file = await fileSelector("") if (file.name.endsWith(".onnx") == false) { alert("モデルファイルの拡張子はonnxである必要があります。") return } props.clientState.setSettingState({ ...props.clientState.settingState, onnxModel: file }) } const onOnnxFileClearClicked = () => { props.clientState.setSettingState({ ...props.clientState.settingState, onnxModel: null }) } const onModelUploadClicked = async () => { props.clientState.loadModel() } return ( <>
Model Uploader
PyTorch(.pth)
{props.clientState.settingState.pyTorchModel?.name}
select
clear
Config(.json)
{props.clientState.settingState.configFile?.name}
select
clear
Onnx(.onnx)
{props.clientState.settingState.onnxModel?.name}
select
clear
{props.clientState.isUploading ? `uploading.... ${props.clientState.uploadProgress}%` : ""}
upload
) }, [ props.clientState.settingState, props.clientState.loadModel, props.clientState.isUploading, props.clientState.uploadProgress]) const protocolRow = useMemo(() => { const onProtocolChanged = async (val: Protocol) => { props.clientState.setSettingState({ ...props.clientState.settingState, protocol: val }) } return (
Protocol
) }, [props.clientState.settingState]) const frameworkRow = useMemo(() => { const onFrameworkChanged = async (val: Framework) => { props.clientState.setSettingState({ ...props.clientState.settingState, framework: val }) } return (
Framework
) }, [props.clientState.settingState]) const onnxExecutionProviderRow = useMemo(() => { if (props.clientState.settingState.framework != "ONNX") { return } const onOnnxExecutionProviderChanged = async (val: OnnxExecutionProvider) => { props.clientState.setSettingState({ ...props.clientState.settingState, onnxExecutionProvider: val }) } return (
OnnxExecutionProvider
) }, [props.clientState.settingState]) const serverSetting = useMemo(() => { return ( <>
Server Setting
{mmvcServerUrlRow} {uploadeModelRow} {frameworkRow} {onnxExecutionProviderRow} {protocolRow} ) }, [mmvcServerUrlRow, uploadeModelRow, frameworkRow, onnxExecutionProviderRow, protocolRow]) return { serverSetting, } }