Skip to content

Commit

Permalink
Use baseUrl, no header (#370)
Browse files Browse the repository at this point in the history
* Use baseUrl, no header

* Fix urls

* Add log

* Logs IP

* Fix chat service url

* Fix download

* Remove logs
  • Loading branch information
Janaka-Steph authored Oct 9, 2023
1 parent 25f6d4e commit 34b7711
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 71 deletions.
9 changes: 1 addition & 8 deletions src/modules/prem-audio/api/generateTranscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import axios from "axios";
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 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 && service?.invokeMethod.header) {
const [key, value] = service.invokeMethod.header.split(":");
Object.assign(headers, { [key]: value });
}
return axios.post(`${service.invokeMethod.baseUrl}/v1/audio/transcriptions`, formData, {
return axios.post(`${service.baseUrl}/v1/audio/transcriptions`, formData, {
headers,
});
};
Expand Down
9 changes: 1 addition & 8 deletions src/modules/prem-image/api/generateImage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import axios from "axios";
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 isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "application/json" };
if (isProxyEnabled() && isIP && service?.invokeMethod.header) {
const [key, value] = service.invokeMethod.header.split(":");
Object.assign(headers, { [key]: value });
}
return axios.post(`${service.invokeMethod.baseUrl}/v1/images/generation`, data, { headers });
return axios.post(`${service.baseUrl}/v1/images/generation`, data, { headers });
};

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

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

Expand All @@ -15,13 +13,8 @@ const generateImageViaBaseImage = async (service: Service, image: File, data: Im
formData.append("negative_prompt", `${data.negative_prompt || ""}`);
formData.append("seed", `${data.seed}`);

const isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "multipart/form-data" };
if (isProxyEnabled() && isIP && service?.invokeMethod.header) {
const [key, value] = service.invokeMethod.header.split(":");
Object.assign(headers, { [key]: value });
}
return axios.post(`${service.invokeMethod.baseUrl}/v1/images/edits`, formData, { headers });
return axios.post(`${service.baseUrl}/v1/images/edits`, formData, { headers });
};

export default generateImageViaBaseImage;
9 changes: 1 addition & 8 deletions src/modules/prem-text-audio/api/generateAudio.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import axios from "axios";
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 isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "application/json" };
if (isProxyEnabled() && isIP && service?.invokeMethod.header) {
const [key, value] = service.invokeMethod.header.split(":");
Object.assign(headers, { [key]: value });
}
return axios.post(`${service.invokeMethod.baseUrl}/v1/audio/generation`, data, { headers });
return axios.post(`${service.baseUrl}/v1/audio/generation`, data, { headers });
};

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

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

Expand All @@ -13,13 +11,8 @@ const generateUpscalerImage = async (service: Service, data: ImageGeneration) =>
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 && service?.invokeMethod.header) {
const [key, value] = service.invokeMethod.header.split(":");
Object.assign(headers, { [key]: value });
}
return axios.post(`${service.invokeMethod.baseUrl}/v1/images/upscale`, formData, { headers });
return axios.post(`${service.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
Expand Up @@ -39,8 +39,8 @@ const ServiceGeneralInfo = ({ service }: ServiceGeneralInfoProps) => {
if (!service.running) {
return "";
}
return `${service.invokeMethod.baseUrl}/docs`;
}, [service.invokeMethod, service.running]);
return `${service.baseUrl}/docs`;
}, [service.baseUrl, service.running]);

return (
<div className="card px-[22px] py-8 mt-4">
Expand Down
10 changes: 8 additions & 2 deletions src/modules/service/api/downloadServiceStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ const downloadServiceStream = async (
onMessage: (message: DownloadMessage) => void,
onceCompleted: () => void,
): Promise<void> => {
const backendUrl = new URL(useSettingStore.getState().backendUrl);
try {
instantiateEventSource(backendUrl.toString(), serviceId);
const isIP = useSettingStore.getState().isIP;
let baseUrl;
if (isIP) {
baseUrl = `${useSettingStore.getState().backendUrl}premd/`;
} else {
baseUrl = `${window.location.protocol}//premd.${window.location.host}/`;
}
instantiateEventSource(baseUrl, serviceId);
eventSources[serviceId].onmessage = (event) => {
if (!event.data) return;
const parsedData = JSON.parse(event.data);
Expand Down
5 changes: 1 addition & 4 deletions src/modules/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ export type Service = {
beta: boolean;
comingSoon: boolean;
promptTemplate: string;
invokeMethod: {
header: string;
baseUrl: string;
};
baseUrl: string;
};

export type SearchFilterProps = {
Expand Down
3 changes: 1 addition & 2 deletions src/shared/api/dnsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const apiDnsd = () => {
let baseURL;
if (isProxyEnabled()) {
if (isIP) {
baseURL = useSettingStore.getState().backendUrl;
Object.assign(headers, { "X-Host-Override": "dnsd" });
baseURL = `${useSettingStore.getState().backendUrl}dnsd/`;
} else {
baseURL = `${window.location.protocol}//dnsd.${window.location.host}/`;
}
Expand Down
11 changes: 8 additions & 3 deletions src/shared/api/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import useSettingStore from "../store/setting";
const api = () => {
const isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "application/json" };
if (isProxyEnabled() && isIP) {
Object.assign(headers, { "X-Host-Override": "premd" });
let baseURL;
if (isProxyEnabled()) {
if (isIP) {
baseURL = `${useSettingStore.getState().backendUrl}premd/`;
} else {
baseURL = `${window.location.protocol}//premd.${window.location.host}/`;
}
}
return axios.create({
baseURL: useSettingStore.getState().backendUrl,
baseURL,
headers,
});
};
Expand Down
21 changes: 4 additions & 17 deletions src/shared/hooks/usePremChatStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ 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 { isProxyEnabled } from "shared/helpers/utils";
import { v4 as uuid } from "uuid";
import { shallow } from "zustand/shallow";

import usePremChatStore from "../store/prem-chat";
import useSettingStore from "../store/setting";

import useService from "./useService";

Expand Down Expand Up @@ -56,16 +54,11 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe
}),
shallow,
);
const [backendUrlState, setBackendUrlState] = useState("");

useEffect(() => {
if (service) {
const backendUrl = `${window.location.protocol}//${service.invokeMethod.baseUrl}`;
setBackendUrlState(backendUrl);
setChatServiceUrl(backendUrl);
}
setChatServiceUrl(`${service?.baseUrl}/v1/chat/completions`);
if (!promptTemplate) {
setPromptTemplate(service?.promptTemplate || "");
setPromptTemplate(service?.promptTemplate ?? "");
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [service]);
Expand Down Expand Up @@ -93,14 +86,8 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe
setLoading(true);
abortController.current = new AbortController();

const isIP = useSettingStore.getState().isIP;
const headers = { "Content-Type": "application/json" };
if (isProxyEnabled() && isIP && service?.invokeMethod.header) {
const [key, value] = service.invokeMethod.header.split(":");
Object.assign(headers, { [key]: value });
}

console.log("chatServiceUrl", chatServiceUrl);
try {
fetchEventSource(chatServiceUrl, {
method: "POST",
Expand Down Expand Up @@ -204,8 +191,8 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe
}, [service, setPromptTemplate]);

const resetChatServiceUrl = useCallback(() => {
setChatServiceUrl(backendUrlState);
}, [backendUrlState, setChatServiceUrl]);
setChatServiceUrl(service?.baseUrl ?? "");
}, [service?.baseUrl, setChatServiceUrl]);

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

0 comments on commit 34b7711

Please sign in to comment.