Skip to content

Commit

Permalink
馃敡 refactor: refactor to fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Nov 15, 2023
1 parent 7338a9e commit d875be6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
8 changes: 4 additions & 4 deletions api/edge-speech.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createEdgeSpeechComletion } from '../src/server/createEdgeSpeechComletion';
import { EdgeSpeechPayload } from '../src/server/types';
import { EdgeSpeechPayload, createEdgeSpeechComletion } from '@lobehub/tts';

export const config = {
runtime: 'edge',
};

export default async (req: Request) => {
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });

const payload = (await req.json()) as EdgeSpeechPayload;
const res = await createEdgeSpeechComletion({ payload });
return res;

return createEdgeSpeechComletion({ payload });
};
3 changes: 1 addition & 2 deletions api/microsoft-speech.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createMicrosoftSpeechComletion } from '../src/server/createMicrosoftSpeechComletion';
import { MicrosoftSpeechPayload } from '../src/server/types';
import { MicrosoftSpeechPayload, createMicrosoftSpeechComletion } from '@lobehub/tts';

export const config = {
runtime: 'edge',
Expand Down
15 changes: 9 additions & 6 deletions api/open-stt.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { OpenAISTTPayload, createOpenaiAudioTranscriptionsCompletion } from '@lobehub/tts';
import OpenAI from 'openai';

import { OPENAI_API_KEY, OPENAI_PROXY_URL } from '@/const/api';

import { createOpenaiAudioTranscriptionsCompletion } from '../src/server/createOpenaiAudioTranscriptionsCompletion';
import { OpenAISTTPayload } from '../src/server/types';

export const config = {
runtime: 'edge',
};

export default async (req: Request) => {
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
const payload = (await req.json()) as OpenAISTTPayload;

const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const OPENAI_PROXY_URL = process.env.OPENAI_PROXY_URL;

if (!OPENAI_API_KEY) return new Response('OPENAI_API_KEY is not set', { status: 500 });

const payload = (await req.json()) as OpenAISTTPayload;

const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
const res = await createOpenaiAudioTranscriptionsCompletion({ openai, payload });

return new Response(JSON.stringify(res), {
headers: {
'content-type': 'application/json;charset=UTF-8',
Expand Down
16 changes: 8 additions & 8 deletions api/openai-tts.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { OpenAITTSPayload, createOpenaiAudioSpeechCompletion } from '@lobehub/tts';
import OpenAI from 'openai';

import { OPENAI_API_KEY, OPENAI_PROXY_URL } from '@/const/api';

import { createOpenaiAudioSpeechCompletion } from '../src/server/createOpenaiAudioSpeechCompletion';
import { OpenAITTSPayload } from '../src/server/types';

export const config = {
runtime: 'edge',
};

export default async (req: Request) => {
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
const payload = (await req.json()) as OpenAITTSPayload;
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const OPENAI_PROXY_URL = process.env.OPENAI_PROXY_URL;

if (!OPENAI_API_KEY) return new Response('OPENAI_API_KEY is not set', { status: 500 });
const payload = (await req.json()) as OpenAITTSPayload;

const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
const res = await createOpenaiAudioSpeechCompletion({ openai, payload });
return res;

return createOpenaiAudioSpeechCompletion({ openai, payload });
};
12 changes: 3 additions & 9 deletions src/const/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const MICROSOFT_SPEECH_URL =
export const EDGE_SPEECH_URL =
'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
export const EDGE_API_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';
export const OPENAI_BASE_URL = 'https://api.openai.com/v1';

export const MICROSOFT_SPEECH_API_URL = '/api/microsoft-speech';
export const EDGE_SPEECH_API_URL = '/api/edge-speech';
Expand All @@ -15,13 +16,6 @@ export const AZURE_SPEECH_KEY =
process.env.AZURE_SPEECH_KEY || process.env.NEXT_PUBLIC_AZURE_SPEECH_KEY || '';
export const AZURE_SPEECH_REGION =
process.env.AZURE_SPEECH_REGION || process.env.NEXT_PUBLIC_AZURE_SPEECH_REGION || '';
export const OPENAI_API_KEY =
process.env.OPENAI_API_KEY || process.env.NEXT_PUBLIC_OPENAI_API_KEY || '';
export const OPENAI_PROXY_URL =
process.env.OPENAI_PROXY_URL ||
process.env.NEXT_PUBLIC_OPENAI_PROXY_URL ||
'https://api.openai.com/v1';

export const OPENAI_TTS_URL = (api?: string) => urlJoin(api || OPENAI_PROXY_URL, 'audio/speech');
export const OPENAI_STT_URL = (api?: string) =>
urlJoin(api || OPENAI_PROXY_URL, 'audio/transcriptions');
export const OPENAI_TTS_URL = (api: string) => urlJoin(api, 'audio/speech');
export const OPENAI_STT_URL = (api: string) => urlJoin(api, 'audio/transcriptions');
5 changes: 2 additions & 3 deletions src/services/fetchOpenaiSTT.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OPENAI_API_KEY, OPENAI_PROXY_URL, OPENAI_STT_URL } from '@/const/api';
import { OPENAI_BASE_URL, OPENAI_STT_URL } from '@/const/api';
import { OpenAISTTPayload } from '@/server/types';
import { RecordMineType, getRecordMineType } from '@/utils/getRecordMineType';

Expand Down Expand Up @@ -26,8 +26,7 @@ export const fetchOpenaiSTT = async (
speech: Blob,
{ api = {}, model = 'whisper-1', mineType }: OpenaiSttOptions,
): Promise<string> => {
const key = api?.key || OPENAI_API_KEY;
const url = api?.proxy || OPENAI_PROXY_URL;
const { key, url = OPENAI_BASE_URL } = api;

const payload: OpenAISTTPayload = {
blob: speech,
Expand Down
5 changes: 2 additions & 3 deletions src/services/fetchOpenaiTTS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OPENAI_API_KEY, OPENAI_PROXY_URL, OPENAI_TTS_URL } from '@/const/api';
import { OPENAI_BASE_URL, OPENAI_TTS_URL } from '@/const/api';
import { OpenAITTSPayload } from '@/server/types';
import { arrayBufferConvert } from '@/utils/arrayBufferConvert';
import { type SsmlOptions } from '@/utils/genSSML';
Expand All @@ -18,8 +18,7 @@ export const fetchOpenaiTTS = async (
input: string,
{ api = {}, model = 'tts-1', voice }: OpenaiTtsOptions,
): Promise<AudioBuffer> => {
const key = api?.key || OPENAI_API_KEY;
const url = api?.proxy || OPENAI_PROXY_URL;
const { key, url = OPENAI_BASE_URL } = api;

const payload: OpenAITTSPayload = {
input,
Expand Down

0 comments on commit d875be6

Please sign in to comment.