From 5b2408d7b5f8bf0e13cef31166ea6f50c4b1d188 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 30 Aug 2021 04:47:45 -0400 Subject: [PATCH] abstract out model --- vocodes/src/v2/api/tts/GetTtsModel.ts | 55 ++++++++++++++++++ .../tts/tts_model_view/TtsModelViewFc.tsx | 57 ++----------------- 2 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 vocodes/src/v2/api/tts/GetTtsModel.ts diff --git a/vocodes/src/v2/api/tts/GetTtsModel.ts b/vocodes/src/v2/api/tts/GetTtsModel.ts new file mode 100644 index 0000000000..8dd1e81f20 --- /dev/null +++ b/vocodes/src/v2/api/tts/GetTtsModel.ts @@ -0,0 +1,55 @@ +import { ApiConfig } from "../../../common/ApiConfig"; + +interface TtsModelViewResponsePayload { + success: boolean, + model: TtsModel, +} + +export interface TtsModel { + model_token: string, + title: string, + tts_model_type: string, + maybe_default_pretrained_vocoder: string | null, + text_preprocessing_algorithm: string, + creator_user_token: string, + creator_username: string, + creator_display_name: string, + creator_gravatar_hash: string, + description_markdown: string, + description_rendered_html: string, + creator_set_visibility: string, + updatable_slug: string, + created_at: string, + updated_at: string, + maybe_moderator_fields: TtsModelModeratorFields | null | undefined, +} + +export interface TtsModelModeratorFields { + creator_ip_address_creation: string, + creator_ip_address_last_update: string, + mod_deleted_at: string | undefined | null, + user_deleted_at: string | undefined | null, +} + +export async function GetTtsModel(modelToken: string) : Promise { + const endpoint = new ApiConfig().viewTtsModel(modelToken); + + return await fetch(endpoint, { + method: 'GET', + headers: { + 'Accept': 'application/json', + }, + credentials: 'include', + }) + .then(res => res.json()) + .then(res => { + const response : TtsModelViewResponsePayload = res; + if (!response.success) { + return; + } + return response?.model; + }) + .catch(e => { + return undefined; + }); +} diff --git a/vocodes/src/v2/view/tts/tts_model_view/TtsModelViewFc.tsx b/vocodes/src/v2/view/tts/tts_model_view/TtsModelViewFc.tsx index 6454d6f9ae..42cf12e7cd 100644 --- a/vocodes/src/v2/view/tts/tts_model_view/TtsModelViewFc.tsx +++ b/vocodes/src/v2/view/tts/tts_model_view/TtsModelViewFc.tsx @@ -9,42 +9,13 @@ import { v4 as uuidv4 } from 'uuid'; import { FrontendUrlConfig } from '../../../../common/FrontendUrlConfig'; import { HiddenIconFc } from '../../_icons/HiddenIcon'; import { VisibleIconFc } from '../../_icons/VisibleIcon'; - -interface TtsModelViewResponsePayload { - success: boolean, - model: TtsModel, -} +import { GetTtsModel, TtsModel } from '../../../api/tts/GetTtsModel'; interface TtsModelUseCountResponsePayload { success: boolean, count: number | null | undefined, } -interface TtsModel { - model_token: string, - title: string, - tts_model_type: string, - maybe_default_pretrained_vocoder: string | null, - text_preprocessing_algorithm: string, - creator_user_token: string, - creator_username: string, - creator_display_name: string, - description_markdown: string, - description_rendered_html: string, - creator_set_visibility: string, - updatable_slug: string, - created_at: string, - updated_at: string, - maybe_moderator_fields: TtsModelModeratorFields | null | undefined, -} - -interface TtsModelModeratorFields { - creator_ip_address_creation: string, - creator_ip_address_last_update: string, - mod_deleted_at: string | undefined | null, - user_deleted_at: string | undefined | null, -} - interface Props { sessionWrapper: SessionWrapper, enqueueTtsJob: (jobToken: string) => void, @@ -58,27 +29,11 @@ function TtsModelViewFc(props: Props) { const [ttsModelUseCount, setTtsModelUseCount] = useState(undefined); const [text, setText] = useState(""); - const getModel = useCallback((token) => { - const api = new ApiConfig(); - const endpointUrl = api.viewTtsModel(token); - - fetch(endpointUrl, { - method: 'GET', - headers: { - 'Accept': 'application/json', - }, - credentials: 'include', - }) - .then(res => res.json()) - .then(res => { - const modelsResponse : TtsModelViewResponsePayload = res; - if (!modelsResponse.success) { - return; - } - - setTtsModel(modelsResponse.model) - }) - .catch(e => {}); + const getModel = useCallback(async (token) => { + const model = await GetTtsModel(token); + if (model) { + setTtsModel(model); + } }, []); const getModelUseCount = useCallback((token) => {