Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add texttospeech pipeline to API, closes #552

davidmezzetti34fa089e68f8
diff --git a/src/python/txtai/api/routers/texttospeech.py b/src/python/txtai/api/routers/texttospeech.py
new file mode 100644
index 00000000..ef3bdbc9
--- /dev/null
+++ b/src/python/txtai/api/routers/texttospeech.py
@@ -0,0 +1,33 @@
+"""
+Defines API paths for TTS endpoints
+"""
+
+from typing import Optional
+
+from fastapi import APIRouter, Response
+
+from .. import application
+from ..route import EncodingAPIRoute
+
+router = APIRouter(route_class=EncodingAPIRoute)
+
+
[email protected]("/texttospeech")
+def texttospeech(text: str, speaker: Optional[str] = None, encoding: Optional[str] = "mp3"):
+ """
+ Generates speech from text.
+
+ Args:
+ text: text
+ speaker: speaker id, defaults to 1
+ encoding: optional audio encoding format
+
+ Returns:
+ Audio data
+ """
+
+ # Convert to audio
+ audio = application.get().pipeline("texttospeech", text, speaker=speaker, encoding=encoding)
+
+ # Write audio
+ return Response(audio, headers={"Content-Disposition": f"attachment;filename=speech.{encoding.lower()}"})