Skip to content

Commit

Permalink
♻️ refactor: Refactor api
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 authored and 闻冰 committed Nov 15, 2023
1 parent 8698bc7 commit a7c0095
Show file tree
Hide file tree
Showing 39 changed files with 477 additions and 638 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ Click button below to deploy your private plugins' gateway.

This project provides some additional configuration items set with environment variables:

| Environment Variable | Description | Default |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
| `ALLOW_ORIGINS` | Allow origins , string or string array | |
| `OPENAI_API_KEY` | This is the API key you apply on the OpenAI account page | `sk-xxxxxx...xxxxxx` |
| `OPENAI_PROXY_URL` | If you manually configure the OpenAI interface proxy, you can use this configuration item to override the default OpenAI API request base URL | `https://api.openai.com/v1` |
| `AZURE_SPEECH_KEY` | This is the API key of Azure Speech Service | |
| `AZURE_SPEECH_REGION` | This is the region of Azure Speech Service | |
| `AZURE_SPEECH_PROXY_URL` | If you manually configure the AZURE Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/azure-speech` |
| `MICROSOFT_SPEECH_PROXY_URL` | If you manually configure the Microsoft Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/microsoft-speech` |
| `EDDGE_API_TOKEN` | This is the API key of Edge Speech Service | |
| `EDDGE_PROXY_URL` | If you manually configure the Edge interface proxy, you can use this configuration item to override the default Edge wss request base URL | |
| Environment Variable | Description | Default |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
| `ALLOW_ORIGINS` | Allow origins , string or string array | |
| `OPENAI_API_KEY` | This is the API key you apply on the OpenAI account page | `sk-xxxxxx...xxxxxx` |
| `OPENAI_PROXY_URL` | If you manually configure the OpenAI interface proxy, you can use this configuration item to override the default OpenAI API request base URL | `https://api.openai.com/v1` |
| `AZURE_SPEECH_KEY` | This is the API key of Azure Speech Service | |
| `AZURE_SPEECH_REGION` | This is the region of Azure Speech Service | |
| `AZURE_SPEECH_PROXY_URL` | If you manually configure the AZURE Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/azure-speech` |
| `MICROSOFT_SPEECH_API_URL` | If you manually configure the Microsoft Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/microsoft-speech` |
| `EDGE_API_TOKEN` | This is the API key of Edge Speech Service | |
| `EDGE_SPEECH_API_URL` | If you manually configure the Edge interface proxy, you can use this configuration item to override the default Edge wss request base URL | |

<div align="right">

Expand Down
28 changes: 0 additions & 28 deletions api/azure-speech.ts

This file was deleted.

13 changes: 13 additions & 0 deletions api/edge-speech.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createEdgeSpeechComletion } from '../src/server/createEdgeSpeechComletion';
import { EdgeSpeechPayload } 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 EdgeSpeechPayload;
const res = await createEdgeSpeechComletion({ payload });
return res;
};
12 changes: 5 additions & 7 deletions api/microsoft-speech.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import cors from '../src/server/cors';
import { getAllowOrigins } from '../src/server/getAllowOrigins';
import { handleMicrosoftSpeechRequest } from '../src/server/handleMicrosoftSpeechRequest';
import { createMicrosoftSpeechComletion } from '../src/server/createMicrosoftSpeechComletion';
import { MicrosoftSpeechPayload } 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 origin = getAllowOrigins(req);
if (!origin) return new Response('Origin Not Allowed', { status: 403 });
const res = await handleMicrosoftSpeechRequest(req);
return cors(req, new Response(res.body, res), { methods: ['POST'], origin });
const payload = (await req.json()) as MicrosoftSpeechPayload;
const res = await createMicrosoftSpeechComletion({ payload });
return res;
};
19 changes: 19 additions & 0 deletions api/open-stt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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;
if (!OPENAI_API_KEY) return new Response('OPENAI_API_KEY is not set', { status: 500 });
const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
const res = await createOpenaiAudioTranscriptionsCompletion({ openai, payload });
return res;
};
19 changes: 19 additions & 0 deletions api/openai-tts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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;
if (!OPENAI_API_KEY) return new Response('OPENAI_API_KEY is not set', { status: 500 });
const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
const res = await createOpenaiAudioSpeechCompletion({ openai, payload });
return res;
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@
"antd-style": "^3",
"lodash-es": "^4",
"lucide-react": "latest",
"microsoft-cognitiveservices-speech-sdk": "^1",
"openai": "^4.17.3",
"query-string": "^8",
"react-error-boundary": "^4.0.11",
"react-layout-kit": "^1",
"ssml-document": "^1",
"swr": "^2",
"url-join": "^5",
"uuid": "^9"
Expand Down
32 changes: 10 additions & 22 deletions src/const/api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import urlJoin from 'url-join';

