From e7700cf6e65fd737bf51962f8a5c5cb2414d5ab6 Mon Sep 17 00:00:00 2001 From: Lukas Holzner Date: Tue, 1 Oct 2024 11:10:47 +0200 Subject: [PATCH] Added tts endpoint --- backend/app/ai_manager.py | 36 +++++++++++++++++++++++++++++++++++- backend/app/main.py | 3 ++- backend/app/routers/tts.py | 10 ++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 backend/app/routers/tts.py diff --git a/backend/app/ai_manager.py b/backend/app/ai_manager.py index 8328fda..eba4410 100644 --- a/backend/app/ai_manager.py +++ b/backend/app/ai_manager.py @@ -1,10 +1,13 @@ import os from dotenv import load_dotenv -from fastapi import UploadFile +from fastapi import UploadFile, HTTPException +from fastapi.responses import FileResponse from openai import OpenAI from typing import List, Dict import json import io +import tempfile +from starlette.background import BackgroundTask # Load environment variables from .env file load_dotenv() @@ -160,3 +163,34 @@ async def convert_audio_to_text(audio_file: UploadFile): except Exception as e: print(f"Error in audio transcription: {str(e)}") return None + +async def convert_text_to_audio(text: str, voice: str = "alloy"): + """ + Convert text to audio using OpenAI's text-to-speech API and return a FastAPI FileResponse. + + :param text: The text to convert to speech + :param voice: The voice to use (default is "alloy") + :return: A FastAPI FileResponse containing the audio file + """ + try: + response = client.audio.speech.create( + model="tts-1", + voice=voice, + input=text + ) + + # Create a temporary file to store the audio content + with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file: + temp_file.write(response.content) + temp_file_path = temp_file.name + + # Return a FileResponse + return FileResponse( + path=temp_file_path, + media_type="audio/mpeg", + filename="speech.mp3", + background=BackgroundTask(lambda: os.unlink(temp_file_path)) + ) + except Exception as e: + print(f"Error in text-to-speech conversion: {str(e)}") + raise HTTPException(status_code=500, detail="Text-to-speech conversion failed") diff --git a/backend/app/main.py b/backend/app/main.py index 3b71314..a8ae498 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -6,7 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware from .database import engine from . import models -from .routers import meetings, users +from .routers import meetings, users, tts import os models.Base.metadata.create_all(bind=engine) @@ -34,6 +34,7 @@ # Include the router with the /api prefix app.include_router(meetings.router, tags=["Meetings"]) app.include_router(users.router, tags=["Users"]) +app.include_router(tts.router, tags=["TTS"]) @app.get("/", include_in_schema=False) async def serve_index(): diff --git a/backend/app/routers/tts.py b/backend/app/routers/tts.py new file mode 100644 index 0000000..ba2fa66 --- /dev/null +++ b/backend/app/routers/tts.py @@ -0,0 +1,10 @@ +from fastapi import APIRouter, Body +from fastapi.responses import FileResponse +from app.ai_manager import convert_text_to_audio + + +router = APIRouter() + +@router.post("/tts/") +async def tts(text: str = Body(...), voice: str = Body("alloy")): + return await convert_text_to_audio(text, voice) \ No newline at end of file