Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Janaka-Steph committed Oct 3, 2023
1 parent c4c3100 commit 28fc65c
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/modules/prem-audio/api/generateTranscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import axios from "axios";
import { getServiceUrl, isProxyEnabled } from "shared/helpers/utils";
import { isProxyEnabled } from "shared/helpers/utils";

import useSettingStore from "../../../shared/store/setting";
import type { Service } from "../../service/types";
import type { TranscriptionsGeneration } from "../types";

const generateTranscriptions = async (service: Service, data: TranscriptionsGeneration) => {
const backendUrl = getServiceUrl(service.invokeMethod, "v1/audio/transcriptions");
const formData = new FormData();
formData.append("file", data.file);
formData.append("model", data.model);

const isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "multipart/form-data" };
if (isProxyEnabled() && isIP) {
Object.assign(headers, service.invokeMethod.header);
}
return axios.post(backendUrl, formData, { headers });
return axios.post(`${service.invokeMethod.baseUrl}/v1/audio/transcriptions`, formData, {
headers,
});
};

export default generateTranscriptions;
5 changes: 2 additions & 3 deletions src/modules/prem-image/api/generateImage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import axios from "axios";
import { getServiceUrl, isProxyEnabled } from "shared/helpers/utils";
import { isProxyEnabled } from "shared/helpers/utils";

import useSettingStore from "../../../shared/store/setting";
import type { Service } from "../../service/types";
import type { ImageGeneration } from "../types";

const generateImage = async (service: Service, data: ImageGeneration) => {
const backendUrl = getServiceUrl(service.invokeMethod, "v1/images/generations");
const isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "application/json" };
if (isProxyEnabled() && isIP) {
Object.assign(headers, service.invokeMethod.header);
}
return axios.post(backendUrl, data, { headers });
return axios.post(`${service.invokeMethod.baseUrl}/v1/images/generation`, data, { headers });
};

export default generateImage;
7 changes: 2 additions & 5 deletions src/modules/prem-image/api/generateImageViaBaseImage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import axios from "axios";
import { getServiceUrl, isProxyEnabled } from "shared/helpers/utils";
import { isProxyEnabled } from "shared/helpers/utils";

import useSettingStore from "../../../shared/store/setting";
import type { Service } from "../../service/types";
import type { ImageGeneration } from "../types";

const generateImageViaBaseImage = async (service: Service, image: File, data: ImageGeneration) => {
const backendUrl = getServiceUrl(service.invokeMethod, "v1/images/edits");

const formData = new FormData();
formData.append("image", image);
formData.append("prompt", data.prompt);
Expand All @@ -22,8 +20,7 @@ const generateImageViaBaseImage = async (service: Service, image: File, data: Im
if (isProxyEnabled() && isIP) {
Object.assign(headers, service.invokeMethod.header);
}

return axios.post(backendUrl, formData, { headers });
return axios.post(`${service.invokeMethod.baseUrl}/v1/images/edits`, formData, { headers });
};

export default generateImageViaBaseImage;
5 changes: 2 additions & 3 deletions src/modules/prem-text-audio/api/generateAudio.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import axios from "axios";
import { getServiceUrl, isProxyEnabled } from "shared/helpers/utils";
import { isProxyEnabled } from "shared/helpers/utils";

import useSettingStore from "../../../shared/store/setting";
import type { Service } from "../../service/types";
import type { AudioGenerationData } from "../types";

const generateAudio = async (service: Service, data: AudioGenerationData) => {
const backendUrl = getServiceUrl(service.invokeMethod, "v1/audio/generation");
const isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "application/json" };
if (isProxyEnabled() && isIP) {
Object.assign(headers, service.invokeMethod.header);
}
return axios.post(`${backendUrl}`, data, { headers });
return axios.post(`${service.invokeMethod.baseUrl}/v1/audio/generation`, data, { headers });
};

export default generateAudio;
7 changes: 2 additions & 5 deletions src/modules/prem-upscaler/api/generateUpscalerImage.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import axios from "axios";
import { getServiceUrl, isProxyEnabled } from "shared/helpers/utils";
import { isProxyEnabled } from "shared/helpers/utils";

import useSettingStore from "../../../shared/store/setting";
import type { Service } from "../../service/types";
import type { ImageGeneration } from "../types";

