Skip to content

Commit

Permalink
Added tts endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-holzner committed Oct 1, 2024
1 parent 8bfaa98 commit e7700cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
36 changes: 35 additions & 1 deletion backend/app/ai_manager.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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")
3 changes: 2 additions & 1 deletion backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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():
Expand Down
10 changes: 10 additions & 0 deletions backend/app/routers/tts.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e7700cf

Please sign in to comment.