-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from richardr1126/server-tts
Server TTS
- Loading branch information
Showing
12 changed files
with
439 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import OpenAI from 'openai'; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
// Get API credentials from headers | ||
const openApiKey = req.headers.get('x-openai-key'); | ||
const openApiBaseUrl = req.headers.get('x-openai-base-url'); | ||
const { text, voice, speed } = await req.json(); | ||
console.log('Received TTS request:', text, voice, speed); | ||
|
||
if (!openApiKey || !openApiBaseUrl) { | ||
return NextResponse.json({ error: 'Missing API credentials' }, { status: 401 }); | ||
} | ||
|
||
if (!text || !voice || !speed) { | ||
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 }); | ||
} | ||
|
||
// Initialize OpenAI client | ||
const openai = new OpenAI({ | ||
apiKey: openApiKey, | ||
baseURL: openApiBaseUrl, | ||
}); | ||
|
||
// Request audio from OpenAI | ||
const response = await openai.audio.speech.create({ | ||
model: 'tts-1', | ||
voice: voice as "alloy", | ||
input: text, | ||
speed: speed, | ||
}); | ||
|
||
// Get the audio data as array buffer | ||
const arrayBuffer = await response.arrayBuffer(); | ||
|
||
// Return audio data with appropriate headers | ||
return new NextResponse(arrayBuffer); | ||
} catch (error) { | ||
console.error('Error generating TTS:', error); | ||
return NextResponse.json( | ||
{ error: 'Failed to generate audio' }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
const DEFAULT_VOICES = ['alloy', 'ash', 'coral', 'echo', 'fable', 'onyx', 'nova', 'sage', 'shimmer']; | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
// Get API credentials from headers | ||
const openApiKey = req.headers.get('x-openai-key'); | ||
const openApiBaseUrl = req.headers.get('x-openai-base-url'); | ||
|
||
if (!openApiKey || !openApiBaseUrl) { | ||
return NextResponse.json({ error: 'Missing API credentials' }, { status: 401 }); | ||
} | ||
|
||
// Request voices from OpenAI | ||
const response = await fetch(`${openApiBaseUrl}/audio/voices`, { | ||
headers: { | ||
'Authorization': `Bearer ${openApiKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error('Failed to fetch voices'); | ||
} | ||
|
||
const data = await response.json(); | ||
return NextResponse.json({ voices: data.voices || DEFAULT_VOICES }); | ||
} catch (error) { | ||
console.error('Error fetching voices:', error); | ||
// Return default voices on error | ||
return NextResponse.json({ voices: DEFAULT_VOICES }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.