You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[tts rest api server code : main.py (using fastapi library)]
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import openai
import aiofiles
import os
import uuid
from pathlib import Path
from starlette.responses import StreamingResponse
import time
app = FastAPI()
authkey of yours any
REQUIRED_AUTH_KEY = "any generated key for you"
Model for the request
class TTSRequest(BaseModel):
api_key: str # openai api key
voice: str
model: str
input_text: str
auth_key: str #any generated key for you
Directory to save the mp3 files
TTS_DIR = "tts"
if not os.path.exists(TTS_DIR):
os.makedirs(TTS_DIR)
@app.post("/generate_audio/")
async def generate_audio(request: TTSRequest):
# Check the auth key
if request.auth_key != REQUIRED_AUTH_KEY:
raise HTTPException(status_code=403, detail="Invalid authentication key")
# Update the OpenAI API key for the request
openai.api_key = request.api_key
client = openai.OpenAI(api_key=request.api_key)
# Generate audio using OpenAI's TTS API
speech_file_name = f"{uuid.uuid4()}.mp3"
speech_file_path = Path(TTS_DIR) / speech_file_name
try:
response = client.audio.speech.create(
model=request.model,
voice=request.voice,
input=request.input_text
)
response.stream_to_file(speech_file_path)
# Ensure the file is completely written
while not os.path.exists(speech_file_path):
time.sleep(0.1)
# Create the audio data URL
audio_data_url = f"https://your.custom.url/tts/{speech_file_name}"
# Return the HTML audio tag
html_audio = f'<audio controls><source src="{audio_data_url}" type="audio/mp3"></audio>'
return {"html_audio": html_audio}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/tts/{file_name}")
async def serve_audio(file_name: str):
file_path = os.path.join(TTS_DIR, file_name)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
async def iterfile():
async with aiofiles.open(file_path, 'rb') as f:
while True:
chunk = await f.read(1024) # 파일을 1KB씩 읽기
if not chunk:
break
yield chunk
return StreamingResponse(iterfile(), media_type="audio/mp3")
if name == "main":
uvicorn.run(app, host="0.0.0.0", port=8000)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
[tts rest api server code : main.py (using fastapi library)]
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import openai
import aiofiles
import os
import uuid
from pathlib import Path
from starlette.responses import StreamingResponse
import time
app = FastAPI()
authkey of yours any
REQUIRED_AUTH_KEY = "any generated key for you"
Model for the request
class TTSRequest(BaseModel):
api_key: str # openai api key
voice: str
model: str
input_text: str
auth_key: str #any generated key for you
Directory to save the mp3 files
TTS_DIR = "tts"
if not os.path.exists(TTS_DIR):
os.makedirs(TTS_DIR)
@app.post("/generate_audio/")
async def generate_audio(request: TTSRequest):
# Check the auth key
if request.auth_key != REQUIRED_AUTH_KEY:
raise HTTPException(status_code=403, detail="Invalid authentication key")
@app.get("/tts/{file_name}")
async def serve_audio(file_name: str):
file_path = os.path.join(TTS_DIR, file_name)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
if name == "main":
uvicorn.run(app, host="0.0.0.0", port=8000)
[in flowise custom tool code]
const fetch = require('node-fetch');
const url = 'https://your.custom.url/generate_audio/';
const data = {
'api_key': 'your open api key',
'voice': 'nova',
'model': 'tts-1',
'input_text': $answer_text,
'auth_key': 'REQUIRED_AUTH_KEY in the rest api main.py'
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
};
try {
const response = await fetch(url, options);
const result = await response.json();
console.log(result.html_audio);
return result.html_audio;
} catch (error) {
console.error(error);
return '';
}
Beta Was this translation helpful? Give feedback.
All reactions