Skip to content

Commit

Permalink
Merge pull request #1397 from arc53/tts
Browse files Browse the repository at this point in the history
Add endpoint for Text-To-Speech conversion
  • Loading branch information
dartpain authored Oct 29, 2024
2 parents 1c791f2 + 7ff3a31 commit 631e77c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
25 changes: 25 additions & 0 deletions application/api/user/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from application.extensions import api
from application.utils import check_required_fields
from application.vectorstore.vector_creator import VectorCreator
from application.tts.google_tts import GoogleTTS

mongo = MongoClient(settings.MONGO_URI)
db = mongo["docsgpt"]
Expand Down Expand Up @@ -1663,3 +1664,27 @@ def post(self):
return make_response(jsonify({"success": False, "error": str(err)}), 400)

return make_response(jsonify({"success": True}), 200)


@user_ns.route("/api/tts")
class TextToSpeech(Resource):
tts_model = api.model(
"TextToSpeechModel",
{
"text": fields.String(required=True, description="Text to be synthesized as audio"),
},
)

@api.expect(tts_model)
@api.doc(description="Synthesize audio speech from text")
def post(self):
data = request.get_json()
text = data["text"]
try:
tts_instance = GoogleTTS(text)
audio_base64, detected_language = tts_instance.text_to_speech()
return make_response(jsonify({"success": True,'audio_base64': audio_base64,'lang':detected_language}), 200)
except Exception as err:
return make_response(jsonify({"success": False, "error": str(err)}), 400)


1 change: 1 addition & 0 deletions application/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ vine==5.1.0
wcwidth==0.2.13
werkzeug==3.0.4
yarl==1.11.1
gTTS==2.3.2
10 changes: 10 additions & 0 deletions application/tts/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from abc import ABC, abstractmethod


class BaseTTS(ABC):
def __init__(self):
pass

@abstractmethod
def text_to_speech(self, *args, **kwargs):
pass
19 changes: 19 additions & 0 deletions application/tts/google_tts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import io
import base64
from gtts import gTTS
from application.tts.base import BaseTTS


class GoogleTTS(BaseTTS):
def __init__(self, text):
self.text = text


def text_to_speech(self):
lang = "en"
audio_fp = io.BytesIO()
tts = gTTS(text=self.text, lang=lang, slow=False)
tts.write_to_fp(audio_fp)
audio_fp.seek(0)
audio_base64 = base64.b64encode(audio_fp.read()).decode("utf-8")
return audio_base64, lang

0 comments on commit 631e77c

Please sign in to comment.