const generateUpscalerImage = async (service: Service, data: ImageGeneration) => {
const backendUrl = getServiceUrl(service.invokeMethod, "v1/images/upscale");
const formData = new FormData();
formData.append("image", data.image);
formData.append("prompt", data.prompt);
formData.append("n", `${data.n}`);
formData.append("response_format", data.response_format);
formData.append("guidance_scale", `${data.guidance_scale}`);
formData.append("num_inference_steps", `${data.num_inference_steps}`);

const isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "multipart/form-data" };
if (isProxyEnabled() && isIP) {
Object.assign(headers, service.invokeMethod.header);
}

return axios.post(backendUrl, formData, { headers });
return axios.post(`${service.invokeMethod.baseUrl}/v1/images/upscale`, formData, { headers });
};

export default generateUpscalerImage;
4 changes: 2 additions & 2 deletions src/modules/service-detail/components/ServiceGeneralInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isArray, startCase } from "lodash";
import { useMemo } from "react";
import { formatInfo, getServiceUrl } from "shared/helpers/utils";
import { formatInfo } from "shared/helpers/utils";

import type { ServiceGeneralInfoProps, ServiceInfoValue } from "../types";

Expand Down Expand Up @@ -39,7 +39,7 @@ const ServiceGeneralInfo = ({ service }: ServiceGeneralInfoProps) => {
if (!service.running) {
return "";
}
return getServiceUrl(service.invokeMethod, "docs");
return `${service.invokeMethod.baseUrl}/docs`;
}, [service.invokeMethod, service.running]);

return (
Expand Down
12 changes: 0 additions & 12 deletions src/shared/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,6 @@ export const AUDIO_TO_TEXT_ID = "audio-to-text";
export const TEXT_TO_AUDIO_ID = "text-to-audio";
export const UPSCALER_ID = "upscaler";

export const getServiceUrl = (invokeMethod: Service["invokeMethod"], pathname: string) => {
let serviceUrl = "http://localhost:54321";
if (isProxyEnabled()) {
if (invokeMethod.header && isIP(window.location.host)) {
serviceUrl = `${window.location.protocol}//${window.location.host}/`;
} else {
serviceUrl = invokeMethod.baseUrl;
}
}
return `${serviceUrl}/${pathname}`;
};

export const isDeveloperMode = () => {
return (window as any).VITE_DEVELOPER_MODE === "1" || import.meta.env.VITE_DEVELOPER_MODE === "1";
};
Expand Down
4 changes: 2 additions & 2 deletions src/shared/hooks/usePremChatStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PremChatResponse } from "modules/prem-chat/types";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useNavigate } from "react-router-dom";
import { toast } from "react-toastify";
import { getServiceUrl, isProxyEnabled } from "shared/helpers/utils";
import { isProxyEnabled } from "shared/helpers/utils";
import { v4 as uuid } from "uuid";
import { shallow } from "zustand/shallow";

Expand Down Expand Up @@ -60,7 +60,7 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe

useEffect(() => {
if (service) {
const backendUrl = getServiceUrl(service.invokeMethod, "v1/chat/completions");
const backendUrl = `${service.invokeMethod.baseUrl}/v1/chat/completions`;
setBackendUrlState(backendUrl);
setChatServiceUrl(backendUrl);
}
Expand Down
4 changes: 1 addition & 3 deletions src/shared/hooks/usePremTextAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import usePremTextAudioStore from "shared/store/prem-text-audio";
import { v4 as uuid } from "uuid";
import { shallow } from "zustand/shallow";

import { getServiceUrl } from "../helpers/utils";

import useService from "./useService";

const usePremTextAudio = (serviceId: string, historyId: string | undefined): PremTextAudioHook => {
Expand Down Expand Up @@ -42,7 +40,7 @@ const usePremTextAudio = (serviceId: string, historyId: string | undefined): Pre
file,
prompt,
timestamp: new Date().toISOString(),
fileUrl: getServiceUrl(service!.invokeMethod, `files/${file}`),
fileUrl: `${service!.invokeMethod.baseUrl}/files/${file}`,
});
navigate(`/prem-text-audio/${serviceId}/${id}`);
},
Expand Down

0 comments on commit 28fc65c

Please sign in to comment.