export const MICROSOFT_SPPECH_URL =
export const MICROSOFT_SPEECH_URL =
'https://southeastasia.api.speech.microsoft.com/accfreetrial/texttospeech/acc/v3.0-beta1/vcg/speak';
export const MICROSOFT_SPEECH_PROXY_URL =
process.env.MICROSOFT_SPEECH_PROXY_URL ||
process.env.NEXT_PUBLIC_MICROSOFT_SPEECH_PROXY_URL ||
'/api/microsoft-speech';
export const AZURE_SPEECH_PROXY_URL =
process.env.AZURE_SPEECH_PROXY_URL ||
process.env.NEXT_PUBLIC_AZURE_SPEECH_PROXY_URL ||
'/api/azure-speech';
export const EDGE_SPEECH_URL =
'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
export const EDGE_API_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';

export const MICROSOFT_SPEECH_API_URL = '/api/microsoft-speech';
export const EDGE_SPEECH_API_URL = '/api/edge-speech';
export const OPENAI_TTS_API_URL = '/api/openai-tts';
export const OPENAI_STT_API_URL = '/api/openai-stt';

export const AZURE_SPEECH_KEY =
process.env.AZURE_SPEECH_KEY || process.env.NEXT_PUBLIC_AZURE_SPEECH_KEY || '';
export const AZURE_SPEECH_REGION =
Expand All @@ -20,14 +19,3 @@ 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 EDDGE_PROXY_URL =
process.env.EDDGE_PROXY_URL ||
process.env.NEXT_PUBLIC_EDDGE_PROXY_UR ||
'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
export const EDDGE_API_TOKEN =
process.env.EDDGE_API_TOKEN ||
process.env.NEXT_PUBLIC_EDDGE_API_TOKEN ||
'6A5AA1D4EAFF4E9FB37E23D68491D6F4';
File renamed without changes.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ export { default as AudioVisualizer, type AudioVisualizerProps } from './AudioVi
export { default as azureVoiceList } from './data/azureVoiceList';
export { default as edgeVoiceList } from './data/edgeVoiceList';
export { default as voiceLocale } from './data/locales';
export { default as nameList } from './data/nameList';
export { default as openaiVoiceList } from './data/openaiVoiceList';
export { default as voiceList } from './data/voiceList';
export { useAudioPlayer } from './hooks/useAudioPlayer';
export { useAudioVisualizer } from './hooks/useAudioVisualizer';
export { useBlobUrl } from './hooks/useBlobUrl';
export { useStreamAudioPlayer } from './hooks/useStreamAudioPlayer';
export { type AzureSpeechOptions, fetchAzureSpeech } from './services/fetchAzureSpeech';
export { type EdgeSpeechOptions, fetchEdgeSpeech } from './services/fetchEdgeSpeech';
export { fetchMicrosoftSpeech, type MicrosoftSpeechOptions } from './services/fetchMicrosoftSpeech';
export { fetchOpenaiSTT, type OpenaiSttOptions } from './services/fetchOpenaiSTT';
export { fetchOpenaiTTS, type OpenaiTtsOptions } from './services/fetchOpenaiTTS';
export { useAudioRecorder } from './useAudioRecorder';
export { useAzureSpeech } from './useAzureSpeech';
export { useEdgeSpeech } from './useEdgeSpeech';
export { useMicrosoftSpeech } from './useMicrosoftSpeech';
export {
Expand All @@ -42,3 +40,9 @@ export {
getSpeechSynthesVoiceOptions,
getVoiceLocaleOptions,
} from './utils/getVoiceList';
export {
EDGE_SPEECH_API_URL,
MICROSOFT_SPEECH_API_URL,
OPENAI_STT_API_URL,
OPENAI_TTS_API_URL,
} from '@/const/api';
7 changes: 5 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { handleAzureSpeechRequest } from './server/handleAzureSpeechRequest';
export { handleMicrosoftSpeechRequest } from './server/handleMicrosoftSpeechRequest';
export { createEdgeSpeechComletion } from '@/server/createEdgeSpeechComletion';
export { createMicrosoftSpeechComletion } from '@/server/createMicrosoftSpeechComletion';
export { createOpenaiAudioSpeechCompletion } from '@/server/createOpenaiAudioSpeechCompletion';
export { createOpenaiAudioTranscriptionsCompletion } from '@/server/createOpenaiAudioTranscriptionsCompletion';
export * from '@/server/types';
140 changes: 0 additions & 140 deletions src/server/cors.ts

This file was deleted.

Loading

0 comments on commit a7c0095

Please sign in to comment.