Skip to content

Commit

Permalink
Use service url for services
Browse files Browse the repository at this point in the history
  • Loading branch information
Janaka-Steph committed Oct 3, 2023
1 parent ae5c589 commit ddec10f
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 34 deletions.
9 changes: 5 additions & 4 deletions src/modules/prem-audio/api/generateTranscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import axios from "axios";
import { generateUrl, isProxyEnabled } from "shared/helpers/utils";
import { generateUrl, getServiceUrl, 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 (port: number, data: TranscriptionsGeneration) => {
const generateTranscriptions = async (service: Service, data: TranscriptionsGeneration) => {
const backendUrl = generateUrl(
useSettingStore.getState().backendUrl,
port,
getServiceUrl(service.id),
service.runningPort,
"v1/audio/transcriptions",
);
const formData = new FormData();
Expand Down
9 changes: 5 additions & 4 deletions src/modules/prem-image/api/generateImage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import axios from "axios";
import { generateUrl, isProxyEnabled } from "shared/helpers/utils";
import { generateUrl, getServiceUrl, 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 (port: number, data: ImageGeneration) => {
const generateImage = async (service: Service, data: ImageGeneration) => {
const backendUrl = generateUrl(
useSettingStore.getState().backendUrl,
port,
getServiceUrl(service.id),
service.runningPort,
"v1/images/generations",
);
const isIP = useSettingStore.getState().isIP;
Expand Down
7 changes: 4 additions & 3 deletions src/modules/prem-image/api/generateImageViaBaseImage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import axios from "axios";
import { generateUrl, isProxyEnabled } from "shared/helpers/utils";
import { generateUrl, getServiceUrl, 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 (port: number, image: File, data: ImageGeneration) => {
const backendUrl = generateUrl(useSettingStore.getState().backendUrl, port, "v1/images/edits");
const generateImageViaBaseImage = async (service: Service, image: File, data: ImageGeneration) => {
const backendUrl = generateUrl(getServiceUrl(service.id), service.runningPort, "v1/images/edits");

const formData = new FormData();
formData.append("image", image);
Expand Down
10 changes: 5 additions & 5 deletions src/modules/prem-text-audio/api/generateAudio.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import axios from "axios";
import { generateUrl } from "shared/helpers/utils";
import { generateUrl, getServiceUrl } from "shared/helpers/utils";

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

const generateAudio = async (port: number, data: AudioGenerationData) => {
const generateAudio = async (service: Service, data: AudioGenerationData) => {
const backendUrl = generateUrl(
useSettingStore.getState().backendUrl,
port,
getServiceUrl(service.id),
service.runningPort,
"v1/audio/generation",
);

Expand Down
11 changes: 8 additions & 3 deletions src/modules/prem-upscaler/api/generateUpscalerImage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import axios from "axios";
import { generateUrl, isProxyEnabled } from "shared/helpers/utils";
import { generateUrl, getServiceUrl, 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 (port: number, data: ImageGeneration) => {
const backendUrl = generateUrl(useSettingStore.getState().backendUrl, port, "v1/images/upscale");
const generateUpscalerImage = async (service: Service, data: ImageGeneration) => {
const backendUrl = generateUrl(
getServiceUrl(service.id),
service.runningPort,
"v1/images/upscale",
);
const formData = new FormData();
formData.append("image", data.image);
formData.append("prompt", data.prompt);
Expand Down
21 changes: 21 additions & 0 deletions src/shared/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ export const generateUrl = (baseUrl: string, port: number, pathname: string) =>
return url.toString();
};

export const getServiceUrl = (serviceId: Service["id"]) => {
let serviceUrl = "http://localhost:54321";
const isBackendSet = () => {
return (
((window as any).VITE_BACKEND_URL !== undefined || import.meta.env.VITE_BACKEND_URL) &&
((window as any).VITE_BACKEND_URL !== "" || import.meta.env.VITE_BACKEND_URL !== "")
);
};
if (isBackendSet()) {
serviceUrl = (window as any).VITE_BACKEND_URL || import.meta.env.VITE_BACKEND_URL;
}
if (isPackaged()) {
serviceUrl = `${window.location.protocol}//${window.location.host}/`;
}
if (isProxyEnabled() && !isIP(window.location.host)) {
const arr = serviceUrl.split("://");
serviceUrl = arr[0] + `://${serviceId}.` + arr[1];
}
return serviceUrl;
};

export const isDeveloperMode = () => {
return (window as any).VITE_DEVELOPER_MODE === "1" || import.meta.env.VITE_DEVELOPER_MODE === "1";
};
Expand Down
2 changes: 1 addition & 1 deletion src/shared/hooks/usePremAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const usePremAudio = (serviceId: string, historyId: string | undefined): PremAud

const { isLoading, isError, mutate } = useMutation(
() =>
generateTranscriptions(service?.runningPort ?? 0, {
generateTranscriptions(service!, {
file: file!,
model,
}),
Expand Down
19 changes: 10 additions & 9 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 { generateUrl, isProxyEnabled } from "shared/helpers/utils";
import { generateUrl, getServiceUrl, isProxyEnabled } from "shared/helpers/utils";
import { v4 as uuid } from "uuid";
import { shallow } from "zustand/shallow";

Expand Down Expand Up @@ -59,14 +59,15 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe
const [backendUrlState, setBackendUrlState] = useState("");

useEffect(() => {
const backendUrl = generateUrl(
useSettingStore.getState().backendUrl,
service?.runningPort ?? 0,
"v1/chat/completions",
);
setBackendUrlState(backendUrl);
setChatServiceUrl(backendUrl);

if (service) {
const backendUrl = generateUrl(
getServiceUrl(service.id),
service?.runningPort ?? 0,
"v1/chat/completions",
);
setBackendUrlState(backendUrl);
setChatServiceUrl(backendUrl);
}
if (!promptTemplate) {
setPromptTemplate(service?.promptTemplate || "");
}
Expand Down
4 changes: 1 addition & 3 deletions src/shared/hooks/usePremImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ const usePremImage = (serviceId: string, historyId: string | undefined): PremIma

const { isLoading, isError, mutate } = useMutation(
() =>
file
? generateImageViaBaseImage(service?.runningPort ?? 0, file, payload)
: generateImage(service?.runningPort ?? 0, payload),
file ? generateImageViaBaseImage(service!, file, payload) : generateImage(service!, payload),
{
onSuccess: (response) => {
setFile(undefined);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/hooks/usePremTextAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const usePremTextAudio = (serviceId: string, historyId: string | undefined): Pre

const { isLoading, isError, mutate } = useMutation(
() =>
generateAudio(service?.runningPort ?? 0, {
generateAudio(service!, {
prompt,
}),
{
Expand Down
2 changes: 1 addition & 1 deletion src/shared/hooks/usePremUpscaler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const usePremUpscaler = (serviceId: string, historyId: string | undefined): Prem

const { isLoading, isError, mutate } = useMutation(
() =>
generateUpscalerImage(service?.runningPort ?? 0, {
generateUpscalerImage(service!, {
image: file!,
prompt,
n,
Expand Down

0 comments on commit ddec10f

Please sign in to comment.